GlassButton.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /// <summary>
  12. /// 水晶按钮
  13. /// </summary>
  14. public class GlassButton : Control
  15. {
  16. #region 控件状态
  17. /// <summary>
  18. /// 控件状态
  19. /// </summary>
  20. public enum State
  21. {
  22. /// <summary>
  23. /// 无0
  24. /// </summary>
  25. Normal = 0,
  26. /// <summary>
  27. /// 获得焦点1
  28. /// </summary>
  29. Focused = 1,
  30. /// <summary>
  31. /// 失去焦点2
  32. /// </summary>
  33. LostFocused = 2,
  34. /// <summary>
  35. /// 鼠标指针进入控件3
  36. /// </summary>
  37. MouseEnter = 3,
  38. /// <summary>
  39. /// 鼠标按下按钮时4
  40. /// </summary>
  41. MouseDown = 4 //
  42. }
  43. #endregion
  44. #region//私有变量
  45. private int bmp_Left;
  46. private int bmp_Top = 6;
  47. private int bmp_Size = 48;
  48. // private bool _focused = false;
  49. private State state = State.Normal;
  50. private Bitmap _BackImg = new Bitmap(LYFZ.ComponentLibrary.Properties.Resources.toolbar_hover);
  51. /// <summary>
  52. /// 设置背景图片
  53. /// </summary>
  54. public Bitmap BackImg
  55. {
  56. get { return _BackImg; }
  57. set { _BackImg = value; }
  58. }
  59. private Bitmap _bmp = LYFZ.ComponentLibrary.Properties.Resources._062;
  60. #endregion
  61. #region//构造函数
  62. public GlassButton()
  63. {
  64. try
  65. {
  66. this.SetStyle(ControlStyles.DoubleBuffer, true);
  67. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  68. this.SetStyle(ControlStyles.UserPaint, true);
  69. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  70. this.SetStyle(ControlStyles.StandardDoubleClick, false);
  71. this.SetStyle(ControlStyles.Selectable, true);
  72. ResizeRedraw = true;
  73. BackColor = Color.Transparent;
  74. ForeColor = Color.White;//初始文本颜色
  75. Size = new Size(80, 75);//初始大小
  76. Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
  77. }
  78. catch { }
  79. }
  80. #endregion
  81. #region//属性
  82. private bool _thisButton = false;
  83. /// <summary>
  84. /// 当前铵钮
  85. /// </summary>
  86. public bool ThisButton
  87. {
  88. get { return _thisButton; }
  89. set { _thisButton = value; Invalidate(false); }
  90. }
  91. /* /// <summary>
  92. /// 图片大小
  93. /// </summary>
  94. [CategoryAttribute("组件扩展属性"), Description("获取或设置控件显示的图标的大小")]
  95. public int Bmp_Size
  96. {
  97. get { return bmp_Size; }
  98. set { bmp_Size = value; }
  99. }*/
  100. /// <summary>
  101. /// 获取或设置控件显示的图片
  102. /// </summary>
  103. [CategoryAttribute("组件扩展属性"), Description("获取或设置控件显示的图标")]
  104. public Bitmap Bitmap
  105. {
  106. get { return _bmp; }
  107. set
  108. {
  109. _bmp = value;
  110. Invalidate(false);
  111. }
  112. }
  113. /// <summary>
  114. /// 重写Text属性
  115. /// </summary>
  116. [DescriptionAttribute("重写Text属性"), CategoryAttribute("组件扩展属性")]
  117. public override string Text
  118. {
  119. set { base.Text = value; Invalidate(false); }
  120. get
  121. {
  122. return base.Text;
  123. }
  124. }
  125. #endregion
  126. #region//重载事件
  127. //自定义绘制
  128. protected override void OnPaint(PaintEventArgs e)
  129. {
  130. base.OnPaint(e);
  131. Graphics g = e.Graphics;
  132. g.SmoothingMode = SmoothingMode.HighQuality;
  133. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  134. DrawImage(g);
  135. switch (state)
  136. {
  137. case State.MouseEnter:
  138. case State.Focused:
  139. {
  140. DrawIsSelected(g,2);
  141. break;
  142. }
  143. case State.MouseDown:
  144. DrawIsSelected(g, 1);
  145. break;
  146. case State.Normal:
  147. case State.LostFocused:
  148. if (ThisButton)
  149. {
  150. DrawIsSelected(g,2);
  151. }
  152. break;
  153. }
  154. DrawText(g);
  155. }
  156. bool isProhibitedResize = true;
  157. /// <summary>
  158. /// 获取或设置控件显示的图片
  159. /// </summary>
  160. [CategoryAttribute("组件扩展属性"), Description("禁止调整大小")]
  161. public bool IsProhibitedResize
  162. {
  163. get { return isProhibitedResize; }
  164. set
  165. {
  166. isProhibitedResize = value;
  167. Invalidate(false);
  168. }
  169. }
  170. /// <summary>
  171. /// 禁止调整大小
  172. /// </summary>
  173. /// <param name="e"></param>
  174. protected override void OnResize(EventArgs e)
  175. {
  176. if (IsProhibitedResize)
  177. {
  178. Width = 80;
  179. Height = 75;
  180. }
  181. else {
  182. if (Width > Height)
  183. {
  184. this.bmp_Size = Height-6;
  185. }
  186. else {
  187. this.bmp_Size = Width-6;
  188. }
  189. }
  190. }
  191. /// <summary>
  192. /// 获取焦点后
  193. /// </summary>
  194. /// <param name="e"></param>
  195. protected override void OnGotFocus(EventArgs e)
  196. {
  197. base.OnGotFocus(e);
  198. this.state = State.Focused;
  199. Invalidate(false);
  200. }
  201. /// <summary>
  202. /// 失去焦点后
  203. /// </summary>
  204. /// <param name="e"></param>
  205. protected override void OnLostFocus(EventArgs e)
  206. {
  207. base.OnLostFocus(e);
  208. this.state = State.LostFocused;
  209. Invalidate(false);
  210. }
  211. /// <summary>
  212. /// 鼠标进入控件可见部份
  213. /// </summary>
  214. /// <param name="e"></param>
  215. protected override void OnMouseEnter(EventArgs e)
  216. {
  217. base.OnMouseEnter(e);
  218. state = State.MouseEnter;
  219. Invalidate(false);
  220. }
  221. /// <summary>
  222. /// 鼠标离开控件可见部份
  223. /// </summary>
  224. /// <param name="e"></param>
  225. protected override void OnMouseLeave(EventArgs e)
  226. {
  227. base.OnMouseLeave(e);
  228. state = State.Normal;
  229. Invalidate(false);
  230. }
  231. /// <summary>
  232. /// 鼠标弹起后
  233. /// </summary>
  234. /// <param name="mevent"></param>
  235. protected override void OnMouseUp(MouseEventArgs mevent)
  236. {
  237. base.OnMouseUp(mevent);
  238. this.state = State.MouseEnter;
  239. Invalidate(false);
  240. }
  241. /// <summary>
  242. /// 按下
  243. /// </summary>
  244. /// <param name="e"></param>
  245. protected override void OnMouseDown(MouseEventArgs e)
  246. {
  247. base.OnMouseDown(e);
  248. this.state = State.MouseDown;
  249. Invalidate(false);
  250. }
  251. #endregion
  252. #region//方法
  253. #region//Draw
  254. void DrawIsSelected(Graphics g,int index)
  255. {
  256. ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, index, 2);
  257. }
  258. void DrawImage(Graphics g)
  259. {
  260. if (_bmp != null)
  261. {
  262. Bitmap bmp = ScaleZoom(_bmp);
  263. if (!this.Enabled) {
  264. bmp =LYFZ.WinAPI.CustomPublicMethod.GetGrayImage(bmp);
  265. }
  266. bmp_Left = (Width - bmp.Width) / 2;
  267. bmp_Top = (Height - bmp.Height) / 2-6;
  268. if (bmp_Top <= 6) {
  269. bmp_Top = 6;
  270. }
  271. g.DrawImage(bmp, new Rectangle(bmp_Left, bmp_Top, bmp.Width, bmp.Height));
  272. }
  273. }
  274. void DrawText(Graphics g)
  275. {
  276. SizeF size = g.MeasureString(this.Text, Font);
  277. Color fColor = ForeColor;
  278. if (!this.Enabled)
  279. fColor = Color.Gray;
  280. int txtTop = this.Height - 20;
  281. txtTop = (Height -Convert.ToInt32(size.Height)) / 2 + 25;
  282. if (txtTop > Height - 20) { txtTop = this.Height - 20; }
  283. g.DrawString(this.Text, Font, new SolidBrush(fColor), (this.Width - size.Width) / 2, txtTop);
  284. }
  285. #endregion
  286. #region//按比例缩放图片
  287. public Bitmap ScaleZoom(Bitmap bmp)
  288. {
  289. if (bmp != null)
  290. {
  291. double zoomScale;
  292. if (bmp.Width > bmp_Size || bmp.Height > bmp_Size)
  293. {
  294. double imageScale = (double)bmp.Width / (double)bmp.Height;
  295. double thisScale = 1;
  296. if (imageScale > thisScale)
  297. {
  298. zoomScale = (double)bmp_Size / (double)bmp.Width;
  299. return BitMapZoom(bmp, bmp_Size, (int)(bmp.Height * zoomScale));
  300. }
  301. else
  302. {
  303. zoomScale = (double)bmp_Size / (double)bmp.Height;
  304. return BitMapZoom(bmp, (int)(bmp.Width * zoomScale), bmp_Size);
  305. }
  306. }
  307. }
  308. return bmp;
  309. }
  310. #endregion
  311. #region//缩放BitMap
  312. /// <summary>
  313. /// 图片缩放
  314. /// </summary>
  315. /// <param name="bmpSource">源图片</param>
  316. /// <param name="bmpSize">缩放图片的大小</param>
  317. /// <returns>缩放的图片</returns>
  318. public Bitmap BitMapZoom(Bitmap bmpSource, int bmpWidth, int bmpHeight)
  319. {
  320. Bitmap bmp, zoomBmp;
  321. try
  322. {
  323. bmp = new Bitmap(bmpSource);
  324. zoomBmp = new Bitmap(bmpWidth, bmpHeight);
  325. Graphics gh = Graphics.FromImage(zoomBmp);
  326. gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
  327. gh.DrawImage(bmp, new Rectangle(0, 0, bmpWidth, bmpHeight), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  328. gh.Dispose();
  329. return zoomBmp;
  330. }
  331. catch
  332. { }
  333. finally
  334. {
  335. bmp = null;
  336. zoomBmp = null;
  337. GC.Collect();
  338. }
  339. return null;
  340. }
  341. #endregion
  342. #endregion
  343. }
  344. }