Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. namespace SelfDrawRadioButton
  2. {
  3. public partial class Form1 : System.Windows.Forms.Form
  4. {
  5. System.Windows.Forms.GroupBox m_group_box = new System.Windows.Forms.GroupBox();
  6. MyRadioButton m_radio_button1 = new MyRadioButton();
  7. MyRadioButton m_radio_button2 = new MyRadioButton();
  8. MyRadioButton m_radio_button3 = new MyRadioButton();
  9.  
  10. public Form1()
  11. {
  12. try
  13. {
  14. InitializeComponent();
  15.  
  16. m_group_box.Location = new System.Drawing.Point(10, 10);
  17. m_group_box.Size = new System.Drawing.Size(200, 110);
  18. m_group_box.Text = "GroupBox";
  19. Controls.Add(m_group_box);
  20.  
  21. m_radio_button1.Location = new System.Drawing.Point(10, 20);
  22. m_radio_button1.AutoSize = true;
  23. m_radio_button1.Text = "RadioButton1";
  24. m_group_box.Controls.Add(m_radio_button1);
  25. m_radio_button2.Location = new System.Drawing.Point(10, 50);
  26. m_radio_button2.AutoSize = true;
  27. m_radio_button2.Text = "RadioButton2";
  28. m_group_box.Controls.Add(m_radio_button2);
  29. m_radio_button3.Location = new System.Drawing.Point(10, 80);
  30. m_radio_button3.AutoSize = true;
  31. m_radio_button3.Text = "RadioButton3";
  32. m_group_box.Controls.Add(m_radio_button3);
  33.  
  34. m_radio_button1.CheckedChanged += M_radio_button1_CheckedChanged;
  35. m_radio_button2.CheckedChanged += M_radio_button2_CheckedChanged;
  36. m_radio_button3.CheckedChanged += M_radio_button3_CheckedChanged;
  37. m_radio_button1.Checked = true;
  38. }
  39. catch
  40. {
  41. }
  42. }
  43.  
  44. /// <summary>
  45. /// チェックが変更された
  46. /// </summary>
  47. /// <param name="sender"></param>
  48. /// <param name="e"></param>
  49. private void M_radio_button1_CheckedChanged(object sender, System.EventArgs e)
  50. {
  51. try
  52. {
  53. if (false == m_radio_button1.Checked)
  54. {
  55. System.Console.WriteLine("RadioButton1 はチェックされていない。");
  56. }
  57. else
  58. {
  59. System.Console.WriteLine("RadioButton1 はチェックされている。");
  60. }
  61. }
  62. catch
  63. {
  64. }
  65. }
  66.  
  67. /// <summary>
  68. /// チェックが変更された
  69. /// </summary>
  70. /// <param name="sender"></param>
  71. /// <param name="e"></param>
  72. private void M_radio_button2_CheckedChanged(object sender, System.EventArgs e)
  73. {
  74. try
  75. {
  76. if (false == m_radio_button2.Checked)
  77. {
  78. System.Console.WriteLine("RadioButton2 はチェックされていない。");
  79. }
  80. else
  81. {
  82. System.Console.WriteLine("RadioButton2 はチェックされている。");
  83. }
  84. }
  85. catch
  86. {
  87. }
  88. }
  89.  
  90. /// <summary>
  91. /// チェックが変更された
  92. /// </summary>
  93. /// <param name="sender"></param>
  94. /// <param name="e"></param>
  95. private void M_radio_button3_CheckedChanged(object sender, System.EventArgs e)
  96. {
  97. try
  98. {
  99. if (false == m_radio_button3.Checked)
  100. {
  101. System.Console.WriteLine("RadioButton3 はチェックされていない。");
  102. }
  103. else
  104. {
  105. System.Console.WriteLine("RadioButton3 はチェックされている。");
  106. }
  107. }
  108. catch
  109. {
  110. }
  111. }
  112. }
  113.  
  114. /// <summary>
  115. /// ラジオボタン
  116. /// </summary>
  117. public class MyRadioButton : System.Windows.Forms.RadioButton
  118. {
  119. /// <summary>
  120. /// マウスカーソルが乗っている
  121. /// </summary>
  122. protected bool m_is_mouse_over = false;
  123.  
  124. /// <summary>
  125. /// 背景色
  126. /// </summary>
  127. public static System.Drawing.Color m_back_color = System.Drawing.Color.FromArgb(255, 100, 100, 100);
  128. /// <summary>
  129. /// マウスカーソルが乗っている状態の背景色
  130. /// </summary>
  131. public static System.Drawing.Color m_mouse_over_back_color = System.Drawing.Color.FromArgb(255, 150, 150, 150);
  132. /// <summary>
  133. /// チェックエリアの色
  134. /// </summary>
  135. public static System.Drawing.Color m_check_area_color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
  136. /// <summary>
  137. /// チェックの色
  138. /// </summary>
  139. public static System.Drawing.Color m_check_color = System.Drawing.Color.FromArgb(255, 0, 128, 255);
  140. /// <summary>
  141. /// 前景色
  142. /// </summary>
  143. public static System.Drawing.Color m_fore_color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
  144. /// <summary>
  145. /// マウスカーソルが乗っている状態の前景色
  146. /// </summary>
  147. public static System.Drawing.Color m_mouse_over_fore_color = System.Drawing.Color.FromArgb(255, 255, 255, 255);
  148.  
  149. /// <summary>
  150. /// 再描画
  151. /// </summary>
  152. /// <param name="pevent"></param>
  153. protected override void OnPaint(System.Windows.Forms.PaintEventArgs pevent)
  154. {
  155. try
  156. {
  157. using (System.Drawing.StringFormat format = new System.Drawing.StringFormat())
  158. {
  159. int space_size1 = 0, space_size2 = 0; // スペースのサイズ
  160.  
  161. format.Alignment = System.Drawing.StringAlignment.Near;
  162. format.LineAlignment = System.Drawing.StringAlignment.Center;
  163.  
  164. space_size1 = (int)(0.18f * (float)Height);
  165. space_size2 = (int)(0.28f * (float)Height);
  166.  
  167. // マウスカーソルが乗ってない & フォーカスがない
  168. if ((false == m_is_mouse_over) && (false == Focused))
  169. {
  170. pevent.Graphics.FillRectangle(new System.Drawing.SolidBrush(m_back_color), pevent.ClipRectangle);
  171. 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));
  172. pevent.Graphics.DrawString(Text, Font, new System.Drawing.SolidBrush(m_fore_color), new System.Drawing.RectangleF(Height, 0, Size.Width - Height, Size.Height), format);
  173. }
  174. // マウスカーソルが乗っている
  175. else
  176. {
  177. pevent.Graphics.FillRectangle(new System.Drawing.SolidBrush(m_mouse_over_back_color), pevent.ClipRectangle);
  178. 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));
  179. 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);
  180. }
  181. // チェックが付いている
  182. if (true == Checked)
  183. {
  184. pevent.Graphics.FillEllipse(new System.Drawing.SolidBrush(m_check_color), space_size2, space_size2, Height - (space_size2 + space_size2), Height - (space_size2 + space_size2));
  185. }
  186. }
  187. }
  188. catch
  189. {
  190. }
  191. }
  192.  
  193. /// <summary>
  194. /// マウスカーソルが触れた
  195. /// </summary>
  196. /// <param name="e"></param>
  197. protected override void OnMouseEnter(System.EventArgs e)
  198. {
  199. try
  200. {
  201. base.OnMouseEnter(e);
  202. m_is_mouse_over = true;
  203. Invalidate();
  204. }
  205. catch
  206. {
  207. }
  208. }
  209.  
  210. /// <summary>
  211. /// マウスカーソルが離れた
  212. /// </summary>
  213. /// <param name="e"></param>
  214. protected override void OnMouseLeave(System.EventArgs e)
  215. {
  216. try
  217. {
  218. base.OnMouseLeave(e);
  219. m_is_mouse_over = false;
  220. Invalidate();
  221. }
  222. catch
  223. {
  224. }
  225. }
  226. }
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement