PushButton.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. using CustomControls.HelperClasses;
  6. using System.Drawing.Design;
  7. namespace CustomControls.Win32Controls
  8. {
  9. public class PushButton:Button
  10. {
  11. #region "Variables"
  12. private Enumerations.ButtonState _State=Enumerations.ButtonState.Normal;
  13. private Image _Image= null;
  14. private ContentAlignment _TextAlign=ContentAlignment.MiddleLeft;
  15. private ContentAlignment _ImageAlign=ContentAlignment.MiddleRight;
  16. private StringFormat strFormat;
  17. #endregion
  18. #region "Properties"
  19. [Browsable(false)]
  20. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  21. public override Color ForeColor
  22. {
  23. get
  24. {
  25. return CustomControls.BaseClasses.AppColors.TextColor;
  26. }
  27. set
  28. {
  29. }
  30. }
  31. [Browsable(false)]
  32. [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
  33. public override Color BackColor
  34. {
  35. get
  36. {
  37. return CustomControls.BaseClasses.AppColors.ControlColor;
  38. }
  39. set
  40. {
  41. }
  42. }
  43. override protected System.Drawing.Size DefaultSize
  44. {
  45. get{return new System.Drawing.Size(100, 20);}
  46. }
  47. [Category("Appearance")]
  48. [DefaultValue(typeof(System.Drawing.Image),"null")]
  49. public new Image Image
  50. {
  51. get{return _Image;}
  52. set
  53. {
  54. if (value !=_Image)
  55. {
  56. _Image= value;
  57. Invalidate();
  58. }
  59. }
  60. }
  61. protected virtual Enumerations.ButtonState State
  62. {
  63. get{return _State;}
  64. set
  65. {
  66. if(value!=_State)
  67. {
  68. _State= value;
  69. Invalidate();
  70. }
  71. }
  72. }
  73. [Category("Appearance")]
  74. [DefaultValue(typeof(ContentAlignment),"MiddleLeft")]
  75. public override ContentAlignment TextAlign
  76. {
  77. get{return _TextAlign;}
  78. set
  79. {
  80. if (value!=_TextAlign)
  81. {
  82. _TextAlign= value;
  83. Invalidate();
  84. }
  85. }
  86. }
  87. [Category("Appearance")]
  88. [DefaultValue(typeof(ContentAlignment),"MiddleRight")]
  89. public new ContentAlignment ImageAlign
  90. {
  91. get{return _ImageAlign;}
  92. set
  93. {
  94. if (value!=_ImageAlign)
  95. {
  96. _ImageAlign= value;
  97. Invalidate();
  98. }
  99. }
  100. }
  101. public override string Text
  102. {
  103. get{return base.Text;}
  104. set
  105. {
  106. if (value!=base.Text)
  107. {
  108. base.Text= value;
  109. Invalidate();
  110. base.OnTextChanged(new EventArgs());
  111. }
  112. }
  113. }
  114. #endregion
  115. #region "Constructor"
  116. public PushButton()
  117. {
  118. SetStyle(ControlStyles.UserPaint, true);
  119. SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  120. SetStyle(ControlStyles.DoubleBuffer, true);
  121. strFormat= new StringFormat(StringFormatFlags.NoWrap);
  122. strFormat.Alignment=StringAlignment.Center;
  123. }
  124. #endregion
  125. #region "Overrides"
  126. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  127. {
  128. PaintBackground(e.Graphics,ClientRectangle);
  129. if (Image!=null)
  130. {
  131. DrawImage(e.Graphics,GetImageRectangle(e.Graphics));
  132. }
  133. if (Text!=string.Empty)
  134. {
  135. DrawText(e.Graphics,GetTextRect(e.Graphics));
  136. }
  137. }
  138. protected override void OnResize(System.EventArgs e)
  139. {
  140. base.OnResize(e);
  141. Invalidate();
  142. }
  143. protected override void OnMouseEnter(EventArgs e)
  144. {
  145. base.OnMouseEnter(e);
  146. State= Enumerations.ButtonState.Hot;
  147. }
  148. protected override void OnMouseLeave(EventArgs e)
  149. {
  150. base.OnMouseLeave(e);
  151. State= Enumerations.ButtonState.Normal;
  152. }
  153. protected override void OnMouseDown(MouseEventArgs e)
  154. {
  155. base.OnMouseDown(e);
  156. Focus();
  157. State= Enumerations.ButtonState.Pushed;
  158. }
  159. protected override void OnMouseUp(MouseEventArgs e)
  160. {
  161. State= Enumerations.ButtonState.Hot;
  162. base.OnMouseUp(e);
  163. }
  164. protected override void OnGotFocus(EventArgs e)
  165. {
  166. base.OnGotFocus(e);
  167. Invalidate();
  168. }
  169. protected override void OnLostFocus(EventArgs e)
  170. {
  171. base.OnLostFocus(e);
  172. State= Enumerations.ButtonState.Normal;
  173. Invalidate();
  174. }
  175. protected override void OnKeyDown(KeyEventArgs e)
  176. {
  177. base.OnKeyDown (e);
  178. if(e.KeyData==Keys.Space)
  179. {
  180. State=Enumerations.ButtonState.Pushed;
  181. }
  182. else if (e.KeyData== Keys.Enter)
  183. {
  184. base.OnClick(new EventArgs());
  185. }
  186. }
  187. protected override void OnKeyUp(KeyEventArgs e)
  188. {
  189. if(e.KeyData==Keys.Space)
  190. {
  191. if (RectangleToScreen(DisplayRectangle).Contains(Cursor.Position))
  192. {State=Enumerations.ButtonState.Hot; }
  193. else
  194. {State=Enumerations.ButtonState.Normal;}
  195. base.OnClick(new EventArgs());
  196. }
  197. else{base.OnKeyUp(e);}
  198. }
  199. #endregion
  200. #region "Implementation"
  201. protected virtual void PaintBackground(Graphics g, Rectangle bounds)
  202. {
  203. Color BackColor=(this.Parent!=null)?Parent.BackColor:CustomControls.BaseClasses.AppColors.ControlColor;
  204. Color BorderColor=CustomControls.BaseClasses.AppColors.HighlightColor;
  205. if(Enabled)
  206. {
  207. switch (State)
  208. {
  209. case Enumerations.ButtonState.Normal:
  210. {
  211. if (Focused)
  212. {
  213. BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
  214. }
  215. break;
  216. }
  217. case Enumerations.ButtonState.Hot:
  218. {
  219. BackColor=CustomControls.BaseClasses.AppColors.HighlightColor;
  220. BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
  221. break;
  222. }
  223. case Enumerations.ButtonState.Pushed:
  224. {
  225. BackColor=CustomControls.BaseClasses.AppColors.HighlightColorDark;
  226. BorderColor=CustomControls.BaseClasses.AppColors.HighlightColorDarkDark;
  227. break;
  228. }
  229. }
  230. }
  231. else
  232. {
  233. BackColor=CustomControls.BaseClasses.AppColors.ControlColor;
  234. BorderColor=CustomControls.BaseClasses.AppColors.ToolbarBackColor;
  235. }
  236. using( SolidBrush BackBrush= new SolidBrush(Color.White))
  237. {
  238. if(State==Enumerations.ButtonState.Hot || State==Enumerations.ButtonState.Pushed ){g.FillRectangle(BackBrush,bounds);}
  239. BackBrush.Color=BackColor ;
  240. g.FillRectangle(BackBrush,bounds);
  241. }
  242. using(Pen BorderPen = new Pen(BorderColor))
  243. {
  244. g.DrawRectangle(BorderPen,new Rectangle(bounds.X,bounds.Y,bounds.Width-1,bounds.Height-1));
  245. }
  246. }
  247. protected virtual void DrawText(Graphics g,Rectangle bounds)
  248. {
  249. using (SolidBrush brush= new SolidBrush(SystemColors.ControlText))
  250. {
  251. StringFormat StrFormat= new StringFormat(StringFormatFlags.NoWrap);
  252. StrFormat.Alignment=StringAlignment.Center;
  253. if (Enabled)
  254. {
  255. switch (State)
  256. {
  257. case Enumerations.ButtonState.Normal:
  258. case Enumerations.ButtonState.Hot:
  259. {
  260. g.DrawString(Text,Font,brush,bounds,StrFormat );
  261. break;
  262. }
  263. case Enumerations.ButtonState.Pushed:
  264. {
  265. brush.Color=SystemColors.HighlightText;
  266. g.DrawString(Text,Font,brush,bounds,StrFormat );
  267. break;
  268. }
  269. }
  270. }
  271. else
  272. {
  273. brush.Color=SystemColors.GrayText;
  274. g.DrawString(Text,Font,brush,bounds,StrFormat);
  275. }
  276. }
  277. }
  278. protected virtual void DrawImage(Graphics g,Rectangle bounds )
  279. {
  280. if (Enabled)
  281. {
  282. switch (State)
  283. {
  284. case Enumerations.ButtonState.Normal:
  285. case Enumerations.ButtonState.Pushed:
  286. {
  287. g.DrawImage(Image,bounds);
  288. break;
  289. }
  290. case Enumerations.ButtonState.Hot:
  291. {
  292. DrawImageDisabled(g,new Rectangle(bounds.X+1,bounds.Y+1,bounds.Width,bounds.Height),Image);
  293. g.DrawImage(Image,new Rectangle(bounds.X-1,bounds.Y-1,bounds.Width,bounds.Height));
  294. break;
  295. }
  296. }
  297. }
  298. else
  299. {
  300. DrawImageDisabled(g,bounds,Image);
  301. }
  302. }
  303. protected void DrawImageDisabled(Graphics g,Rectangle bounds, Image image)
  304. {
  305. Bitmap DisabledImage = new Bitmap((Bitmap) image);
  306. Color shadowColor = SystemColors.ControlDark;
  307. Color transparent1 = Color.FromArgb(0, 0, 0, 0);
  308. for(int pixelX = 0; pixelX < image.Width; pixelX++)
  309. {
  310. for(int pixelY = 0; pixelY < image.Height; pixelY++)
  311. {
  312. Color pixel=DisabledImage.GetPixel(pixelX, pixelY);
  313. if ( pixel!= transparent1 )
  314. DisabledImage.SetPixel(pixelX, pixelY, shadowColor);
  315. }
  316. }
  317. g.DrawImage(DisabledImage, bounds);
  318. }
  319. private Rectangle GetTextRect(Graphics g)
  320. {
  321. SizeF textSize=g.MeasureString(Text,Font,Width,strFormat);
  322. return PositionRect(new Rectangle(3,3,Width-6,Height-6),new Rectangle(0,0,(int)textSize.Width+3,(int)textSize.Height+3),TextAlign);
  323. }
  324. private Rectangle GetImageRectangle(Graphics g)
  325. {
  326. return PositionRect(new Rectangle(3,3,Width-6,Height-6),new Rectangle(0,0,Image.Width,Image.Height),ImageAlign);
  327. }
  328. private Rectangle PositionRect(Rectangle parentRect, Rectangle childRect, ContentAlignment position)
  329. {
  330. int cWidth= Math.Min(parentRect.Width,childRect.Width);
  331. int cHeight=Math.Min(parentRect.Height,childRect.Height);
  332. switch(position)
  333. {
  334. case ContentAlignment.TopLeft:
  335. {
  336. return new Rectangle(parentRect.X,parentRect.Y,cWidth,cHeight);
  337. }
  338. case ContentAlignment.MiddleLeft:
  339. {
  340. return new Rectangle(parentRect.X,parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
  341. }
  342. case ContentAlignment.BottomLeft:
  343. {
  344. return new Rectangle(parentRect.X,parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
  345. }
  346. case ContentAlignment.TopCenter:
  347. {
  348. return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y,cWidth,cHeight);
  349. }
  350. case ContentAlignment.MiddleCenter:
  351. {
  352. return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
  353. }
  354. case ContentAlignment.BottomCenter:
  355. {
  356. return new Rectangle(parentRect.X + CenterSegment(parentRect.Width,cWidth),parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
  357. }
  358. case ContentAlignment.TopRight:
  359. {
  360. return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y,cWidth,cHeight);
  361. }
  362. case ContentAlignment.MiddleRight:
  363. {
  364. return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y+ CenterSegment(parentRect.Height,cHeight),cWidth,cHeight);
  365. }
  366. case ContentAlignment.BottomRight:
  367. {
  368. return new Rectangle(parentRect.X +parentRect.Width-cWidth,parentRect.Y+parentRect.Height-cHeight,cWidth,cHeight);
  369. }
  370. default:{return parentRect;}
  371. }
  372. }
  373. private int CenterSegment(int parentSegment, int childSegment)
  374. {
  375. return Math.Max(0,(parentSegment-childSegment)/2);
  376. }
  377. #endregion
  378. }
  379. }