winform 自定义控件:半透明Loading控件
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控件的更多相关文章
- (一)c#Winform自定义控件-基类控件
前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. 开源地址:https://gitee.com/kwwwvagaa/net_winform_custom_control ...
- C# 自定义控件VS用户控件
1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container 控件,Container控件可以添加其他Con ...
- WPF实现炫酷Loading控件
Win8系统的Loading效果还是很不错的,网上也有人用CSS3等技术实现,研究了一下,并打算用WPF自定义一个Loading控件实现类似的效果,并可以让用户对Loading的颗粒(Particle ...
- 自定义控件VS用户控件
自定义控件VS用户控件 2015-06-16 1 自定义控件与用户控件区别 WinForm中, 用户控件(User Control):继承自 UserControl,主要用于开发 Container ...
- 富客户端 wpf, Winform 多线程更新UI控件
前言 在富客户端的app中,如果在主线程中运行一些长时间的任务,那么应用程序的UI就不能正常相应.因为主线程要负责消息循环,相应鼠标等事件还有展现UI. 因此我们可以开启一个线程来格外处理需要长时间的 ...
- [转] WinForm实现移除控件某个事件的方法
原文 WinForm实现移除控件某个事件的方法 本文实例讲述了WinForm实现移除控件某个事件的方法,供大家参考借鉴一下.具体功能代码如下: 主要功能部分代码如下: /// <summary& ...
- Winform中修改WebBrowser控件User-Agent的方法(已经测试成功)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...
- WPF中嵌入WinForm中的webbrowser控件
原文:WPF中嵌入WinForm中的webbrowser控件 使用VS2008创建WPF应用程序,需使用webbrowser.从工具箱中添加WPF组件中的webbrowser发现其中有很多属性事件不能 ...
- C# winform项目中ListView控件使用CheckBoxes属性实现单选功能
C# winform项目中ListView控件使用CheckBoxes属性实现单选功能 在做项目时需要使用ListView控件的CheckBoxes属性显示,还要在点击行时自动选中CheckBoxes ...
随机推荐
- 关于p标签
说p标签是不能嵌套div和p的,嵌套会被浏览器解析分离.但如果你使用了document.createElement创建div,再appendChild的话反而可以了.看来浏览器并不支持动态解析
- 探索ORM ————iBati(一)
ibatis iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton Begin在2001年发起的开放源代码项目.最初侧重于密码软件的开发,现在是一个基于Jav ...
- CentOS随笔 - 5.CentOS7安装Sql Server 2017
前言 转帖请注明出处: http://www.cnblogs.com/Troy-Lv5/ 开发环境嘛, 作为.Net系Sql Server那是必备的. 听过Sql server可以安装在Linux上了 ...
- VisualSVN Server迁移的方法
VisualSVN Server迁移涉及到两种情况: 第一种情况:VisualSVN Server没有更换电脑或者服务器,只是修改Server name. 第二种情况:当VisualSVN Serve ...
- linux smem 查看各进程使用memory情况
SMEM(8) SMEM(8) NAME smem - Report memory usage with shared memory divided proportionally. SYNOPSIS ...
- iOS手势处理
iOS手势处理 iOS手势有着如下几种: UITapGestureRecognizer UIPinchGestureRecognizer UIRotationGestureRecognizer UIS ...
- Redis学习---CentOs/RedHat下Redis的安装
redis是C语言开发,建议在linux上运行,本教程使用Centos6.4作为安装环境. 安装redis需要先将官网下载的源码进行编译,编译依赖gcc环境,如果没有gcc环境,需要安装gc ...
- 【原创】Qt 使用ODBC driver 连接SQL Server
最近在做数据库的课程设计.第一个需要解决的问题是使用什么工具来实现这个系统.经过一番资料查找,决定使用SQL Server Express 2012作为服务器,使用Qt作为编写客户端程序语言.问题是c ...
- C++用法总结
1.C++的绝对值符号 如果是整形的,就是abs() 如果是浮点型的,是fabs() 这两个函数都从属于库函数math.h #include <cmath> or #include< ...
- Windows10自动更新之后,无线有线都连不上网
大概浪费了我至少6个小时. 一个是无线网卡,这个后来可以修复,其实也不是网卡的原因.最主要的原因是 Realtek PCIe GBE Family Controller 这个驱动.只找到一个win1 ...