TransparentControl.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Diagnostics;
  5. using System.Text;
  6. using System.Drawing;
  7. using System.Windows.Forms;
  8. namespace LYFZ.ComponentLibrary
  9. {
  10. /// <summary>
  11. /// 透明按钮
  12. /// </summary>
  13. [ToolboxBitmap(typeof(TransparentControl))]
  14. public class TransparentControl :System.Windows.Forms.Control
  15. {
  16. private bool _transparentBG = true;
  17. private int _alpha = 30;
  18. private System.ComponentModel.Container components = new System.ComponentModel.Container();
  19. public TransparentControl()
  20. : this(30, true)
  21. {
  22. }
  23. public TransparentControl(int Alpha, bool showLoadingImage)
  24. {
  25. try
  26. {
  27. this.SetStyle(ControlStyles.DoubleBuffer, true);
  28. this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
  29. this.SetStyle(ControlStyles.UserPaint, true);
  30. this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
  31. this.SetStyle(ControlStyles.StandardDoubleClick, false);
  32. this.SetStyle(ControlStyles.Selectable, true);
  33. this.SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
  34. ResizeRedraw = true;
  35. BackColor = Color.Transparent;
  36. ForeColor = Color.White;//初始文本颜色
  37. Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
  38. base.CreateControl();
  39. this._alpha = Alpha;
  40. this.thisAlpha = _alpha;
  41. this.Width = 60;
  42. this.Height = 60;
  43. }
  44. catch { }
  45. //此处可以放置一个显示加载进度的图片
  46. //if (showLoadingImage)
  47. //{
  48. // PictureBox pictureBox_Loading = new PictureBox();
  49. // pictureBox_Loading.BackColor = System.Drawing.Color.White;
  50. // pictureBox_Loading.Image = global::MyOpaqueLayer.Properties.Resources.loading;//图片名称叫loading
  51. // pictureBox_Loading.Name = "pictureBox_Loading";
  52. // pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
  53. // pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  54. // Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);
  55. // pictureBox_Loading.Location = Location;
  56. // pictureBox_Loading.Anchor = AnchorStyles.None;
  57. // this.Controls.Add(pictureBox_Loading);
  58. //}
  59. }
  60. protected override void Dispose(bool disposing)
  61. {
  62. if (disposing)
  63. {
  64. if (!((components == null)))
  65. {
  66. components.Dispose();
  67. }
  68. }
  69. base.Dispose(disposing);
  70. }
  71. private int thisAlpha = 0;
  72. /// <summary>
  73. /// 按下
  74. /// </summary>
  75. /// <param name="e"></param>
  76. protected override void OnMouseDown(MouseEventArgs e)
  77. {
  78. base.OnMouseDown(e);
  79. this._alpha = 20;
  80. Invalidate(false);
  81. }
  82. protected override void OnGotFocus(EventArgs e)
  83. {
  84. base.OnGotFocus(e);
  85. this._alpha = 35;
  86. Invalidate(false);
  87. }
  88. /// <summary>
  89. /// 失去焦点后
  90. /// </summary>
  91. /// <param name="e"></param>
  92. protected override void OnLostFocus(EventArgs e)
  93. {
  94. base.OnLostFocus(e);
  95. this._alpha = thisAlpha;
  96. Invalidate(false);
  97. }
  98. /// <summary>
  99. /// 鼠标弹起后
  100. /// </summary>
  101. /// <param name="mevent"></param>
  102. protected override void OnMouseUp(MouseEventArgs mevent)
  103. {
  104. base.OnMouseUp(mevent);
  105. this._alpha = 35;
  106. Invalidate(false);
  107. }
  108. /// <summary>
  109. /// 鼠标移上控件
  110. /// </summary>
  111. /// <param name="e"></param>
  112. protected override void OnMouseEnter(EventArgs e)
  113. {
  114. base.OnMouseEnter(e);
  115. this._alpha = 35;
  116. Invalidate(false);
  117. }
  118. /// <summary>
  119. /// 鼠标离开控件
  120. /// </summary>
  121. /// <param name="e"></param>
  122. protected override void OnMouseLeave(EventArgs e)
  123. {
  124. base.OnMouseLeave(e);
  125. this._alpha = thisAlpha;
  126. Invalidate(false);
  127. }
  128. /// <summary>
  129. /// 自定义绘制窗体
  130. /// </summary>
  131. /// <param name="e"></param>
  132. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  133. {
  134. float vlblControlWidth;
  135. float vlblControlHeight;
  136. base.InvokePaintBackground(this, new PaintEventArgs(e.Graphics, base.ClientRectangle));
  137. Pen labelBorderPen;
  138. SolidBrush labelBackColorBrush;
  139. Color drawColor = Color.FromArgb(this._alpha, this.BackColor);
  140. if (_transparentBG)
  141. {
  142. labelBorderPen = new Pen(drawColor, 0);
  143. labelBackColorBrush = new SolidBrush(drawColor);
  144. }
  145. else
  146. {
  147. labelBorderPen = new Pen(this.BackColor, 0);
  148. labelBackColorBrush = new SolidBrush(this.BackColor);
  149. }
  150. // base.OnPaint(e);
  151. vlblControlWidth = this.Size.Width;
  152. vlblControlHeight = this.Size.Height;
  153. Rectangle rele= new Rectangle(0, 0, this.Size.Width, this.Size.Height);
  154. e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
  155. e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
  156. }
  157. /// <summary>
  158. ///
  159. /// </summary>
  160. protected override CreateParams CreateParams//v1.10
  161. {
  162. get
  163. {
  164. CreateParams cp = base.CreateParams;
  165. cp.ExStyle |= 0x20; // 开启 WS_EX_TRANSPARENT,使控件支持透明
  166. return cp;
  167. }
  168. }
  169. [Category("TransparentControl"), Description("是否使用透明,默认为True")]
  170. public bool TransparentBG
  171. {
  172. get { return _transparentBG; }
  173. set
  174. {
  175. _transparentBG = value;
  176. this.Invalidate();
  177. }
  178. }
  179. [Category("TransparentControl"), Description("设置透明度")]
  180. public int Alpha
  181. {
  182. get { return _alpha; }
  183. set
  184. {
  185. if (value <= 255 && value>=0)
  186. {
  187. _alpha = value;
  188. thisAlpha = _alpha;
  189. }
  190. this.Invalidate();
  191. }
  192. }
  193. }
  194. }