From 22b9bb90cc31f56d562d97bf3f0da2a9d44ebaf7 Mon Sep 17 00:00:00 2001 From: Robin Date: Mon, 7 Dec 2015 15:49:58 +0000 Subject: [PATCH] Added DarkRadioButton --- DarkUI/Config/Consts.cs | 1 + DarkUI/Controls/DarkCheckBox.cs | 16 +- DarkUI/Controls/DarkRadioButton.cs | 348 ++++++++++++++++++ DarkUI/DarkUI.csproj | 3 + .../Forms/Dialogs/DialogControls.Designer.cs | 106 +++++- 5 files changed, 457 insertions(+), 17 deletions(-) create mode 100644 DarkUI/Controls/DarkRadioButton.cs diff --git a/DarkUI/Config/Consts.cs b/DarkUI/Config/Consts.cs index f234d2e..26a0152 100644 --- a/DarkUI/Config/Consts.cs +++ b/DarkUI/Config/Consts.cs @@ -9,6 +9,7 @@ public static int MinimumThumbSize = 11; public static int CheckBoxSize = 12; + public static int RadioButtonSize = 12; public const int ToolWindowHeaderSize = 25; public const int DocumentTabAreaSize = 24; diff --git a/DarkUI/Controls/DarkCheckBox.cs b/DarkUI/Controls/DarkCheckBox.cs index a133f81..59d7fa8 100644 --- a/DarkUI/Controls/DarkCheckBox.cs +++ b/DarkUI/Controls/DarkCheckBox.cs @@ -1,5 +1,4 @@ using DarkUI.Config; -using DarkUI.Icons; using System; using System.ComponentModel; using System.Drawing; @@ -291,7 +290,6 @@ namespace DarkUI.Controls { if (Focused) { - textColor = Colors.BlueHighlight; borderColor = Colors.BlueHighlight; fillColor = Colors.BlueSelection; } @@ -321,7 +319,7 @@ namespace DarkUI.Controls using (var p = new Pen(borderColor)) { - var boxRect = new Rectangle(0, (rect.Height / 2) - (size / 2), size, size); + var boxRect = new Rectangle(0, (rect.Height / 2) - (size / 2) - 1, size, size); g.DrawRectangle(p, boxRect); } @@ -329,15 +327,21 @@ namespace DarkUI.Controls { using (var b = new SolidBrush(fillColor)) { - Rectangle boxRect = new Rectangle(2, (rect.Height / 2) - ((size - 4) / 2), size - 3, size - 3); + Rectangle boxRect = new Rectangle(2, (rect.Height / 2) - ((size - 4) / 2) - 1, size - 3, size - 3); g.FillRectangle(b, boxRect); } } using (var b = new SolidBrush(textColor)) { - var modRect = new Rectangle(size + 5, 0, rect.Width - (size + 5), rect.Height); - g.DrawString(Text, Font, b, modRect); + var stringFormat = new StringFormat + { + LineAlignment = StringAlignment.Near, + Alignment = StringAlignment.Center + }; + + var modRect = new Rectangle(size + 4, 0, rect.Width - size, rect.Height); + g.DrawString(Text, Font, b, modRect, stringFormat); } } diff --git a/DarkUI/Controls/DarkRadioButton.cs b/DarkUI/Controls/DarkRadioButton.cs new file mode 100644 index 0000000..7d2f64d --- /dev/null +++ b/DarkUI/Controls/DarkRadioButton.cs @@ -0,0 +1,348 @@ +using DarkUI.Config; +using System; +using System.ComponentModel; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Windows.Forms; + +namespace DarkUI.Controls +{ + public class DarkRadioButton : RadioButton + { + #region Field Region + + private DarkControlState _controlState = DarkControlState.Normal; + + private bool _spacePressed; + + #endregion + + #region Property Region + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new Appearance Appearance + { + get { return base.Appearance; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new bool AutoEllipsis + { + get { return base.AutoEllipsis; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new Image BackgroundImage + { + get { return base.BackgroundImage; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new ImageLayout BackgroundImageLayout + { + get { return base.BackgroundImageLayout; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new bool FlatAppearance + { + get { return false; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new FlatStyle FlatStyle + { + get { return base.FlatStyle; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new Image Image + { + get { return base.Image; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new ContentAlignment ImageAlign + { + get { return base.ImageAlign; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new int ImageIndex + { + get { return base.ImageIndex; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new string ImageKey + { + get { return base.ImageKey; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new ImageList ImageList + { + get { return base.ImageList; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new ContentAlignment TextAlign + { + get { return base.TextAlign; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new TextImageRelation TextImageRelation + { + get { return base.TextImageRelation; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new bool UseCompatibleTextRendering + { + get { return false; } + } + + [Browsable(false)] + [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] + public new bool UseVisualStyleBackColor + { + get { return false; } + } + + #endregion + + #region Constructor Region + + public DarkRadioButton() + { + SetStyle(ControlStyles.SupportsTransparentBackColor | + ControlStyles.OptimizedDoubleBuffer | + ControlStyles.ResizeRedraw | + ControlStyles.UserPaint, true); + } + + #endregion + + #region Method Region + + private void SetControlState(DarkControlState controlState) + { + if (_controlState != controlState) + { + _controlState = controlState; + Invalidate(); + } + } + + #endregion + + #region Event Handler Region + + protected override void OnMouseMove(MouseEventArgs e) + { + base.OnMouseMove(e); + + if (_spacePressed) + return; + + if (e.Button == MouseButtons.Left) + { + if (ClientRectangle.Contains(e.Location)) + SetControlState(DarkControlState.Pressed); + else + SetControlState(DarkControlState.Hover); + } + else + { + SetControlState(DarkControlState.Hover); + } + } + + protected override void OnMouseDown(MouseEventArgs e) + { + base.OnMouseDown(e); + + if (!ClientRectangle.Contains(e.Location)) + return; + + SetControlState(DarkControlState.Pressed); + } + + protected override void OnMouseUp(MouseEventArgs e) + { + base.OnMouseUp(e); + + if (_spacePressed) + return; + + SetControlState(DarkControlState.Normal); + } + + protected override void OnMouseLeave(EventArgs e) + { + base.OnMouseLeave(e); + + if (_spacePressed) + return; + + SetControlState(DarkControlState.Normal); + } + + protected override void OnMouseCaptureChanged(EventArgs e) + { + base.OnMouseCaptureChanged(e); + + if (_spacePressed) + return; + + var location = Cursor.Position; + + if (!ClientRectangle.Contains(location)) + SetControlState(DarkControlState.Normal); + } + + protected override void OnGotFocus(EventArgs e) + { + base.OnGotFocus(e); + + Invalidate(); + } + + protected override void OnLostFocus(EventArgs e) + { + base.OnLostFocus(e); + + _spacePressed = false; + + var location = Cursor.Position; + + if (!ClientRectangle.Contains(location)) + SetControlState(DarkControlState.Normal); + else + SetControlState(DarkControlState.Hover); + } + + protected override void OnKeyDown(KeyEventArgs e) + { + base.OnKeyDown(e); + + if (e.KeyCode == Keys.Space) + { + _spacePressed = true; + SetControlState(DarkControlState.Pressed); + } + } + + protected override void OnKeyUp(KeyEventArgs e) + { + base.OnKeyUp(e); + + if (e.KeyCode == Keys.Space) + { + _spacePressed = false; + + var location = Cursor.Position; + + if (!ClientRectangle.Contains(location)) + SetControlState(DarkControlState.Normal); + else + SetControlState(DarkControlState.Hover); + } + } + + #endregion + + #region Paint Region + + protected override void OnPaint(PaintEventArgs e) + { + var g = e.Graphics; + var rect = new Rectangle(0, 0, ClientSize.Width, ClientSize.Height); + + var size = Consts.RadioButtonSize; + + var textColor = Colors.LightText; + var borderColor = Colors.LightText; + var fillColor = Colors.LightestBackground; + + if (Enabled) + { + if (Focused) + { + borderColor = Colors.BlueHighlight; + fillColor = Colors.BlueSelection; + } + + if (_controlState == DarkControlState.Hover) + { + borderColor = Colors.BlueHighlight; + fillColor = Colors.BlueSelection; + } + else if (_controlState == DarkControlState.Pressed) + { + borderColor = Colors.GreyHighlight; + fillColor = Colors.GreySelection; + } + } + else + { + textColor = Colors.DisabledText; + borderColor = Colors.GreyHighlight; + fillColor = Colors.GreySelection; + } + + using (var b = new SolidBrush(Colors.GreyBackground)) + { + g.FillRectangle(b, rect); + } + + g.SmoothingMode = SmoothingMode.HighQuality; + + using (var p = new Pen(borderColor)) + { + var boxRect = new Rectangle(0, (rect.Height / 2) - (size / 2) - 1, size, size); + g.DrawEllipse(p, boxRect); + } + + if (Checked) + { + using (var b = new SolidBrush(fillColor)) + { + Rectangle boxRect = new Rectangle(3, (rect.Height / 2) - ((size - 7) / 2) - 2, size - 6, size - 6); + g.FillEllipse(b, boxRect); + } + } + + g.SmoothingMode = SmoothingMode.Default; + + using (var b = new SolidBrush(textColor)) + { + var stringFormat = new StringFormat + { + LineAlignment = StringAlignment.Near, + Alignment = StringAlignment.Center + }; + + var modRect = new Rectangle(size + 4, 0, rect.Width - size, rect.Height); + g.DrawString(Text, Font, b, modRect, stringFormat); + } + } + + #endregion + } +} diff --git a/DarkUI/DarkUI.csproj b/DarkUI/DarkUI.csproj index 1acab31..fd060f5 100644 --- a/DarkUI/DarkUI.csproj +++ b/DarkUI/DarkUI.csproj @@ -46,6 +46,9 @@ Component + + Component + Component diff --git a/Example/Forms/Dialogs/DialogControls.Designer.cs b/Example/Forms/Dialogs/DialogControls.Designer.cs index 6158edb..a2c49f1 100644 --- a/Example/Forms/Dialogs/DialogControls.Designer.cs +++ b/Example/Forms/Dialogs/DialogControls.Designer.cs @@ -39,8 +39,14 @@ namespace Example this.lstTest = new DarkUI.Controls.DarkListView(); this.pnlMessageBox = new DarkUI.Controls.DarkSectionPanel(); this.panel1 = new System.Windows.Forms.Panel(); + this.panel5 = new System.Windows.Forms.Panel(); + this.darkRadioButton3 = new DarkUI.Controls.DarkRadioButton(); + this.darkRadioButton2 = new DarkUI.Controls.DarkRadioButton(); + this.darkRadioButton1 = new DarkUI.Controls.DarkRadioButton(); + this.panel4 = new System.Windows.Forms.Panel(); this.darkCheckBox2 = new DarkUI.Controls.DarkCheckBox(); this.darkCheckBox1 = new DarkUI.Controls.DarkCheckBox(); + this.panel3 = new System.Windows.Forms.Panel(); this.btnMessageBox = new DarkUI.Controls.DarkButton(); this.panel2 = new System.Windows.Forms.Panel(); this.btnDialog = new DarkUI.Controls.DarkButton(); @@ -50,6 +56,9 @@ namespace Example this.pnlListView.SuspendLayout(); this.pnlMessageBox.SuspendLayout(); this.panel1.SuspendLayout(); + this.panel5.SuspendLayout(); + this.panel4.SuspendLayout(); + this.panel3.SuspendLayout(); this.panel2.SuspendLayout(); this.SuspendLayout(); // @@ -137,9 +146,9 @@ namespace Example // // panel1 // - this.panel1.Controls.Add(this.darkCheckBox2); - this.panel1.Controls.Add(this.darkCheckBox1); - this.panel1.Controls.Add(this.btnMessageBox); + this.panel1.Controls.Add(this.panel5); + this.panel1.Controls.Add(this.panel4); + this.panel1.Controls.Add(this.panel3); this.panel1.Controls.Add(this.panel2); this.panel1.Dock = System.Windows.Forms.DockStyle.Fill; this.panel1.Location = new System.Drawing.Point(1, 25); @@ -148,35 +157,99 @@ namespace Example this.panel1.Size = new System.Drawing.Size(220, 374); this.panel1.TabIndex = 0; // + // panel5 + // + this.panel5.Controls.Add(this.darkRadioButton3); + this.panel5.Controls.Add(this.darkRadioButton2); + this.panel5.Controls.Add(this.darkRadioButton1); + this.panel5.Dock = System.Windows.Forms.DockStyle.Top; + this.panel5.Location = new System.Drawing.Point(10, 147); + this.panel5.Name = "panel5"; + this.panel5.Size = new System.Drawing.Size(200, 100); + this.panel5.TabIndex = 12; + // + // darkRadioButton3 + // + this.darkRadioButton3.AutoSize = true; + this.darkRadioButton3.Checked = true; + this.darkRadioButton3.Enabled = false; + this.darkRadioButton3.Location = new System.Drawing.Point(0, 50); + this.darkRadioButton3.Name = "darkRadioButton3"; + this.darkRadioButton3.Size = new System.Drawing.Size(139, 19); + this.darkRadioButton3.TabIndex = 4; + this.darkRadioButton3.TabStop = true; + this.darkRadioButton3.Text = "Disabled radio button"; + // + // darkRadioButton2 + // + this.darkRadioButton2.AutoSize = true; + this.darkRadioButton2.Location = new System.Drawing.Point(0, 25); + this.darkRadioButton2.Name = "darkRadioButton2"; + this.darkRadioButton2.Size = new System.Drawing.Size(94, 19); + this.darkRadioButton2.TabIndex = 3; + this.darkRadioButton2.Text = "Radio button"; + // + // darkRadioButton1 + // + this.darkRadioButton1.AutoSize = true; + this.darkRadioButton1.Location = new System.Drawing.Point(0, 0); + this.darkRadioButton1.Name = "darkRadioButton1"; + this.darkRadioButton1.Size = new System.Drawing.Size(94, 19); + this.darkRadioButton1.TabIndex = 2; + this.darkRadioButton1.Text = "Radio button"; + // + // panel4 + // + this.panel4.AutoSize = true; + this.panel4.Controls.Add(this.darkCheckBox2); + this.panel4.Controls.Add(this.darkCheckBox1); + this.panel4.Dock = System.Windows.Forms.DockStyle.Top; + this.panel4.Location = new System.Drawing.Point(10, 90); + this.panel4.Name = "panel4"; + this.panel4.Padding = new System.Windows.Forms.Padding(0, 0, 0, 10); + this.panel4.Size = new System.Drawing.Size(200, 57); + this.panel4.TabIndex = 11; + // // darkCheckBox2 // this.darkCheckBox2.AutoSize = true; this.darkCheckBox2.Checked = true; this.darkCheckBox2.CheckState = System.Windows.Forms.CheckState.Checked; this.darkCheckBox2.Enabled = false; - this.darkCheckBox2.Location = new System.Drawing.Point(10, 111); + this.darkCheckBox2.Location = new System.Drawing.Point(0, 25); this.darkCheckBox2.Name = "darkCheckBox2"; this.darkCheckBox2.Size = new System.Drawing.Size(124, 19); - this.darkCheckBox2.TabIndex = 9; + this.darkCheckBox2.TabIndex = 13; this.darkCheckBox2.Text = "Disabled checkbox"; // // darkCheckBox1 // this.darkCheckBox1.AutoSize = true; - this.darkCheckBox1.Location = new System.Drawing.Point(10, 86); + this.darkCheckBox1.Location = new System.Drawing.Point(0, 0); this.darkCheckBox1.Name = "darkCheckBox1"; this.darkCheckBox1.Size = new System.Drawing.Size(121, 19); - this.darkCheckBox1.TabIndex = 8; + this.darkCheckBox1.TabIndex = 12; this.darkCheckBox1.Text = "Enabled checkbox"; // + // panel3 + // + this.panel3.AutoSize = true; + this.panel3.Controls.Add(this.btnMessageBox); + this.panel3.Dock = System.Windows.Forms.DockStyle.Top; + this.panel3.Location = new System.Drawing.Point(10, 50); + this.panel3.Name = "panel3"; + this.panel3.Padding = new System.Windows.Forms.Padding(0, 0, 0, 10); + this.panel3.Size = new System.Drawing.Size(200, 40); + this.panel3.TabIndex = 10; + // // btnMessageBox // this.btnMessageBox.Dock = System.Windows.Forms.DockStyle.Top; - this.btnMessageBox.Location = new System.Drawing.Point(10, 50); + this.btnMessageBox.Location = new System.Drawing.Point(0, 0); this.btnMessageBox.Name = "btnMessageBox"; this.btnMessageBox.Padding = new System.Windows.Forms.Padding(5); this.btnMessageBox.Size = new System.Drawing.Size(200, 30); - this.btnMessageBox.TabIndex = 6; + this.btnMessageBox.TabIndex = 12; this.btnMessageBox.Text = "Message Box"; // // panel2 @@ -217,6 +290,11 @@ namespace Example this.pnlMessageBox.ResumeLayout(false); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); + this.panel5.ResumeLayout(false); + this.panel5.PerformLayout(); + this.panel4.ResumeLayout(false); + this.panel4.PerformLayout(); + this.panel3.ResumeLayout(false); this.panel2.ResumeLayout(false); this.ResumeLayout(false); @@ -232,10 +310,16 @@ namespace Example private DarkListView lstTest; private DarkSectionPanel pnlMessageBox; private System.Windows.Forms.Panel panel1; - private DarkButton btnMessageBox; private System.Windows.Forms.Panel panel2; private DarkButton btnDialog; - private DarkCheckBox darkCheckBox1; + private System.Windows.Forms.Panel panel4; + private System.Windows.Forms.Panel panel3; + private DarkButton btnMessageBox; private DarkCheckBox darkCheckBox2; + private DarkCheckBox darkCheckBox1; + private System.Windows.Forms.Panel panel5; + private DarkRadioButton darkRadioButton2; + private DarkRadioButton darkRadioButton1; + private DarkRadioButton darkRadioButton3; } } \ No newline at end of file