ButtonExpand.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. /// <summary>
  44. /// 默认背景
  45. /// </summary>
  46. private Bitmap _DfBackImg = null;
  47. private Bitmap _BackImg = new Bitmap(LYFZ.ComponentLibrary.Properties.Resources.btn_bg);
  48. /// <summary>
  49. ///
  50. /// </summary>
  51. [DescriptionAttribute("设置画到组件上背景图片"), CategoryAttribute("组件扩展属性")]
  52. public Bitmap BackImg
  53. {
  54. get { return _BackImg; }
  55. set { _BackImg = value; Invalidate(false); }
  56. }
  57. private Rectangle _BacklightLTRB=new Rectangle(10,10,27,22);
  58. /// <summary>
  59. /// 设置画到组件上背景图片位置
  60. /// </summary>
  61. [DescriptionAttribute("设置画到组件上背景图片位置"), CategoryAttribute("组件扩展属性"), TypeConverter(typeof(Rectangle))]
  62. public Rectangle BacklightLTRB
  63. {
  64. get { return _BacklightLTRB; }
  65. set { _BacklightLTRB = value; Invalidate(false); }
  66. }
  67. bool _isShowText = false;
  68. /// <summary>
  69. ///
  70. /// </summary>
  71. [DescriptionAttribute("是否显示文字"), CategoryAttribute("组件扩展属性")]
  72. public bool IsShowText
  73. {
  74. get { return _isShowText; }
  75. set {
  76. _isShowText = value;
  77. if (_isShowText) {
  78. if (this.Width == 27&&this.Height==22)
  79. {
  80. this.Size = new Size(75,22);
  81. }
  82. if (this.BackImg == null)
  83. {
  84. this.BackImg = LYFZ.ComponentLibrary.Properties.Resources.btn_bg4;
  85. }
  86. _BacklightLTRB = new Rectangle(10, 10, 26, 21);
  87. }
  88. Invalidate(false); }
  89. }
  90. bool isCustomBackImg = false;
  91. /// <summary>
  92. ///
  93. /// </summary>
  94. [DescriptionAttribute("设置是否可以自定义按钮背景图片"), CategoryAttribute("组件扩展属性")]
  95. public bool IsCustomBackImg
  96. {
  97. get { return isCustomBackImg; }
  98. set { isCustomBackImg = value; Invalidate(false); }
  99. }
  100. #endregion
  101. public ButtonExpand()
  102. // : base()
  103. {
  104. try
  105. {
  106. this.SetStyle(ControlStyles.DoubleBuffer, true);
  107. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  108. this.SetStyle(ControlStyles.UserPaint, true);
  109. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  110. this.SetStyle(ControlStyles.StandardDoubleClick, false);
  111. this.SetStyle(ControlStyles.Selectable, true);
  112. ResizeRedraw = true;
  113. BackColor = Color.Transparent;
  114. ForeColor = Color.White;//初始文本颜色
  115. Size = new Size(27, 22);//初始大小
  116. Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
  117. }
  118. catch { }
  119. }
  120. public ButtonExpand(Bitmap dfBackImg)
  121. : this()
  122. {
  123. this._DfBackImg = dfBackImg;
  124. }
  125. /// <summary>
  126. /// 按下
  127. /// </summary>
  128. /// <param name="e"></param>
  129. protected override void OnMouseDown(MouseEventArgs e)
  130. {
  131. base.OnMouseDown(e);
  132. this.state = State.MouseDown;
  133. Invalidate(false);
  134. }
  135. protected override void OnGotFocus(EventArgs e)
  136. {
  137. base.OnGotFocus(e);
  138. this.state = State.MouseOver;
  139. Invalidate(false);
  140. }
  141. /// <summary>
  142. /// 失去焦点后
  143. /// </summary>
  144. /// <param name="e"></param>
  145. protected override void OnLostFocus(EventArgs e)
  146. {
  147. base.OnLostFocus(e);
  148. this.state = State.Normal;
  149. Invalidate(false);
  150. }
  151. /// <summary>
  152. /// 鼠标弹起后
  153. /// </summary>
  154. /// <param name="mevent"></param>
  155. protected override void OnMouseUp(MouseEventArgs mevent)
  156. {
  157. base.OnMouseUp(mevent);
  158. this.state = State.MouseOver;
  159. Invalidate(false);
  160. }
  161. /// <summary>
  162. /// 鼠标移上控件
  163. /// </summary>
  164. /// <param name="e"></param>
  165. protected override void OnMouseEnter(EventArgs e)
  166. {
  167. base.OnMouseEnter(e);
  168. state = State.MouseOver;
  169. Invalidate(false);
  170. }
  171. /// <summary>
  172. /// 鼠标离开控件
  173. /// </summary>
  174. /// <param name="e"></param>
  175. protected override void OnMouseLeave(EventArgs e)
  176. {
  177. base.OnMouseLeave(e);
  178. state = State.Normal;
  179. Invalidate(false);
  180. }
  181. /// <summary>
  182. /// 重绘控件
  183. /// </summary>
  184. /// <param name="e"></param>
  185. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  186. {
  187. if (BackImg == null)
  188. {
  189. base.OnPaint(e);
  190. return;
  191. }
  192. int i = (int)state;
  193. //if (this.Focused && state != State.MouseDown ) i = 4;
  194. if (!this.Enabled) i = 5;
  195. Rectangle rc = this.ClientRectangle;
  196. Graphics g = e.Graphics;
  197. base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle));
  198. try
  199. {
  200. if (BackImg != null)
  201. {
  202. if (!isCustomBackImg && this._DfBackImg != null)
  203. {
  204. this._BackImg = this._DfBackImg;
  205. }
  206. else {
  207. if (!isCustomBackImg)
  208. {
  209. switch (this.Name)
  210. {
  211. case "btnAppFormExit":
  212. this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_close;
  213. break;
  214. case "btnAppFormMaximize":
  215. this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_max;
  216. break;
  217. case "btnAppFormMinimize":
  218. this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_mini;
  219. break;
  220. case "btnAppFormMenu":
  221. this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_QuickMenu;
  222. break;
  223. case "btnAppFormSkin":
  224. this._BackImg = LYFZ.ComponentLibrary.GetUIResources.btn_skin;
  225. break;
  226. }
  227. }
  228. }
  229. ImageDrawRect.DrawRect(g, BackImg, rc, Rectangle.FromLTRB(_BacklightLTRB.X, _BacklightLTRB.Y, _BacklightLTRB.Width, _BacklightLTRB.Height), i, 5);
  230. }
  231. }
  232. catch
  233. { }
  234. Image img = null;
  235. Size txts, imgs;
  236. txts = Size.Empty;
  237. imgs = Size.Empty;
  238. if (this.Image != null)
  239. {
  240. img = this.Image;
  241. }
  242. else if (this.ImageList != null && this.ImageIndex != -1)
  243. {
  244. img = this.ImageList.Images[this.ImageIndex];
  245. }
  246. if (img != null)
  247. {
  248. imgs.Width = img.Width;
  249. imgs.Height = img.Height;
  250. }
  251. StringFormat format1;
  252. using (format1 = new StringFormat())
  253. {
  254. format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
  255. SizeF ef1 = g.MeasureString(this.Text, this.Font, new SizeF((float)rc.Width, (float)rc.Height), format1);
  256. txts = Size.Ceiling(ef1);
  257. }
  258. rc.Inflate(-4, -4);
  259. if (imgs.Width * imgs.Height != 0)
  260. {
  261. Rectangle imgr = rc;
  262. imgr = ImageDrawRect.HAlignWithin(imgs, imgr, this.ImageAlign);
  263. imgr = ImageDrawRect.VAlignWithin(imgs, imgr, this.ImageAlign);
  264. if (!this.Enabled)
  265. {
  266. ControlPaint.DrawImageDisabled(g, img, imgr.Left, imgr.Top, this.BackColor);
  267. }
  268. else
  269. {
  270. g.DrawImage(img, imgr.Left, imgr.Top, img.Width, img.Height);
  271. }
  272. }
  273. Rectangle txtr = rc;
  274. txtr = ImageDrawRect.HAlignWithin(txts, txtr, this.TextAlign);
  275. txtr = ImageDrawRect.VAlignWithin(txts, txtr, this.TextAlign);
  276. txtr.Y += 5;
  277. format1 = new StringFormat();
  278. format1.HotkeyPrefix = System.Drawing.Text.HotkeyPrefix.Show;
  279. if (this.RightToLeft == RightToLeft.Yes)
  280. {
  281. format1.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
  282. }
  283. brush = new SolidBrush(this.ForeColor);
  284. if (IsShowText)
  285. g.DrawString(this.Text, this.Font, brush, (RectangleF)txtr, format1);
  286. brush.Dispose();
  287. }
  288. }
  289. }