Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace SelfDrawRadioButton
- {
- public partial class Form1 : System.Windows.Forms.Form
- {
- System.Windows.Forms.GroupBox m_group_box = new System.Windows.Forms.GroupBox();
- MyRadioButton m_radio_button1 = new MyRadioButton();
- MyRadioButton m_radio_button2 = new MyRadioButton();
- MyRadioButton m_radio_button3 = new MyRadioButton();
- public Form1()
- {
- try
- {
- InitializeComponent();
- m_group_box.Location = new System.Drawing.Point(10, 10);
- m_group_box.Size = new System.Drawing.Size(200, 110);
- m_group_box.Text = "GroupBox";
- Controls.Add(m_group_box);
- m_radio_button1.Location = new System.Drawing.Point(10, 20);
- m_radio_button1.AutoSize = true;
- m_radio_button1.Text = "RadioButton1";
- m_group_box.Controls.Add(m_radio_button1);
- m_radio_button2.Location = new System.Drawing.Point(10, 50);
- m_radio_button2.AutoSize = true;
- m_radio_button2.Text = "RadioButton2";
- m_group_box.Controls.Add(m_radio_button2);
- m_radio_button3.Location = new System.Drawing.Point(10, 80);
- m_radio_button3.AutoSize = true;
- m_radio_button3.Text = "RadioButton3";
- m_group_box.Controls.Add(m_radio_button3);
- m_radio_button1.CheckedChanged += M_radio_button1_CheckedChanged;
- m_radio_button2.CheckedChanged += M_radio_button2_CheckedChanged;
- m_radio_button3.CheckedChanged += M_radio_button3_CheckedChanged;
- m_radio_button1.Checked = true;
- }
- catch
- {
- }
- }
- /// <summary>
- /// チェックが変更された
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void M_radio_button1_CheckedChanged(object sender, System.EventArgs e)
- {
- try
- {
- if (false == m_radio_button1.Checked)
- {
- System.Console.WriteLine("RadioButton1 はチェックされていない。");
- }
- else
- {
- System.Console.WriteLine("RadioButton1 はチェックされている。");
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// チェックが変更された
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void M_radio_button2_CheckedChanged(object sender, System.EventArgs e)
- {
- try
- {
- if (false == m_radio_button2.Checked)
- {
- System.Console.WriteLine("RadioButton2 はチェックされていない。");
- }
- else
- {
- System.Console.WriteLine("RadioButton2 はチェックされている。");
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// チェックが変更された
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void M_radio_button3_CheckedChanged(object sender, System.EventArgs e)
- {
- try
- {
- if (false == m_radio_button3.Checked)
- {
- System.Console.WriteLine("RadioButton3 はチェックされていない。");
- }
- else
- {
- System.Console.WriteLine("RadioButton3 はチェックされている。");
- }
- }
- catch
- {
- }
- }
- }
- /// <summary>
- /// ラジオボタン
- /// </summary>
- public class MyRadioButton : System.Windows.Forms.RadioButton
- {
- /// <summary>
- /// マウスカーソルが乗っている
- /// </summary>
- protected bool m_is_mouse_over = false;
- /// <summary>
- /// 背景色
- /// </summary>
- public static System.Drawing.Color m_back_color = System.Drawing.Color.FromArgb(255, 100, 100, 100);
- /// <summary>
- /// マウスカーソルが乗っている状態の背景色
- /// </summary>
- public static System.Drawing.Color m_mouse_over_back_color = System.Drawing.Color.FromArgb(255, 150, 150, 150);
- /// <summary>
- /// チェックエリアの色
- /// </summary>
- public static System.Drawing.Color m_check_area_color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
- /// <summary>
- /// チェックの色
- /// </summary>
- public static System.Drawing.Color m_check_color = System.Drawing.Color.FromArgb(255, 0, 128, 255);
- /// <summary>
- /// 前景色
- /// </summary>
- public static System.Drawing.Color m_fore_color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
- /// <summary>
- /// マウスカーソルが乗っている状態の前景色
- /// </summary>
- public static System.Drawing.Color m_mouse_over_fore_color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
- /// <summary>
- /// 再描画
- /// </summary>
- /// <param name="pevent"></param>
- protected override void OnPaint(System.Windows.Forms.PaintEventArgs pevent)
- {
- try
- {
- using (System.Drawing.StringFormat format = new System.Drawing.StringFormat())
- {
- int space_size1 = 0, space_size2 = 0; // スペースのサイズ
- format.Alignment = System.Drawing.StringAlignment.Near;
- format.LineAlignment = System.Drawing.StringAlignment.Center;
- space_size1 = (int)(0.18f * (float)Height);
- space_size2 = (int)(0.28f * (float)Height);
- // マウスカーソルが乗ってない & フォーカスがない
- if ((false == m_is_mouse_over) && (false == Focused))
- {
- pevent.Graphics.FillRectangle(new System.Drawing.SolidBrush(m_back_color), pevent.ClipRectangle);
- pevent.Graphics.FillEllipse(new System.Drawing.SolidBrush(m_check_area_color), space_size1, space_size1, Height - (space_size1 + space_size1), Height - (space_size1 + space_size1));
- pevent.Graphics.DrawString(Text, Font, new System.Drawing.SolidBrush(m_fore_color), new System.Drawing.RectangleF(Height, 0, Size.Width - Height, Size.Height), format);
- }
- // マウスカーソルが乗っている
- else
- {
- pevent.Graphics.FillRectangle(new System.Drawing.SolidBrush(m_mouse_over_back_color), pevent.ClipRectangle);
- pevent.Graphics.FillEllipse(new System.Drawing.SolidBrush(m_check_area_color), space_size1, space_size1, Height - (space_size1 + space_size1), Height - (space_size1 + space_size1));
- pevent.Graphics.DrawString(Text, Font, new System.Drawing.SolidBrush(m_mouse_over_fore_color), new System.Drawing.RectangleF(Height, 0, Size.Width - Height, Size.Height), format);
- }
- // チェックが付いている
- if (true == Checked)
- {
- pevent.Graphics.FillEllipse(new System.Drawing.SolidBrush(m_check_color), space_size2, space_size2, Height - (space_size2 + space_size2), Height - (space_size2 + space_size2));
- }
- }
- }
- catch
- {
- }
- }
- /// <summary>
- /// マウスカーソルが触れた
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseEnter(System.EventArgs e)
- {
- try
- {
- base.OnMouseEnter(e);
- m_is_mouse_over = true;
- Invalidate();
- }
- catch
- {
- }
- }
- /// <summary>
- /// マウスカーソルが離れた
- /// </summary>
- /// <param name="e"></param>
- protected override void OnMouseLeave(System.EventArgs e)
- {
- try
- {
- base.OnMouseLeave(e);
- m_is_mouse_over = false;
- Invalidate();
- }
- catch
- {
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement