1,需要一个动态的londing文件;在项目中我们新建一个文件夹来存放它;

 

2,在需要出现londing状态的窗体上加上一个Panel;

黄色区域是Panel,灰色的是需要被加载的区域。当需要触发londing,我们就把Panel显示出来,不让用户在这个区域能做任何修改;

下面的两个button事件就是触发londing的;在这里实例化的 OpaqueCommand对象;我们需要创建一个OpaqueCommand类

  1. OpaqueCommand cmd = new OpaqueCommand();
  2. /// <summary>
  3. /// 启动加载功能
  4. /// </summary>
  5. /// <param name="sender"></param>
  6. /// <param name="e"></param>
  7. private void show_Click(object sender, EventArgs e)
  8. {
  9. cmd.ShowOpaqueLayer(panelLonding, 125, true);
  10. }
  11. /// <summary>
  12. /// 关闭加载功能
  13. /// </summary>
  14. /// <param name="sender"></param>
  15. /// <param name="e"></param>
  16. private void hide_Click(object sender, EventArgs e)
  17. {
  18. cmd.HideOpaqueLayer();
  19. }

3,OpaqueCommand类里面的内容

  1. public class OpaqueCommand
  2. {
  3. private OpaqueLayer m_OpaqueLayer = null;
  4.  
  5. public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage)
  6. {
  7. try
  8. {
  9. if (this.m_OpaqueLayer == null)
  10. {
  11. this.m_OpaqueLayer = new OpaqueLayer(alpha, isShowLoadingImage);
  12. control.Controls.Add(this.m_OpaqueLayer);
  13. this.m_OpaqueLayer.Dock = DockStyle.Fill;
  14. this.m_OpaqueLayer.BringToFront();
  15. }
  16. this.m_OpaqueLayer.Enabled = true;
  17. this.m_OpaqueLayer.Visible = true;
  18. }
  19. catch { }
  20. }
  21.  
  22. public void HideOpaqueLayer()
  23. {
  24. try
  25. {
  26. if (this.m_OpaqueLayer != null)
  27. {
  28. this.m_OpaqueLayer.Visible = false;
  29. this.m_OpaqueLayer.Enabled = false;
  30. }
  31. }
  32. catch (Exception ex)
  33. {
  34. //MessageBox.Show(ex.Message);
  35. }
  36. }
  37. }

 4,创建 OpaqueLayer 类,这里面是重新绘制了窗体,内容如下

  1. [ToolboxBitmap(typeof(OpaqueLayer))]
  2. public class OpaqueLayer:Control
  3. {
  4. private bool transparentBG = true;//是否使用透明
  5. private int alpha = 125;//设置透明度
  6. private Container components = new Container();
  7. public OpaqueLayer()
  8. : this(125, true)
  9. {
  10. }
  11.  
  12. public OpaqueLayer(int Alpha, bool IsShowLoadingImage)
  13. {
  14. SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
  15. base.CreateControl();
  16.  
  17. this.alpha = Alpha;
  18. if (IsShowLoadingImage)
  19. {
  20. PictureBox pictureBox_Loading = new PictureBox();
  21. pictureBox_Loading.BackColor = System.Drawing.Color.White;
  22. pictureBox_Loading.Image = WinLonding.Properties.Resources.Loading;
  23. pictureBox_Loading.Name = "pictureBox_Loading";
  24. pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
  25. pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
  26. Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中
  27. pictureBox_Loading.Location = Location;
  28. pictureBox_Loading.Anchor = AnchorStyles.None;
  29. this.Controls.Add(pictureBox_Loading);
  30. }
  31. }
  32.  
  33.  
  34. protected override void Dispose(bool disposing)
  35. {
  36. if (disposing)
  37. {
  38. if (!((components == null)))
  39. {
  40. components.Dispose();
  41. }
  42. }
  43. base.Dispose(disposing);
  44. }
  45.  
  46. /// <summary>
  47. /// 自定义绘制窗体
  48. /// </summary>
  49. /// <param name="e"></param>
  50. protected override void OnPaint(PaintEventArgs e)
  51. {
  52. float vlblControlWidth;
  53. float vlblControlHeight;
  54.  
  55. Pen labelBorderPen;
  56. SolidBrush labelBackColorBrush;
  57.  
  58. if (transparentBG)
  59. {
  60. Color drawColor = Color.FromArgb(this.alpha, this.BackColor);
  61. labelBorderPen = new Pen(drawColor, 0);
  62. labelBackColorBrush = new SolidBrush(drawColor);
  63. }
  64. else
  65. {
  66. labelBorderPen = new Pen(this.BackColor, 0);
  67. labelBackColorBrush = new SolidBrush(this.BackColor);
  68. }
  69. base.OnPaint(e);
  70. vlblControlWidth = this.Size.Width;
  71. vlblControlHeight = this.Size.Height;
  72. e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
  73. e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
  74. }
  75.  
  76.  
  77. protected override CreateParams CreateParams//v1.10
  78. {
  79. get
  80. {
  81. CreateParams cp = base.CreateParams;
  82. cp.ExStyle |= 0x00000020; //0x20; // 开启 WS_EX_TRANSPARENT,使控件支持透明
  83. return cp;
  84. }
  85. }
  86.  
  87. [Category("OpaqueLayer"), Description("Whether to use transparent, the default is true")]
  88. public bool TransparentBG
  89. {
  90. get
  91. {
  92. return transparentBG;
  93. }
  94. set
  95. {
  96. transparentBG = value;
  97. this.Invalidate();
  98. }
  99. }
  100.  
  101. [Category("OpaqueLayer"), Description("Set the transparency")]
  102. public int Alpha
  103. {
  104. get
  105. {
  106. return alpha;
  107. }
  108. set
  109. {
  110. alpha = value;
  111. this.Invalidate();
  112. }
  113. }
  114. }

这时候再点击show button 就能看到效果啦!

 

版权声明:本文为victor-huang原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/victor-huang/p/8579693.html