Added hover and word break support to custom check box

This commit is contained in:
Jaex 2015-04-18 12:15:05 +03:00
parent de2bf2e887
commit 75b3a35627
3 changed files with 56 additions and 30 deletions

View file

@ -78,7 +78,7 @@ public override string Text
public int SpaceAfterCheckBox { get; set; }
private bool isChecked;
private bool isChecked, isHover;
private string text;
private LinearGradientBrush backgroundBrush, backgroundCheckedBrush, innerBorderBrush, innerBorderCheckedBrush;
@ -135,28 +135,18 @@ protected override void OnPaint(PaintEventArgs pe)
}
}
private void DrawBackground(Graphics g)
protected override void OnMouseEnter(EventArgs e)
{
if (Checked)
{
g.FillRectangle(backgroundCheckedBrush, new Rectangle(2, 2, checkBoxSize - 4, checkBoxSize - 4));
g.DrawRectangle(innerBorderCheckedPen, new Rectangle(1, 1, checkBoxSize - 3, checkBoxSize - 3));
}
else
{
g.FillRectangle(backgroundBrush, new Rectangle(2, 2, checkBoxSize - 4, checkBoxSize - 4));
g.DrawRectangle(innerBorderPen, new Rectangle(1, 1, checkBoxSize - 3, checkBoxSize - 3));
}
g.DrawRectangle(borderPen, new Rectangle(0, 0, checkBoxSize - 1, checkBoxSize - 1));
base.OnMouseEnter(e);
isHover = true;
Refresh();
}
private void DrawText(Graphics g)
protected override void OnMouseLeave(EventArgs e)
{
Rectangle rect = new Rectangle(checkBoxSize + SpaceAfterCheckBox, 0, ClientRectangle.Width - checkBoxSize + SpaceAfterCheckBox, ClientRectangle.Height);
TextFormatFlags tff = TextFormatFlags.Left | TextFormatFlags.Top;
TextRenderer.DrawText(g, Text, Font, new Rectangle(rect.X, rect.Y + 1, rect.Width, rect.Height + 1), Color.Black, tff);
TextRenderer.DrawText(g, Text, Font, rect, ForeColor, tff);
base.OnMouseLeave(e);
isHover = false;
Refresh();
}
protected override void OnClick(EventArgs e)
@ -174,6 +164,38 @@ protected virtual void OnCheckedChanged(EventArgs e)
}
}
private void DrawBackground(Graphics g)
{
if (Checked)
{
g.FillRectangle(backgroundCheckedBrush, new Rectangle(2, 2, checkBoxSize - 4, checkBoxSize - 4));
g.DrawRectangle(innerBorderCheckedPen, new Rectangle(1, 1, checkBoxSize - 3, checkBoxSize - 3));
}
else
{
g.FillRectangle(backgroundBrush, new Rectangle(2, 2, checkBoxSize - 4, checkBoxSize - 4));
if (isHover)
{
g.DrawRectangle(innerBorderCheckedPen, new Rectangle(1, 1, checkBoxSize - 3, checkBoxSize - 3));
}
else
{
g.DrawRectangle(innerBorderPen, new Rectangle(1, 1, checkBoxSize - 3, checkBoxSize - 3));
}
}
g.DrawRectangle(borderPen, new Rectangle(0, 0, checkBoxSize - 1, checkBoxSize - 1));
}
private void DrawText(Graphics g)
{
Rectangle rect = new Rectangle(checkBoxSize + SpaceAfterCheckBox, 0, ClientRectangle.Width - checkBoxSize + SpaceAfterCheckBox, ClientRectangle.Height);
TextFormatFlags tff = TextFormatFlags.Left | TextFormatFlags.Top | TextFormatFlags.WordBreak;
TextRenderer.DrawText(g, Text, Font, new Rectangle(rect.X, rect.Y + 1, rect.Width, rect.Height + 1), Color.Black, tff);
TextRenderer.DrawText(g, Text, Font, rect, ForeColor, tff);
}
protected override void Dispose(bool disposing)
{
if (backgroundBrush != null) backgroundBrush.Dispose();

View file

@ -58,6 +58,7 @@ private void InitializeComponent()
//
// cbDontShow
//
this.cbDontShow.BackColor = System.Drawing.Color.Transparent;
resources.ApplyResources(this.cbDontShow, "cbDontShow");
this.cbDontShow.ForeColor = System.Drawing.Color.White;
this.cbDontShow.Name = "cbDontShow";
@ -69,10 +70,10 @@ private void InitializeComponent()
resources.ApplyResources(this, "$this");
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(70)))), ((int)(((byte)(70)))), ((int)(((byte)(70)))));
this.Controls.Add(this.cbDontShow);
this.Controls.Add(this.lblText);
this.Controls.Add(this.btnNo);
this.Controls.Add(this.btnYes);
this.Controls.Add(this.cbDontShow);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "UpdateMessageBox";

View file

@ -145,13 +145,13 @@
<value>$this</value>
</data>
<data name="&gt;&gt;lblText.ZOrder" xml:space="preserve">
<value>1</value>
<value>0</value>
</data>
<data name="btnNo.Font" type="System.Drawing.Font, System.Drawing">
<value>Arial, 12pt</value>
</data>
<data name="btnNo.Location" type="System.Drawing.Point, System.Drawing">
<value>280, 112</value>
<value>280, 120</value>
</data>
<data name="btnNo.Size" type="System.Drawing.Size, System.Drawing">
<value>104, 32</value>
@ -172,13 +172,13 @@
<value>$this</value>
</data>
<data name="&gt;&gt;btnNo.ZOrder" xml:space="preserve">
<value>2</value>
<value>1</value>
</data>
<data name="btnYes.Font" type="System.Drawing.Font, System.Drawing">
<value>Arial, 12pt</value>
</data>
<data name="btnYes.Location" type="System.Drawing.Point, System.Drawing">
<value>168, 112</value>
<value>168, 120</value>
</data>
<data name="btnYes.Size" type="System.Drawing.Size, System.Drawing">
<value>104, 32</value>
@ -199,22 +199,22 @@
<value>$this</value>
</data>
<data name="&gt;&gt;btnYes.ZOrder" xml:space="preserve">
<value>3</value>
<value>2</value>
</data>
<data name="cbDontShow.Font" type="System.Drawing.Font, System.Drawing">
<value>Arial, 8pt</value>
</data>
<data name="cbDontShow.Location" type="System.Drawing.Point, System.Drawing">
<value>8, 88</value>
<value>16, 88</value>
</data>
<data name="cbDontShow.Size" type="System.Drawing.Size, System.Drawing">
<value>376, 16</value>
<value>368, 32</value>
</data>
<data name="cbDontShow.TabIndex" type="System.Int32, mscorlib">
<value>3</value>
</data>
<data name="cbDontShow.Text" xml:space="preserve">
<value>Don't show until next startup</value>
<value>Do not prompt again until ShareX starts next time</value>
</data>
<data name="&gt;&gt;cbDontShow.Name" xml:space="preserve">
<value>cbDontShow</value>
@ -226,7 +226,7 @@
<value>$this</value>
</data>
<data name="&gt;&gt;cbDontShow.ZOrder" xml:space="preserve">
<value>0</value>
<value>3</value>
</data>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
@ -235,7 +235,10 @@
<value>6, 13</value>
</data>
<data name="$this.ClientSize" type="System.Drawing.Size, System.Drawing">
<value>399, 155</value>
<value>399, 168</value>
</data>
<data name="$this.ImeMode" type="System.Windows.Forms.ImeMode, System.Windows.Forms">
<value>NoControl</value>
</data>
<data name="$this.StartPosition" type="System.Windows.Forms.FormStartPosition, System.Windows.Forms">
<value>CenterScreen</value>