在 WinForm 中 如何实现 加载等待功能
1,需要一个动态的londing文件;在项目中我们新建一个文件夹来存放它;
2,在需要出现londing状态的窗体上加上一个Panel;
黄色区域是Panel,灰色的是需要被加载的区域。当需要触发londing,我们就把Panel显示出来,不让用户在这个区域能做任何修改;
下面的两个button事件就是触发londing的;在这里实例化的 OpaqueCommand对象;我们需要创建一个OpaqueCommand类
- OpaqueCommand cmd = new OpaqueCommand();
- /// <summary>
- /// 启动加载功能
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void show_Click(object sender, EventArgs e)
- {
- cmd.ShowOpaqueLayer(panelLonding, 125, true);
- }
- /// <summary>
- /// 关闭加载功能
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="e"></param>
- private void hide_Click(object sender, EventArgs e)
- {
- cmd.HideOpaqueLayer();
- }
3,OpaqueCommand类里面的内容
- public class OpaqueCommand
- {
- private OpaqueLayer m_OpaqueLayer = null;
- public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage)
- {
- try
- {
- if (this.m_OpaqueLayer == null)
- {
- this.m_OpaqueLayer = new OpaqueLayer(alpha, isShowLoadingImage);
- control.Controls.Add(this.m_OpaqueLayer);
- this.m_OpaqueLayer.Dock = DockStyle.Fill;
- this.m_OpaqueLayer.BringToFront();
- }
- this.m_OpaqueLayer.Enabled = true;
- this.m_OpaqueLayer.Visible = true;
- }
- catch { }
- }
- public void HideOpaqueLayer()
- {
- try
- {
- if (this.m_OpaqueLayer != null)
- {
- this.m_OpaqueLayer.Visible = false;
- this.m_OpaqueLayer.Enabled = false;
- }
- }
- catch (Exception ex)
- {
- //MessageBox.Show(ex.Message);
- }
- }
- }
4,创建 OpaqueLayer 类,这里面是重新绘制了窗体,内容如下
- [ToolboxBitmap(typeof(OpaqueLayer))]
- public class OpaqueLayer:Control
- {
- private bool transparentBG = true;//是否使用透明
- private int alpha = 125;//设置透明度
- private Container components = new Container();
- public OpaqueLayer()
- : this(125, true)
- {
- }
- public OpaqueLayer(int Alpha, bool IsShowLoadingImage)
- {
- SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
- base.CreateControl();
- this.alpha = Alpha;
- if (IsShowLoadingImage)
- {
- PictureBox pictureBox_Loading = new PictureBox();
- pictureBox_Loading.BackColor = System.Drawing.Color.White;
- pictureBox_Loading.Image = WinLonding.Properties.Resources.Loading;
- pictureBox_Loading.Name = "pictureBox_Loading";
- pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
- pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
- Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中
- pictureBox_Loading.Location = Location;
- pictureBox_Loading.Anchor = AnchorStyles.None;
- this.Controls.Add(pictureBox_Loading);
- }
- }
- protected override void Dispose(bool disposing)
- {
- if (disposing)
- {
- if (!((components == null)))
- {
- components.Dispose();
- }
- }
- base.Dispose(disposing);
- }
- /// <summary>
- /// 自定义绘制窗体
- /// </summary>
- /// <param name="e"></param>
- protected override void OnPaint(PaintEventArgs e)
- {
- float vlblControlWidth;
- float vlblControlHeight;
- Pen labelBorderPen;
- SolidBrush labelBackColorBrush;
- if (transparentBG)
- {
- Color drawColor = Color.FromArgb(this.alpha, this.BackColor);
- labelBorderPen = new Pen(drawColor, 0);
- labelBackColorBrush = new SolidBrush(drawColor);
- }
- else
- {
- labelBorderPen = new Pen(this.BackColor, 0);
- labelBackColorBrush = new SolidBrush(this.BackColor);
- }
- base.OnPaint(e);
- vlblControlWidth = this.Size.Width;
- vlblControlHeight = this.Size.Height;
- e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
- e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
- }
- protected override CreateParams CreateParams//v1.10
- {
- get
- {
- CreateParams cp = base.CreateParams;
- cp.ExStyle |= 0x00000020; //0x20; // 开启 WS_EX_TRANSPARENT,使控件支持透明
- return cp;
- }
- }
- [Category("OpaqueLayer"), Description("Whether to use transparent, the default is true")]
- public bool TransparentBG
- {
- get
- {
- return transparentBG;
- }
- set
- {
- transparentBG = value;
- this.Invalidate();
- }
- }
- [Category("OpaqueLayer"), Description("Set the transparency")]
- public int Alpha
- {
- get
- {
- return alpha;
- }
- set
- {
- alpha = value;
- this.Invalidate();
- }
- }
- }
这时候再点击show button 就能看到效果啦!