C#模仿360安全卫士玻璃按钮,不闪烁,背景切换效率快
首先先上效果图:
1.准备两张透明的png图片(尺寸74 x 82),一张用于鼠标进入控件时显示,一张用于鼠标单击控件时显示
2.拖一个GlassButton按钮
3.设置按钮属性
this.btnEmailCount.BackColor = System.Drawing.Color.Transparent;
this.btnEmailCount.FlatAppearance.BorderSize = 0;
this.btnEmailCount.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
this.btnEmailCount.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
this.btnEmailCount.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnEmailCount.Font = new System.Drawing.Font("微软雅黑", 9F);
this.btnEmailCount.ForeColor = System.Drawing.Color.White;
this.btnEmailCount.Icon = global::Com.Qisentech.Email.Properties.Resources.btn_email_count;
this.btnEmailCount.Location = new System.Drawing.Point(89, 27);
this.btnEmailCount.Name = "btnEmailCount";
this.btnEmailCount.Size = new System.Drawing.Size(73, 81);
this.btnEmailCount.TabIndex = 15;
this.btnEmailCount.Text = "邮件统计";
this.btnEmailCount.UseVisualStyleBackColor = false;
this.btnEmailCount.Click += new System.EventHandler(this.btnEmailCount_Click);
下面奉上GlassButton的源码
using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Com.Qisentech.Controls.Methods; namespace Com.Qisentech.Controls.QQControls
{
#region//控件状态
/// <summary>
/// 控件状态
/// </summary>
public enum State
{
/// <summary>
/// 无
/// </summary>
Normal = 0,
/// <summary>
/// 获得焦点
/// </summary>
Focused = 1,
/// <summary>
/// 失去焦点
/// </summary>
LostFocused = 2,
/// <summary>
/// 鼠标指针进入控件
/// </summary>
MouseEnter = 3
}
#endregion
public class : Button
{
#region//私有变量
private int bmp_Left;
private const int bmp_Top = 5;
private const int bmp_Size = 45;
private bool _focused = false;
private State state = State.Normal; private Bitmap _icon = Properties.Resources.icon;
private string _text = null; #endregion #region//构造函数
public GlassButton()
{
SetStyle(ControlStyles.AllPaintingInWmPaint |
ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer, true); //双缓冲防止重绘时闪烁
SetStyle(ControlStyles.UserPaint, true); //自定义绘制控件内容
SetStyle(ControlStyles.SupportsTransparentBackColor, true); //模拟透明
SetStyle(ControlStyles.Selectable, true); //接收焦点
SetStyle(ControlStyles.ResizeRedraw, true);
Size = new Size(74, 82); //初始大小
Font = new System.Drawing.Font("微软雅黑", 9F); //控件字体 BackColor = System.Drawing.Color.Transparent;
FlatAppearance.BorderSize = 0;
FlatAppearance.MouseDownBackColor = System.Drawing.Color.Transparent;
FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
ForeColor = System.Drawing.Color.White;
UseVisualStyleBackColor = false;
Text = "玻璃按钮1"; }
#endregion #region//属性
/// <summary>
/// 获取或设置控件显示的图片
/// </summary>
[Description("获取或设置控件显示的图标")]
public Bitmap Icon
{
get { return _icon; }
set
{
_icon = value;
Invalidate(false);
}
} /// <summary>
/// 重写控件焦点属性
/// </summary>
[Description("重写控件焦点属性")]
public new bool Focused
{
get { return _focused; }
set
{
_focused = value; if (_focused)
state = State.Focused;
else
state = State.LostFocused; Invalidate(false);
}
} /// <summary>
/// 与控件关联的文本
/// </summary>
[Description("与控件关联的文本。"), DefaultValue(null)]
public new string Text
{
get
{
return _text;
}
set
{
_text = value;
Invalidate(false);
}
}
#endregion #region//重载事件
//自定义绘制
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
//g.DrawImage(_normalImg, ClientRectangle);
g.SmoothingMode = SmoothingMode.HighQuality;
g.PixelOffsetMode = PixelOffsetMode.HighQuality; switch (state)
{
case State.Focused:
{
DrawSelected(g);
break;
}
case State.MouseEnter:
{
if (!Focused)
g.DrawImage(Properties.Resources.enter, ClientRectangle);
else
DrawSelected(g);
break;
}
} DrawIcon(g);
DrawText(g);
} //焦点进入
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
Focused = true;
} //失去焦点
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
Focused = false;
} //禁止调整大小
protected override void OnResize(EventArgs e)
{
Width = 73;
Height = 81;
} protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
state = State.MouseEnter;
Invalidate(false);
} protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
if (!Focused)
{
state = State.LostFocused;
Invalidate(false);
}
} //只响应单击鼠标左键事件
protected override void OnClick(EventArgs e)
{
MouseEventArgs m = (MouseEventArgs)e;
if (m.Button == MouseButtons.Left)
{
base.OnClick(e);
Focus();
}
}
#endregion #region//方法 #region//Draw
void DrawSelected(Graphics g)
{
g.DrawImage(Properties.Resources.down, ClientRectangle);
} void DrawIcon(Graphics g)
{
if (_icon != null)
{
Bitmap bmp = ScaleZoom(_icon);
bmp_Left = (Width - bmp.Width) / 2;
g.DrawImage(bmp, new Rectangle(bmp_Left, bmp_Top, bmp.Width, bmp.Height));
}
} void DrawText(Graphics g)
{
SizeF size = g.MeasureString(_text, Font);
g.DrawString(_text, Font, new SolidBrush(ForeColor), (Width - size.Width) / 2, 58);
}
#endregion #region//按比例缩放图片
public Bitmap ScaleZoom(Bitmap bmp)
{
if (bmp != null)
{
double zoomScale;
if (bmp.Width > bmp_Size || bmp.Height > bmp_Size)
{
double imageScale = (double)bmp.Width / (double)bmp.Height;
double thisScale = 1; if (imageScale > thisScale)
{
zoomScale = (double)bmp_Size / (double)bmp.Width;
return BitMapZoom(bmp, bmp_Size, (int)(bmp.Height * zoomScale));
}
else
{
zoomScale = (double)bmp_Size / (double)bmp.Height;
return BitMapZoom(bmp, (int)(bmp.Width * zoomScale), bmp_Size);
}
}
}
return bmp;
}
#endregion #region//缩放BitMap
/// <summary>
/// 图片缩放
/// </summary>
/// <param name="bmpSource">源图片</param>
/// <param name="bmpSize">缩放图片的大小</param>
/// <returns>缩放的图片</returns>
public Bitmap BitMapZoom(Bitmap bmpSource, int bmpWidth, int bmpHeight)
{
Bitmap bmp, zoomBmp;
try
{
bmp = new Bitmap(bmpSource);
zoomBmp = new Bitmap(bmpWidth, bmpHeight);
Graphics gh = Graphics.FromImage(zoomBmp);
gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
gh.DrawImage(bmp, new Rectangle(0, 0, bmpWidth, bmpHeight),
new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
gh.Dispose();
return zoomBmp;
}
catch
{ }
finally
{
bmp = null;
zoomBmp = null;
GC.Collect();
}
return null;
}
#endregion #endregion
}
}
C#模仿360安全卫士玻璃按钮,不闪烁,背景切换效率快的更多相关文章
- android 按钮Button单击背景切换
res/drawable/btn_selected.xml <?xml version="1.0" encoding="utf-8"?> <s ...
- C#制作高仿360安全卫士窗体<一>
开始写这一系列博客之前先要向大家说声抱歉,放肆雷特建立很久却很少有更新.当然博客人气也不旺,大部分都是看的人多评论收藏的人少.一直想要改变这种状态,正好赶上了最近工作上做了一个高仿360安全卫士窗体. ...
- C#制作高仿360安全卫士窗体(四)- 水晶按钮
项目越来越紧,我也乐此不疲.自从上次C#制作高仿360安全卫士窗体(三)出来之后,就开始有一些人在说为什么还在坚持写这么落后的东西.我想说的是,我是从事企业信息化工作的,所有程序都只对内部使用.所以只 ...
- WPF如何实现一款类似360安全卫士界面的程序?(共享源码!)
以前学习Windows Form编程的时候,总感觉自己做的界面很丑,看到360安全卫士.迅雷等软件的UI设计都非常美观,心里总是憧憬着要是自己能实现这样的UI效果该多好!!!另一个困扰我的问题是,这个 ...
- C#制作高仿360安全卫士窗体<二>
继上次C#制作高仿360安全卫士窗体<一>发布之后响应还不错,我的博客放肆雷特也来了不少的新朋友,在这里先谢谢大家的支持!我自己也反复看了一下觉得对不起大家,写的非常乱而且很少文字介绍.在 ...
- C#制作高仿360安全卫士窗体(三)
距上篇C#制作高仿360安全卫士窗体(二)也将近一个多月了,这个月事情还是像往常一样的多.不多我也乐在其中,毕竟我做的是我喜欢做的东西.今天特地抽空把怎么制作文本框写一下.同时也希望有爱好这些玩意的同 ...
- C#制作高仿360安全卫士窗体3
C#制作高仿360安全卫士窗体(三) 距上篇C#制作高仿360安全卫士窗体(二)也将近一个多月了,这个月事情还是像往常一样的多.不多我也乐在其中,毕竟我做的是我喜欢做的东西.今天特地抽空把怎么制作 ...
- C#制作高仿360安全卫士窗体2
C#制作高仿360安全卫士窗体 继上次C#制作高仿360安全卫士窗体<一>发布之后响应还不错,我的博客放肆雷特也来了不少的新朋友,在这里先谢谢大家的支持!我自己也反复看了一下觉得对不起大家 ...
- 360安全卫士造成Sharepoint文档库”使用资源管理器打开“异常
备注:企业用户还是少用360为妙 有客户反馈:部门里的XP SP2环境客户机全部异常,使用资源管理器打开Sharepoint文档库,看到的界面样式很老土,跟本地文件夹不一样 ...
随机推荐
- SwapEffect 枚举(定义交换效果)
由于创建设备时要用到这个值,所以在这里总结一下,以免以后再找. 首先引自msdn: Copy 只能为构成单个后台缓冲区的交换链指定此交换效果. 无论交换链是有窗口的还是全屏的,运行库都保证 Devic ...
- nchar 和 nvarchar
字符数据类型(nchar 长度固定,nvarchar 长度可变)和 Unicode 数据使用 UNICODE UCS-2 字符集. nchar [ ( n ) ] n 个字符的固定长度的 Unicod ...
- NAT负载均衡
NAT(Network Address Translation 网络地址转换)简单地说就是将一个IP地址转换为另一个IP地址,一般用于未经注册的内部地址与合法的.已获注册的Internet IP地址间 ...
- 程序员的自我修养(2)——计算机网络(转) good
相关文章:程序员的自我修养——操作系统篇 几乎所有的计算机程序,都会牵涉到网络通信.因此,了解计算机基础网络知识,对每一个程序员来说都是异常重要的. 本文在介绍一些基础网络知识的同时,给出了一些高质量 ...
- imports,using,和include之间的区别
Imports, Using基本一样,有两个作用 1.将后面命名空间中所有的名字导入到当前命名空间 2.为后面的名字取一个当前命名空间可以访问的别名. 比如StreamWriter这个类在System ...
- 不能设置sublime text 2 为默认编辑器
今天遇到一个有趣的事情,当我设置 css 样式表的默认打开方式的时候,却始终无法设置成功,系统总是随机选取一种打开方式来打开文件.比如:pdf.DW.txt等方式. 我设置默认打开方式的步骤如下: 1 ...
- UVa 10213 (欧拉公式+Java大数) How Many Pieces of Land ?
题意: 一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 分析: 首先紫书上的公式是错的,不过根据书上提供的思路很容易稍加修改得到正确答案! 然后推公式吧,这里用到平面图的欧拉公 ...
- bzoj1061: [Noi2008]志愿者招募
线性规划与费用流.http://www.cnblogs.com/iiyiyi/p/5616080.html.数组范围开错了!!!然后2.31-1=0x7fffffff!=0x7f7f7f7f. 开始以 ...
- Android UI设计系统-android selector 开始自定义样式
Selector的结构描述: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:a ...
- 陈正冲老师讲c语言之声明和定义的区别
什么是定义?什么是声明?它们有何区别? 举个例子: A)int i; B)extern int i;(关于extern,后面解释) 哪个是定义?哪个是声明?或者都是定义或者都是声明?我所教过的学生几乎 ...