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. vmware虚拟机挂载Windows磁盘的两种方法

    第一种 vmware虚拟机通过ntfs-3g挂接windows盘 1.共享windows盘虚拟机设置——>添加硬盘——>选择IDE——>使用物理磁盘——>选择本地盘(单分区)— ...

  2. python读取shp

    sf = shapefile.Reader("res2_4m.shp") records = sf.records() print sf.fields for record in ...

  3. ubuntu下配置JDK7环境变量

    ubuntu下JDK配置本质上和win是一样的: 1.去官网下载JDK7,找jdk-7u21-linux-i586.tar.gz并下载:http://www.oracle.com/technetwor ...

  4. python相关知识/技巧文摘

    python文件和目录操作 python连接mysql数据库 Python字符编码详解 unicode相关介绍

  5. .net下log4net的使用

    这里以控制台应用程序为例 首先是要添加引用: 安装后可以看到项目中多了log4net的引用: 添加应用程序配置文件app.config,配置log4net <?xml version=" ...

  6. 使用CAReplicatorLayer [2]

    使用CAReplicatorLayer [2] 工具类 // // Math.h // MathEquation // // Created by YouXianMing on 15/11/20. / ...

  7. [翻译] CotEditor

    CotEditor https://github.com/coteditor/CotEditor CotEditor is a lightweight plain-text editor for OS ...

  8. Python学习---IO的异步[tornado模块]

    tornado是一个异步非阻塞的WEB框架.它的异步非阻塞实际上就是用事件循环写的. 主要体现在2点: 1. 作为webserver可以接收请求,同时支持异步处理请求.Django只能处理完成上一个请 ...

  9. gradle结合spring-boot生成可运行jar包,并打印日志

    1.用gradle把springboot项目打包成jar 1.1 build.gradle 中添加 buildscript { repositories { mavenLocal() maven { ...

  10. 我在德国做SAP CRM One Order redesign工作的心得

    时间过得很快,今天是我到德国工作的第四周,刚好一个月.Prototype的框架已经搭起来了,现在Order能够在新的框架下正常读写,能跑一些简单的scenario,这些scenario对于end us ...