项目越来越紧,我也乐此不疲。自从上次C#制作高仿360安全卫士窗体(三)出来之后,就开始有一些人在说为什么还在坚持写这么落后的东西。我想说的是,我是从事企业信息化工作的,所有程序都只对内部使用。所以只要能满足需求就可以,比较高端先进的技术也没有时间去学习研究。
OK继续上次的内容。上次说到制作文本框,今天要写的是怎么实现水晶按钮的制作。下面是效果图:
下面是这个按钮所需要的图片素材,该素材也提取自360安全卫士。我自己做了一点点小的修改(另存为图片就可以使用):
一、嵌入资源
将以上素材另存为,将图片保存在解决方案中Images目录下的ButtonImages文件夹中,并设置图片属性中生成操作选择为“嵌入的资源”。
二、添加控件
资源嵌入之后再在ControlEx目录中建立一个名为GlassButton的文件夹用来存放该控件的代码。再在该目录中建立一个名为GlassButton的用户控件
三、编码
和之前的控件一样,最开始先把该控件拥有的状态定义出来,这里我定义了四种状态:
#region 控件状态
/// <summary>
/// 控件状态
/// </summary>
public enum State
{
/// <summary>
/// 无
/// </summary>
Normal = ,
/// <summary>
/// 获得焦点
/// </summary>
Focused = ,
/// <summary>
/// 失去焦点
/// </summary>
LostFocused = ,
/// <summary>
/// 鼠标指针进入控件
/// </summary>
MouseEnter =
}
#endregion
下面是该控件的所有代码和之前基本一样:
/// <summary>
/// Toolbar控件
/// </summary>
public class GlassButton : Control
{
#region//私有变量
private int bmp_Left;
private const int bmp_Top = ;
private const int bmp_Size = ;
private bool _focused = false;
private State state = State.Normal;
public Bitmap _BackImg = ImageObject.GetResBitmap("FANGSI.UI.Images.ButtonImages.Toolbar_Hover.png"); private Bitmap _bmp = null;
#endregion #region//构造函数
public GlassButton()
{
try
{
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.StandardDoubleClick, false);
this.SetStyle(ControlStyles.Selectable, true);
ResizeRedraw = true;
BackColor = Color.Transparent;
ForeColor = Color.White;//初始文本颜色
Size = new Size(, );//初始大小
Font = new Font("微软雅黑", , System.Drawing.FontStyle.Bold);//控件字体
}
catch { }
}
#endregion #region//属性
/// <summary>
/// 获取或设置控件显示的图片
/// </summary>
[CategoryAttribute("放肆雷特扩展属性"), Description("获取或设置控件显示的图标")]
public Bitmap Bitmap
{
get { return _bmp; }
set
{
_bmp = value;
Invalidate(false);
}
} /// <summary>
/// 重写控件焦点属性
/// </summary>
[CategoryAttribute("放肆雷特扩展属性"), Description("重写控件焦点属性")]
public new bool Focused
{
get { return _focused; }
set
{
_focused = value; if (_focused)
state = State.Focused;
else
state = State.LostFocused; Invalidate(false);
}
}
#endregion #region//重载事件
//自定义绘制
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
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);
ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, , );
else
DrawSelected(g);
break;
}
} DrawImage(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 = ;
Height = ;
} 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)
{
ImageDrawRect.DrawRect(g, _BackImg, ClientRectangle, , );
} void DrawImage(Graphics g)
{
if (_bmp != null)
{
Bitmap bmp = ScaleZoom(_bmp);
bmp_Left = (Width - bmp.Width) / ;
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) / , );
}
#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 = ; 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(, , bmpWidth, bmpHeight), new Rectangle(, , bmp.Width, bmp.Height), GraphicsUnit.Pixel); gh.Dispose();
return zoomBmp;
}
catch
{ }
finally
{
bmp = null;
zoomBmp = null;
GC.Collect();
}
return null;
}
#endregion #endregion
}
编译通过后即可在工具箱中使用,图标需要自己去找。至此360高仿安全卫士系列文章已经基本完了。没有写什么,就主要记录一下自己的开发历程。过几天整理源码上传上来,给大家下载使用。
本文来自 放肆雷特 | 胖子的技术博客