winform  自定义控件:半透明Loading控件

by wgscd

date:2015-05-05

效果:

using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;

namespace wgscd
{
/// <summary>
/// 自定义控件:半透明控件
/// </summary>

[ToolboxBitmap(typeof(MyLoadingCtl))]
public class MyLoadingCtl : System.Windows.Forms.Control
{
private bool _transparentBG = true;//是否使用透明
private int _alpha = 125;//设置透明度
private SolidBrush fontBrush= new SolidBrush(Color.FromArgb(206,94,94,94));

private System.ComponentModel.Container components = new System.ComponentModel.Container();

public MyLoadingCtl()
: this(125, true)
{
}

/// <summary>
/// 显示遮罩层
/// </summary>
/// <param name="form">要显示的父窗体</param>
public void ShowLoading(Form form)
{

try
{
// SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
base.CreateControl();
form.Controls.Add(this);
// this._alpha = Alpha;
// if (isShowLoadingImage)
//{
// PictureBox pictureBox_Loading = new PictureBox();
// pictureBox_Loading.BackColor = System.Drawing.Color.White;
// pictureBox_Loading.Image = 加载中.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);
//}

this.Dock = DockStyle.Fill;
this.BringToFront();
this.Enabled = true;
this.Visible = true;
Refresh();
Invalidate();

}
catch { }

}

public MyLoadingCtl(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 = 加载中.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);
Invalidate();
}
}

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(System.Windows.Forms.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);
e.Graphics.DrawString("正在加载...",new Font("黑体",10), fontBrush, vlblControlWidth/2-30, vlblControlHeight/2+40);

}

protected override CreateParams CreateParams//v1.10
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x00000020; //0x20; // 开启 WS_EX_TRANSPARENT,使控件支持透明
return cp;
}
}

/*
* [Category("MyLoadingCtl"), Description("是否使用透明,默认为True")]
* 一般用于说明你自定义控件的属性(Property)。
* Category用于说明该属性属于哪个分类,Description自然就是该属性的含义解释。
*/
[Category("MyLoadingCtl"), Description("是否使用透明,默认为True")]
public bool TransparentBG
{
get
{
return _transparentBG;
}
set
{
_transparentBG = value;
this.Invalidate();
}
}

[Category("MyLoadingCtl"), Description("设置透明度")]
public int Alpha
{
get
{
return _alpha;
}
set
{
_alpha = value;
this.Invalidate();
}
}
}

}

/*

外部窗体调用:

public Form1()
{
InitializeComponent();
bgWorker.DoWork += BgWorker_DoWork;
bgWorker.RunWorkerCompleted += BgWorker_RunWorkerCompleted;
}

BackgroundWorker bgWorker = new BackgroundWorker();
wgscd.MyLoadingCtl loadingCtl = new wgscd.MyLoadingCtl(162,true);//定义加载控件
private void BgWorker_DoWork(object sender, DoWorkEventArgs e)
{

System.Threading.Thread.Sleep(3333);

}

private void BgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
loadingCtl.Hide();

}

private void btnShow_Click(object sender, EventArgs e)
{
//显示加载动画
loadingCtl.ShowLoading(this);
Application.DoEvents();
bgWorker.RunWorkerAsync();

}

*/

用到是loading GIF图片:

再来些

winform 自定义控件:半透明Loading控件的更多相关文章

  1. (一)c#Winform自定义控件-基类控件

    前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...

  2. C# 自定义控件VS用户控件

    1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Con ...

  3. WPF实现炫酷Loading控件

    Win8系统的Loading效果还是很不错的,网上也有人用CSS3等技术实现,研究了一下,并打算用WPF自定义一个Loading控件实现类似的效果,并可以让用户对Loading的颗粒(Particle ...

  4. 自定义控件VS用户控件

    自定义控件VS用户控件 2015-06-16 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container ...

  5. 富客户端 wpf, Winform 多线程更新UI控件

    前言 在富客户端的app中,如果在主线程中运行一些长时间的任务,那么应用程序的UI就不能正常相应.因为主线程要负责消息循环,相应鼠标等事件还有展现UI. 因此我们可以开启一个线程来格外处理需要长时间的 ...

  6. [转] WinForm实现移除控件某个事件的方法

    原文 WinForm实现移除控件某个事件的方法 本文实例讲述了WinForm实现移除控件某个事件的方法,供大家参考借鉴一下.具体功能代码如下: 主要功能部分代码如下: /// <summary& ...

  7. Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...

  8. WPF中嵌入WinForm中的webbrowser控件

    原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...

  9. C# winform项目中ListView控件使用CheckBoxes属性实现单选功能

    C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...

随机推荐

  1. Qt——元对象和属性机制

    http://www.cnblogs.com/hellovenus/p/5582521.html 一.元对象 元对象(meta object)意思是描述另一个对象结构的对象,比如获得一个对象有多少成员 ...

  2. CentOS6.4 下安装 jdk1.7.0_67

    1.卸载系统自带的jdk 1.1.查看该操作系统上是否已经安装了jdk [root@xhTest-1 ~]# rpm -qa | grep jdk 1.2.删除系统自带的jdk [root@xhTes ...

  3. 【Kettle】2、文件夹与界面介绍

    1.文件夹介绍 下载Kettle6.1解压后出现下图相关文件夹以及文件夹介绍说明: Lib:存放Kettle的核心(core)jar包.工作引擎(engine)jar包.数据库(DB) jar包.图形 ...

  4. springMVC入门-08

    这一讲介绍用户登录实现以及两种异常处理controller控制器的方法,最后提一下在springMVC中对静态资源访问的实现方法. 用户登录需要一个登录页面login.jsp,对应代码如下所示: &l ...

  5. 使用Membership,您的登录尝试不成功。请重试"的解决方法

    提示信息是标准Login控件产生的,打开数据库,检查aspnet_Membership表,检查IsLockedOut字段的值是否为False, 如果为True,表示这个用户锁定了,把它改成False即 ...

  6. python基础_类型_tuple

    #tuple 元祖,这个没什么特别的,和list差不多,不能删除,不能增加元素,其他功能差不多 #元祖用圆括号扩起来,逗号分隔 a = ('a','b','c') #这玩意一般会用来排除重复,还是很好 ...

  7. spring mvc 解决跨域问题

    Spring MVC 从4.2版本开始增加了对CORS的支持. 在Controller上使用@CrossOrigin注解: // 指定域名 @CrossOrigin("http://doma ...

  8. 定制选择范围的按钮RangeButton

    定制选择范围的按钮RangeButton 效果: 源码: RangeButton.h 与 RangeButton.m // // RangeButton.h // PulsingView // // ...

  9. jQuery插件实例三:图片滚动[切换]效果一

    图片切换效果在很多网站上都能看到,是一种常见的广告/活动宣传方式,通常位于网页上端.这个插件是众多图片切换效果的形式中的一种,数据源可在前端配置,也可从后台通JSON格式传输数据,当然,数据格式是固定 ...

  10. PHP设计模式系列 - 中介者模式

    中介者模式 中介者模式用于开发一个对象,这个对象能够在类似对象相互之间不直接相互的情况下传送或者调解对这些对象的集合的修改.一般处理具有类似属性,需要保持同步的非耦合对象时,最佳的做法就是中介者模式. ...