OpaqueLayer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.ComponentModel;
  5. namespace WCS_Client.UC
  6. {
  7. /// <summary>
  8. /// 自定义控件:半透明控件
  9. /// </summary>
  10. /*
  11. * [ToolboxBitmap(typeof(MyOpaqueLayer))]
  12. * 用于指定当把你做好的自定义控件添加到工具栏时,工具栏显示的图标。
  13. * 正确写法应该是
  14. * [ToolboxBitmap(typeof(XXXXControl),"xxx.bmp")]
  15. * 其中XXXXControl是你的自定义控件,"xxx.bmp"是你要用的图标名称。
  16. */
  17. [ToolboxBitmap(typeof(OpaqueLayer))]
  18. public class OpaqueLayer : System.Windows.Forms.Control
  19. {
  20. private bool _transparentBG = true;//是否使用透明
  21. private int _alpha = 125;//设置透明度
  22. private System.ComponentModel.Container components = new System.ComponentModel.Container();
  23. public OpaqueLayer()
  24. : this(125, true)
  25. {
  26. }
  27. public OpaqueLayer(int Alpha, bool IsShowLoadingImage)
  28. {
  29. SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
  30. base.CreateControl();
  31. this._alpha = Alpha;
  32. if (IsShowLoadingImage)
  33. {
  34. PictureBox pictureBox_Loading = new PictureBox();
  35. pictureBox_Loading.BackColor = System.Drawing.Color.White;
  36. pictureBox_Loading.Image = Properties.Resources.loading;
  37. pictureBox_Loading.Name = "pictureBox_Loading";
  38. pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
  39. pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  40. Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中
  41. pictureBox_Loading.Location = Location;
  42. pictureBox_Loading.Anchor = AnchorStyles.None;
  43. this.Controls.Add(pictureBox_Loading);
  44. }
  45. }
  46. protected override void Dispose(bool disposing)
  47. {
  48. if (disposing)
  49. {
  50. if (!((components == null)))
  51. {
  52. components.Dispose();
  53. }
  54. }
  55. base.Dispose(disposing);
  56. }
  57. /// <summary>
  58. /// 自定义绘制窗体
  59. /// </summary>
  60. /// <param name="e"></param>
  61. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  62. {
  63. float vlblControlWidth;
  64. float vlblControlHeight;
  65. Pen labelBorderPen;
  66. SolidBrush labelBackColorBrush;
  67. if (_transparentBG)
  68. {
  69. Color drawColor = Color.FromArgb(this._alpha, this.BackColor);
  70. labelBorderPen = new Pen(drawColor, 0);
  71. labelBackColorBrush = new SolidBrush(drawColor);
  72. }
  73. else
  74. {
  75. labelBorderPen = new Pen(this.BackColor, 0);
  76. labelBackColorBrush = new SolidBrush(this.BackColor);
  77. }
  78. base.OnPaint(e);
  79. vlblControlWidth = this.Size.Width;
  80. vlblControlHeight = this.Size.Height;
  81. e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
  82. e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
  83. }
  84. protected override CreateParams CreateParams//v1.10
  85. {
  86. get
  87. {
  88. CreateParams cp = base.CreateParams;
  89. cp.ExStyle |= 0x00000020; //0x20; // 开启 WS_EX_TRANSPARENT,使控件支持透明
  90. return cp;
  91. }
  92. }
  93. /*
  94. * [Category("myOpaqueLayer"), Description("是否使用透明,默认为True")]
  95. * 一般用于说明你自定义控件的属性(Property)。
  96. * Category用于说明该属性属于哪个分类,Description自然就是该属性的含义解释。
  97. */
  98. [Category("MyOpaqueLayer"), Description("是否使用透明,默认为True")]
  99. public bool TransparentBG
  100. {
  101. get
  102. {
  103. return _transparentBG;
  104. }
  105. set
  106. {
  107. _transparentBG = value;
  108. this.Invalidate();
  109. }
  110. }
  111. [Category("MyOpaqueLayer"), Description("设置透明度")]
  112. public int Alpha
  113. {
  114. get
  115. {
  116. return _alpha;
  117. }
  118. set
  119. {
  120. _alpha = value;
  121. this.Invalidate();
  122. }
  123. }
  124. }
  125. }