当前位置:   article > 正文

C#自绘蒙版控件,带延时隐藏显示,拷贝底图功能

c# 蒙版
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Drawing.Drawing2D;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. namespace MyUserControl
  11. {
  12. [ToolboxItem(false), Browsable(false), Description("用于屏蔽用户界面的控件;")]
  13. public partial class MaskLayer : Control
  14. {
  15. private int alpha;
  16. private Boolean bShow = true;
  17. Timer timer = new Timer();
  18. public MaskLayer()
  19. {
  20. timer.Interval = 100;
  21. timer.Tick += Timer1_Tick;
  22. timer.Start();
  23. SetStyle(ControlStyles.UserPaint, true);
  24. SetStyle(ControlStyles.AllPaintingInWmPaint, true); // 禁止擦除背景.
  25. SetStyle(ControlStyles.DoubleBuffer, true); // 双缓冲
  26. alpha = 75;
  27. //SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
  28. }
  29. private void Timer1_Tick(object sender, EventArgs e)
  30. {
  31. if (bShow)
  32. ShowMask(bShow);
  33. else
  34. {
  35. ShowMask(bShow);
  36. BackgroundImage = null;
  37. }
  38. }
  39. private delegate void ShowMaskCallback(Boolean bShow);
  40. private void ShowMask(Boolean bShow)
  41. {
  42. // InvokeRequired需要比较调用线程ID和创建线程ID
  43. // 如果它们不相同则返回true
  44. if (this.InvokeRequired)
  45. {
  46. ShowMaskCallback d = new ShowMaskCallback(ShowMask);
  47. this.Invoke(d, new object[] { bShow });
  48. }
  49. else
  50. {
  51. if (bShow)
  52. this.Show();
  53. else
  54. this.Hide();
  55. }
  56. }
  57. //显示线程
  58. public void DelayShowMaskByScreenCopy(Control parentControl)
  59. {
  60. this.BackColor = Color.Black;
  61. this.Left = 0;
  62. this.Top = 0;
  63. this.Width = parentControl.Width;
  64. this.Height = parentControl.Height;
  65. this.Parent = parentControl;
  66. if (this.BackgroundImage == null)
  67. {
  68. Rectangle rect = parentControl.ClientRectangle;
  69. Rectangle sRect = parentControl.RectangleToScreen(rect);
  70. Bitmap bit = new Bitmap(rect.Width, rect.Height);//实例化一个和窗体一样大的bitmap
  71. Graphics g = Graphics.FromImage(bit);
  72. g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
  73. g.CopyFromScreen(sRect.Left, sRect.Top, 0, 0, new Size(this.Width, this.Height));
  74. this.BackgroundImage = bit;
  75. }
  76. bShow = true;
  77. }
  78. public void DelayShowMaskByColorFill(Color color,Control parentControl)
  79. {
  80. this.BackColor = Color.Black;
  81. this.Left = 0;
  82. this.Top = 0;
  83. this.Width = parentControl.Width;
  84. this.Height = parentControl.Height;
  85. this.Parent = parentControl;
  86. SetStyle(ControlStyles.AllPaintingInWmPaint, false); // 擦除背景.有轻微闪烁现象
  87. if (this.BackgroundImage == null)
  88. {
  89. Rectangle rect = parentControl.ClientRectangle;
  90. Rectangle sRect = parentControl.RectangleToScreen(rect);
  91. Bitmap bit = new Bitmap(rect.Width, rect.Height);//实例化一个和窗体一样大的bitmap
  92. Graphics g = Graphics.FromImage(bit);
  93. g.CompositingQuality = CompositingQuality.HighQuality;//质量设为最高
  94. g.FillRectangle(new SolidBrush(Color.FromArgb(alpha, color)), this.ClientRectangle);
  95. this.BackgroundImage = bit;
  96. }
  97. bShow = true;
  98. }
  99. public void DelayHide()
  100. {
  101. bShow = false;
  102. }
  103. protected override void OnPaintBackground(PaintEventArgs e)
  104. {
  105. return;
  106. }
  107. protected override void OnPaint(PaintEventArgs e)
  108. {
  109. Bitmap bmp = new Bitmap(this.Width, this.Height);
  110. Graphics g = Graphics.FromImage(bmp);
  111. g.DrawImage(BackgroundImage, 0, 0, this.Width, this.Height);
  112. Color color = Color.FromArgb(alpha, this.BackColor);
  113. using (SolidBrush brush = new SolidBrush(color))
  114. {
  115. g.FillRectangle(brush, 0, 0, this.Size.Width, this.Size.Height);
  116. }
  117. if (!this.DesignMode)
  118. {
  119. using (Pen pen = new Pen(color))
  120. {
  121. g.DrawRectangle(pen, 0, 0, this.Width, this.Height);
  122. }
  123. }
  124. else
  125. g.DrawRectangle(Pens.Black, 1, 1, this.Width - 2, this.Height - 2);
  126. e.Graphics.DrawImage(bmp, 0, 0);
  127. g.Dispose();
  128. bmp.Dispose();
  129. }
  130. protected override CreateParams CreateParams
  131. {
  132. get
  133. {
  134. const int CS_NOCLOSE = 0x200;
  135. CreateParams cp = base.CreateParams;
  136. cp.ExStyle |= 0x00000020;
  137. cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
  138. return cp;
  139. }
  140. }
  141. public int Alpha
  142. {
  143. get
  144. {
  145. return alpha;
  146. }
  147. set
  148. {
  149. if (value < 0) alpha = 0;
  150. else if (value > 255) alpha = 255;
  151. else alpha = value;
  152. this.Invalidate();
  153. }
  154. }
  155. }
  156. }


加延迟是为了防止在两个控件之间切换时,蒙版反复显示,隐藏闪烁。
使用方法:

  1. MaskLayer maskLayer = new MaskLayer();/****//******/private void ShowMask()
  2. {
  3. maskLayer.DelayShowMaskByScreenCopy(panelEx1);//panelEx1是被蒙版的控件。
  4. }
  5. private void HideMask()
  6. {
  7. maskLayer.DelayHide();
  8. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/257707
推荐阅读
相关标签
  

闽ICP备14008679号