GlassButton.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. public object DefaultTGA = null;
  126. public string DefaultTEXT = "";
  127. public Bitmap DefaultBitMap;
  128. #endregion
  129. #region//重载事件
  130. //自定义绘制
  131. protected override void OnPaint(PaintEventArgs e)
  132. {
  133. base.OnPaint(e);
  134. Graphics g = e.Graphics;
  135. g.SmoothingMode = SmoothingMode.HighQuality;
  136. g.PixelOffsetMode = PixelOffsetMode.HighQuality;
  137. DrawImage(g);
  138. switch (state)
  139. {
  140. case State.MouseEnter:
  141. case State.Focused:
  142. {
  143. DrawIsSelected(g,2);
  144. break;
  145. }
  146. case State.MouseDown:
  147. DrawIsSelected(g, 1);
  148. break;
  149. case State.Normal:
  150. case State.LostFocused:
  151. if (ThisButton)
  152. {
  153. DrawIsSelected(g,2);
  154. }
  155. break;
  156. }
  157. DrawText(g);
  158. }
  159. bool isProhibitedResize = true;
  160. /// <summary>
  161. /// 获取或设置控件显示的图片
  162. /// </summary>
  163. [CategoryAttribute("组件扩展属性"), Description("禁止调整大小")]
  164. public bool IsProhibitedResize
  165. {
  166. get { return isProhibitedResize; }
  167. set
  168. {
  169. isProhibitedResize = value;
  170. Invalidate(false);
  171. }
  172. }
  173. /// <summary>
  174. /// 禁止调整大小
  175. /// </summary>
  176. /// <param name="e"></param>
  177. protected override void OnResize(EventArgs e)
  178. {
  179. if (IsProhibitedResize)
  180. {
  181. Width = 80;
  182. Height = 75;
  183. }
  184. else {
  185. if (Width > Height)
  186. {
  187. this.bmp_Size = Height-6;
  188. }
  189. else {
  190. this.bmp_Size = Width-6;
  191. }
  192. }
  193. }
  194. /// <summary>
  195. /// 获取焦点后
  196. /// </summary>
  197. /// <param name="e"></param>
  198. protected override void OnGotFocus(EventArgs e)
  199. {
  200. base.OnGotFocus(e);
  201. this.state = State.Focused;
  202. Invalidate(false);
  203. }
  204. /// <summary>
  205. /// 失去焦点后
  206. /// </summary>
  207. /// <param name="e"></param>
  208. protected override void OnLostFocus(EventArgs e)
  209. {
  210. base.OnLostFocus(e);
  211. this.state = State.LostFocused;
  212. Invalidate(false);
  213. }
  214. /// <summary>
  215. /// 鼠标进入控件可见部份
  216. /// </summary>
  217. /// <param name="e"></param>
  218. protected override void OnMouseEnter(EventArgs e)
  219. {
  220. base.OnMouseEnter(e);
  221. state = State.MouseEnter;
  222. Invalidate(false);
  223. }
  224. /// <summary>
  225. /// 鼠标离开控件可见部份
  226. /// </summary>
  227. /// <param name="e"></param>
  228. protected override void OnMouseLeave(EventArgs e)
  229. {
  230. base.OnMouseLeave(e);
  231. state = State.Normal;
  232. Invalidate(false);
  233. }
  234. /// <summary>
  235. /// 鼠标弹起后
  236. /// </summary>
  237. /// <param name="mevent"></param>
  238. protected override void OnMouseUp(MouseEventArgs mevent)
  239. {
  240. base.OnMouseUp(mevent);
  241. this.state = State.MouseEnter;
  242. Invalidate(false);
  243. }
  244. /// <summary>
  245. /// 按下
  246. /// </summary>
  247. /// <param name="e"></param>
  248. protected override void OnMouseDown(MouseEventArgs e)
  249. {
  250. base.OnMouseDown(e);
  251. this.state = State.MouseDown;
  252. Invalidate(false);
  253. }
  254. #endregion
  255. #region//方法
  256. #region//Draw
  257. void DrawIsSelected(Graphics g,int index)
  258. {
  259. ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, index, 2);
  260. }
  261. void DrawImage(Graphics g)
  262. {
  263. if (_bmp != null)
  264. {
  265. if (IsZoomSize48)
  266. {
  267. if (this.bmp_Size >= 50)
  268. {
  269. this.bmp_Size = 48;
  270. }
  271. }
  272. Bitmap bmp = ScaleZoom(_bmp);
  273. if (!this.Enabled) {
  274. bmp =LYFZ.WinAPI.CustomPublicMethod.GetGrayImage(bmp);
  275. }
  276. bmp_Left = (Width - bmp.Width) / 2;
  277. bmp_Top = (Height - bmp.Height) / 2-6;
  278. if (bmp_Top <= 6) {
  279. bmp_Top = 6;
  280. }
  281. g.DrawImage(bmp, new Rectangle(bmp_Left, bmp_Top, bmp.Width, bmp.Height));
  282. // g.DrawImage(bmp, new Rectangle(bmp_Left + 5, bmp_Top, bmp.Width - 10, bmp.Height - 10), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  283. }
  284. }
  285. void DrawText(Graphics g)
  286. {
  287. SizeF size = g.MeasureString(this.Text, Font);
  288. Color fColor = ForeColor;
  289. if (!this.Enabled)
  290. fColor = Color.Gray;
  291. int txtTop = this.Height - 20;
  292. txtTop = (Height -Convert.ToInt32(size.Height)) / 2 + 25+3;
  293. //txtTop = (Height - Convert.ToInt32(size.Height));
  294. if (txtTop > Height - 20) { txtTop = this.Height - 20; }
  295. g.DrawString(this.Text, Font, new SolidBrush(fColor), (this.Width - size.Width) / 2, txtTop);
  296. }
  297. #endregion
  298. public bool IsZoomSize48 = false;
  299. #region//按比例缩放图片
  300. public Bitmap ScaleZoom(Bitmap bmp)
  301. {
  302. if (bmp != null)
  303. {
  304. double zoomScale;
  305. if (bmp.Width > bmp_Size || bmp.Height > bmp_Size)
  306. {
  307. double imageScale = (double)bmp.Width / (double)bmp.Height;
  308. double thisScale = 1;
  309. if (imageScale > thisScale)
  310. {
  311. zoomScale = (double)bmp_Size / (double)bmp.Width;
  312. return BitMapZoom(bmp, bmp_Size, (int)(bmp.Height * zoomScale));
  313. }
  314. else
  315. {
  316. zoomScale = (double)bmp_Size / (double)bmp.Height;
  317. return BitMapZoom(bmp, (int)(bmp.Width * zoomScale), bmp_Size);
  318. }
  319. }
  320. }
  321. return bmp;
  322. }
  323. #endregion
  324. #region//缩放BitMap
  325. /// <summary>
  326. /// 图片缩放
  327. /// </summary>
  328. /// <param name="bmpSource">源图片</param>
  329. /// <param name="bmpSize">缩放图片的大小</param>
  330. /// <returns>缩放的图片</returns>
  331. public Bitmap BitMapZoom(Bitmap bmpSource, int bmpWidth, int bmpHeight)
  332. {
  333. Bitmap bmp, zoomBmp;
  334. try
  335. {
  336. bmp = new Bitmap(bmpSource);
  337. zoomBmp = new Bitmap(bmpWidth, bmpHeight);
  338. Graphics gh = Graphics.FromImage(zoomBmp);
  339. gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
  340. gh.DrawImage(bmp, new Rectangle(0, 0, bmpWidth, bmpHeight), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
  341. gh.Dispose();
  342. return zoomBmp;
  343. }
  344. catch
  345. { }
  346. finally
  347. {
  348. bmp = null;
  349. zoomBmp = null;
  350. GC.Collect();
  351. }
  352. return null;
  353. }
  354. #endregion
  355. #endregion
  356. }
  357. }