ButtonExpand.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Drawing2D;
  8. using System.ComponentModel;
  9. namespace LYFZ.ComponentLibrary
  10. {
  11. [ToolboxBitmap(typeof(Button))]
  12. public class ButtonExpand :System.Windows.Forms.Button // Control
  13. {
  14. /// <summary>
  15. /// 枚举按钮状态
  16. /// </summary>
  17. public enum State
  18. {
  19. /// <summary>
  20. /// 按钮默认时1
  21. /// </summary>
  22. Normal = 1,//
  23. /// <summary>
  24. /// 鼠标移上按钮时3
  25. /// </summary>
  26. MouseOver = 2,//
  27. /// <summary>
  28. /// 鼠标按下按钮时2
  29. /// </summary>
  30. MouseDown = 3,//
  31. /// <summary>
  32. /// 控件得到Tab焦点时
  33. /// </summary>
  34. Default = 4, //
  35. /// <summary>
  36. /// 当不启用按钮时5
  37. /// </summary>
  38. Disable =5 //(也就是按钮属性Enabled==Ture时)
  39. }
  40. #region//私有变量
  41. private State state = State.Normal;
  42. private SolidBrush brush;
  43. private Bitmap _BackImg = new Bitmap(LYFZ.ComponentLibrary.Properties.Resources.btn_bg);
  44. /// <summary>
  45. ///
  46. /// </summary>
  47. [DescriptionAttribute("设置画到组件上背景图片"), CategoryAttribute("组件扩展属性")]
  48. public Bitmap BackImg
  49. {
  50. get { return _BackImg; }
  51. set { _BackImg = value; Invalidate(false); }
  52. }
  53. private Rectangle _BacklightLTRB=new Rectangle(10,10,27,22);
  54. /// <summary>
  55. /// 设置画到组件上背景图片位置
  56. /// </summary>
  57. [DescriptionAttribute("设置画到组件上背景图片位置"), CategoryAttribute("组件扩展属性"), TypeConverter(typeof(Rectangle))]
  58. public Rectangle BacklightLTRB
  59. {
  60. get { return _BacklightLTRB; }
  61. set { _BacklightLTRB = value; Invalidate(false); }
  62. }
  63. bool _isShowText = false;
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. [DescriptionAttribute("是否显示文字"), CategoryAttribute("组件扩展属性")]
  68. public bool IsShowText
  69. {
  70. get { return _isShowText; }
  71. set {
  72. _isShowText = value;
  73. if (_isShowText) {
  74. if (this.Width == 27&&this.Height==22)
  75. {
  76. this.Size = new Size(75,22);
  77. }
  78. this.BackImg = LYFZ.ComponentLibrary.Properties.Resources.btn_bg4;
  79. _BacklightLTRB = new Rectangle(10, 10, 26, 21);
  80. }
  81. Invalidate(false); }
  82. }
  83. #endregion
  84. public ButtonExpand()
  85. // : base()
  86. {
  87. try
  88. {
  89. this.SetStyle(ControlStyles.DoubleBuffer, true);
  90. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  91. this.SetStyle(ControlStyles.UserPaint, true);
  92. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  93. this.SetStyle(ControlStyles.StandardDoubleClick, false);
  94. this.SetStyle(ControlStyles.Selectable, true);
  95. ResizeRedraw = true;
  96. BackColor = Color.Transparent;
  97. ForeColor = Color.White;//初始文本颜色
  98. Size = new Size(27, 22);//初始大小
  99. Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
  100. }
  101. catch { }
  102. }
  103. /// <summary>
  104. /// 按下
  105. /// </summary>
  106. /// <param name="e"></param>
  107. protected override void OnMouseDown(MouseEventArgs e)
  108. {
  109. base.OnMouseDown(e);
  110. this.state = State.MouseDown;
  111. Invalidate(false);
  112. }
  113. protected override void OnGotFocus(EventArgs e)
  114. {
  115. base.OnGotFocus(e);
  116. this.state = State.MouseOver;
  117. Invalidate(false);
  118. }
  119. /// <summary>
  120. /// 失去焦点后
  121. /// </summary>
  122. /// <param name="e"></param>
  123. protected override void OnLostFocus(EventArgs e)
  124. {
  125. base.OnLostFocus(e);
  126. this.state = State.Normal;
  127. Invalidate(false);
  128. }
  129. /// <summary>
  130. /// 鼠标弹起后
  131. /// </summary>
  132. /// <param name="mevent"></param>
  133. protected override void OnMouseUp(MouseEventArgs mevent)
  134. {
  135. base.OnMouseUp(mevent);
  136. this.state = State.MouseOver;
  137. Invalidate(false);
  138. }
  139. /// <summary>
  140. /// 鼠标移上控件
  141. /// </summary>
  142. /// <param name="e"></param>
  143. protected override void OnMouseEnter(EventArgs e)
  144. {
  145. base.OnMouseEnter(e);
  146. state = State.MouseOver;
  147. Invalidate(false);
  148. }
  149. /// <summary>
  150. /// 鼠标离开控件
  151. /// </summary>
  152. /// <param name="e"></param>
  153. protected override void OnMouseLeave(EventArgs e)
  154. {
  155. base.OnMouseLeave(e);
  156. state = State.Normal;
  157. Invalidate(false);
  158. }
  159. /// <summary>
  160. /// 重绘控件
  161. /// </summary>
  162. /// <param name="e"></param>
  163. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  164. {
  165. if (BackImg == null)
  166. {
  167. base.OnPaint(e);
  168. return;
  169. }
  170. int i = (int)state;
  171. //if (this.Focused && state != State.MouseDown ) i = 4;
  172. if (!this.Enabled) i = 5;
  173. Rectangle rc = this.ClientRectangle;
  174. Graphics g = e.Graphics;
  175. base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle));
  176. try
  177. {
  178. if (BackImg != null)
  179. {
  180. ImageDrawRect.DrawRect(g, BackImg, rc, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), i, 5);
  181. }
  182. }
  183. catch
  184. { }
  185. Image img = null;
  186. Size txts, imgs;
  187. txts = Size.Empty;
  188. imgs = Size.Empty;
  189. if (this.Image != null)
  190. {
  191. img = this.Image;
  192. }
  193. else if (this.ImageList != null && this.ImageIndex != -1)
  194. {
  195. img = this.ImageList.Images[this.ImageIndex];
  196. }
  197. if (img != null)
  198. {
  199. imgs.Width = img.Width;
  200. imgs.Height = img.Height;
  201. }
  202. StringFormat format1;
  203. using (format1 = new StringFormat())
  204. {
  205. format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
  206. SizeF ef1 = g.MeasureString(this.Text, this.Font, new SizeF((float)rc.Width, (float)rc.Height), format1);
  207. txts = Size.Ceiling(ef1);
  208. }
  209. rc.Inflate(-4, -4);
  210. if (imgs.Width * imgs.Height != 0)
  211. {
  212. Rectangle imgr = rc;
  213. imgr = ImageDrawRect.HAlignWithin(imgs, imgr, this.ImageAlign);
  214. imgr = ImageDrawRect.VAlignWithin(imgs, imgr, this.ImageAlign);
  215. if (!this.Enabled)
  216. {
  217. ControlPaint.DrawImageDisabled(g, img, imgr.Left, imgr.Top, this.BackColor);
  218. }
  219. else
  220. {
  221. g.DrawImage(img, imgr.Left, imgr.Top, img.Width, img.Height);
  222. }
  223. }
  224. Rectangle txtr = rc;
  225. txtr = ImageDrawRect.HAlignWithin(txts, txtr, this.TextAlign);
  226. txtr = ImageDrawRect.VAlignWithin(txts, txtr, this.TextAlign);
  227. txtr.Y += 5;
  228. format1 = new StringFormat();
  229. format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
  230. if (this.RightToLeft == RightToLeft.Yes)
  231. {
  232. format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
  233. }
  234. brush = new SolidBrush(this.ForeColor);
  235. if (IsShowText)
  236. g.DrawString(this.Text, this.Font, brush, (RectangleF)txtr, format1);
  237. brush.Dispose();
  238. }
  239. }
  240. }