C#制作高仿360安全卫士窗体(四)- 水晶按钮

简介: 原文 http://www.cnblogs.com/kovin/archive/2013/10/18/3375977.html 项目越来越紧,我也乐此不疲。自从上次C#制作高仿360安全卫士窗体(三)出来之后,就开始有一些人在说为什么还在坚持写这么落后的东西。

原文 http://www.cnblogs.com/kovin/archive/2013/10/18/3375977.html

项目越来越紧,我也乐此不疲。自从上次C#制作高仿360安全卫士窗体(三)出来之后,就开始有一些人在说为什么还在坚持写这么落后的东西。我想说的是,我是从事企业信息化工作的,所有程序都只对内部使用。所以只要能满足需求就可以,比较高端先进的技术也没有时间去学习研究。
OK继续上次的内容。上次说到制作文本框,今天要写的是怎么实现水晶按钮的制作。下面是效果图:

下面是这个按钮所需要的图片素材,该素材也提取自360安全卫士。我自己做了一点点小的修改(另存为图片就可以使用):

一、嵌入资源
将以上素材另存为,将图片保存在解决方案中Images目录下的ButtonImages文件夹中,并设置图片属性中生成操作选择为“嵌入的资源”。

二、添加控件
资源嵌入之后再在ControlEx目录中建立一个名为GlassButton的文件夹用来存放该控件的代码。再在该目录中建立一个名为GlassButton的用户控件
三、编码
和之前的控件一样,最开始先把该控件拥有的状态定义出来,这里我定义了四种状态:

复制代码
 1     #region 控件状态
 2     /// <summary>
 3     /// 控件状态
 4     /// </summary>
 5     public enum State
 6     {
 7         /// <summary>
 8         /// 9         /// </summary>
10         Normal = 0,
11         /// <summary>
12         /// 获得焦点
13         /// </summary>
14         Focused = 1,
15         /// <summary>
16         /// 失去焦点
17         /// </summary>
18         LostFocused = 2,
19         /// <summary>
20         /// 鼠标指针进入控件
21         /// </summary>
22         MouseEnter = 3
23     }
24     #endregion
复制代码

下面是该控件的所有代码和之前基本一样:

复制代码
  1 /// <summary>
  2 /// Toolbar控件
  3 /// </summary>
  4 public class GlassButton : Control
  5 {
  6     #region//私有变量
  7     private int bmp_Left;
  8     private const int bmp_Top = 6;
  9     private const int bmp_Size = 48;
 10     private bool _focused = false;
 11     private State state = State.Normal;
 12     public Bitmap _BackImg = ImageObject.GetResBitmap("FANGSI.UI.Images.ButtonImages.Toolbar_Hover.png");
 13 
 14     private Bitmap _bmp = null;
 15     #endregion
 16 
 17     #region//构造函数
 18     public GlassButton()
 19     {
 20         try
 21         {
 22             this.SetStyle(ControlStyles.DoubleBuffer, true);
 23             this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
 24             this.SetStyle(ControlStyles.UserPaint, true);
 25             this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
 26             this.SetStyle(ControlStyles.StandardDoubleClick, false);
 27             this.SetStyle(ControlStyles.Selectable, true);
 28             ResizeRedraw = true;
 29             BackColor = Color.Transparent;
 30             ForeColor = Color.White;//初始文本颜色
 31             Size = new Size(80, 75);//初始大小
 32             Font = new Font("微软雅黑", 9, System.Drawing.FontStyle.Bold);//控件字体
 33         }
 34         catch { }
 35     }
 36     #endregion
 37 
 38     #region//属性
 39     /// <summary>
 40     /// 获取或设置控件显示的图片
 41     /// </summary>
 42     [CategoryAttribute("放肆雷特扩展属性"), Description("获取或设置控件显示的图标")]
 43     public Bitmap Bitmap
 44     {
 45         get { return _bmp; }
 46         set
 47         {
 48             _bmp = value;
 49             Invalidate(false);
 50         }
 51     }
 52 
 53     /// <summary>
 54     /// 重写控件焦点属性
 55     /// </summary>
 56     [CategoryAttribute("放肆雷特扩展属性"), Description("重写控件焦点属性")]
 57     public new bool Focused
 58     {
 59         get { return _focused; }
 60         set
 61         {
 62             _focused = value;
 63 
 64             if (_focused)
 65                 state = State.Focused;
 66             else
 67                 state = State.LostFocused;
 68 
 69             Invalidate(false);
 70         }
 71     }
 72     #endregion
 73 
 74     #region//重载事件
 75     //自定义绘制
 76     protected override void OnPaint(PaintEventArgs e)
 77     {
 78         base.OnPaint(e);
 79         Graphics g = e.Graphics;
 80         g.SmoothingMode = SmoothingMode.HighQuality;
 81         g.PixelOffsetMode = PixelOffsetMode.HighQuality;
 82 
 83         switch (state)
 84         {
 85             case State.Focused:
 86                 {
 87                     DrawSelected(g);
 88                     break;
 89                 }
 90             case State.MouseEnter:
 91                 {
 92                     if (!Focused)
 93                         //g.DrawImage(Properties.Resources.enter, ClientRectangle);
 94                         ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, 1, 2);
 95                     else
 96                         DrawSelected(g);
 97                     break;
 98                 }
 99         }
100 
101         DrawImage(g);
102         DrawText(g);
103     }
104 
105     //焦点进入
106     protected override void OnEnter(EventArgs e)
107     {
108         base.OnEnter(e);
109         Focused = true;
110     }
111 
112     //失去焦点
113     protected override void OnLeave(EventArgs e)
114     {
115         base.OnLeave(e);
116         Focused = false;
117     }
118 
119     //禁止调整大小
120     protected override void OnResize(EventArgs e)
121     {
122         Width = 80;
123         Height = 75;
124     }
125 
126     protected override void OnMouseEnter(EventArgs e)
127     {
128         base.OnMouseEnter(e);
129         state = State.MouseEnter;
130         Invalidate(false);
131     }
132 
133     protected override void OnMouseLeave(EventArgs e)
134     {
135         base.OnMouseLeave(e);
136         if (!Focused)
137         {
138             state = State.LostFocused;
139             Invalidate(false);
140         }
141     }
142 
143     //只响应单击鼠标左键事件
144     protected override void OnClick(EventArgs e)
145     {
146         MouseEventArgs m = (MouseEventArgs)e;
147         if (m.Button == MouseButtons.Left)
148         {
149             base.OnClick(e);
150             Focus();
151         }
152     }
153     #endregion
154 
155     #region//方法
156 
157     #region//Draw
158     void DrawSelected(Graphics g)
159     {
160         ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, 2, 2);
161     }
162 
163     void DrawImage(Graphics g)
164     {
165         if (_bmp != null)
166         {
167             Bitmap bmp = ScaleZoom(_bmp);
168             bmp_Left = (Width - bmp.Width) / 2;
169             g.DrawImage(bmp, new Rectangle(bmp_Left, bmp_Top, bmp.Width, bmp.Height));
170         }
171     }
172 
173     void DrawText(Graphics g)
174     {
175         SizeF size = g.MeasureString(Text, Font);
176         g.DrawString(Text, Font, new SolidBrush(ForeColor), (Width - size.Width) / 2, 55);
177     }
178     #endregion
179 
180     #region//按比例缩放图片
181     public Bitmap ScaleZoom(Bitmap bmp)
182     {
183         if (bmp != null)
184         {
185             double zoomScale;
186             if (bmp.Width > bmp_Size || bmp.Height > bmp_Size)
187             {
188                 double imageScale = (double)bmp.Width / (double)bmp.Height;
189                 double thisScale = 1;
190 
191                 if (imageScale > thisScale)
192                 {
193                     zoomScale = (double)bmp_Size / (double)bmp.Width;
194                     return BitMapZoom(bmp, bmp_Size, (int)(bmp.Height * zoomScale));
195                 }
196                 else
197                 {
198                     zoomScale = (double)bmp_Size / (double)bmp.Height;
199                     return BitMapZoom(bmp, (int)(bmp.Width * zoomScale), bmp_Size);
200                 }
201             }
202         }
203         return bmp;
204     }
205     #endregion
206 
207     #region//缩放BitMap
208     /// <summary>
209     /// 图片缩放
210     /// </summary>
211     /// <param name="bmpSource">源图片</param>
212     /// <param name="bmpSize">缩放图片的大小</param>
213     /// <returns>缩放的图片</returns>
214     public Bitmap BitMapZoom(Bitmap bmpSource, int bmpWidth, int bmpHeight)
215     {
216         Bitmap bmp, zoomBmp;
217         try
218         {
219             bmp = new Bitmap(bmpSource);
220             zoomBmp = new Bitmap(bmpWidth, bmpHeight);
221             Graphics gh = Graphics.FromImage(zoomBmp);
222             gh.InterpolationMode = InterpolationMode.HighQualityBicubic;
223             gh.DrawImage(bmp, new Rectangle(0, 0, bmpWidth, bmpHeight), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
224 
225             gh.Dispose();
226             return zoomBmp;
227         }
228         catch
229         { }
230         finally
231         {
232             bmp = null;
233             zoomBmp = null;
234             GC.Collect();
235         }
236         return null;
237     }
238     #endregion
239 
240     #endregion
241 }
复制代码

编译通过后即可在工具箱中使用,图标需要自己去找。至此360高仿安全卫士系列文章已经基本完了。没有写什么,就主要记录一下自己的开发历程。过几天整理源码上传上来,给大家下载使用。


本文来自 放肆雷特 | 胖子的技术博客

目录
相关文章
|
30天前
|
Java 数据库 C#
C#winforms实现windows窗体人脸识别
C#winforms实现windows窗体人脸识别
29 0
|
6月前
|
关系型数据库 MySQL C#
C# winform 一个窗体需要调用自定义用户控件的控件名称
给用户控件ucQRCode增加属性: //二维码图片 private PictureBox _pictureBoxFSHLQrCode; public PictureBox PictureBoxFSHLQrCode {   get { return _pictureBoxFSHLQrCode; }   set { this.pictureBoxFSHLQrCode = value; } } 在Form1窗体直接调用即可: ucQRCode uQRCode=new ucQRCode(); ucQRCode.PictureBoxFSHLQrCode.属性= 要复制或传给用户控件上的控件的值
36 0
|
4月前
|
JavaScript Linux C#
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
【傻瓜级JS-DLL-WINCC-PLC交互】1.C#用windows窗体控件创建.net控件
65 0
|
4月前
|
C# Windows
C#安装“Windows 窗体应用(.NET Framework)”
C#安装“Windows 窗体应用(.NET Framework)”
50 0
|
4月前
|
C# 数据安全/隐私保护
C# 窗体之间参数互相传递的两种方法与使用
C# 窗体之间参数互相传递的两种方法与使用
|
10月前
|
C# 数据安全/隐私保护
ApeForms | C# WinForm窗体控件平滑减速运动
在桌面软件开发中,有时会需要控制窗体或控件移动以实现某些界面效果,比如幻灯片换页、侧面的展开栏等。 通常情况下我们会使用Timer以每隔一段时间修改一下坐标位置的方式来实现目标对象的位移效果,但通过这个方式实现的动效存在几个问题: 匀速运动效果生硬; 运动过程中不便灵活改变运动状态(如侧栏展开一半时令其收起); 动效多时需要创建多个Timer对象,不易管理且占用资源; ApeForms中为控件和窗体提供了平滑运动的扩展方法,很好的解决了这些问题。不仅是坐标的平滑运动,还有控件\窗体尺寸的平滑变化、透明度的平滑变化。允许在变化的中途随时更改目标坐标\尺寸\透明度,且使用共享的Timer
11249 1
ApeForms | C# WinForm窗体控件平滑减速运动
|
C# Windows 容器
C#面向对象程序设计课程实验二: 实验名称:Windows 窗体程序
C#面向对象程序设计课程实验二: 实验名称:Windows 窗体程序
C#面向对象程序设计课程实验二: 实验名称:Windows 窗体程序
|
C#
C#中获得窗体的句柄
C#中获得窗体的句柄
223 0
C#编程学习19:mdi窗体中子窗体不能重复打开的三种实现方式
C#编程学习19:mdi窗体中子窗体不能重复打开的三种实现方式
C#编程学习19:mdi窗体中子窗体不能重复打开的三种实现方式
|
数据库 C#
C#编程学习18:使用多文档窗体框架利用DataGridView对Access数据表进行增删改及导出excel操作
C#编程学习18:使用多文档窗体框架利用DataGridView对Access数据表进行增删改及导出excel操作
C#编程学习18:使用多文档窗体框架利用DataGridView对Access数据表进行增删改及导出excel操作