C# 模仿360安全衛士玻璃按鈕(源碼)
注:感謝Aleax博友提出的Bug(見#7樓),現已糾正,糾正的內容會在要點里說明,修改過的源碼已附上,也謝謝各位博友提出的意見和方法,當然方法有很多種,這只是其中一種非常簡單和直接的方法,給初學者一點參考,望大家多多提意。
效果圖

一 準備工作
兩張透明的png圖片(尺寸73 x 81),一張用于鼠標進入控件時顯示,一張用于鼠標單擊控件時顯示。
新建自定義控件,在構造函數中添加代碼
public GlassButton()
{
SetStyle(ControlStyles.DoubleBuffer, true); //雙緩沖防止重繪時閃爍
SetStyle(ControlStyles.AllPaintingInWmPaint, true); //忽略 WM_ERASEBKGND 窗口消息減少閃爍
SetStyle(ControlStyles.UserPaint, true); //自定義繪制控件內容
SetStyle(ControlStyles.SupportsTransparentBackColor, true); //模擬透明
SetStyle(ControlStyles.Selectable, true); //接收焦點
Size = new Size(73, 81); //初始大小
Font = new Font("微軟雅黑", 9); //控件字體
}
新建枚舉,用來表示當前控件的狀態
///<summary>
/// 控件狀態
///</summary>
public enum State
{
///<summary>
/// 無
///</summary>
Normal = 0,
///<summary>
/// 獲得焦點
///</summary>
Focused = 1,
///<summary>
/// 失去焦點
///</summary>
LostFocused = 2,
///<summary>
/// 鼠標指針進入控件
///</summary>
MouseEnter = 3
}
二 制作方法
比較簡單,在這里只貼一下代碼,大家一看就明白
屬性
Property
///<summary>
/// 獲取或設置控件顯示的圖片
///</summary>
[Description("獲取或設置控件顯示的圖標")]
public Bitmap Bitmap
{
get { return _bmp; }
set {
_bmp = 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);
}
}
重載事件
Override
//自定義繪制
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);
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 = 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();
}
}
方法
Method
#region//Draw
void DrawSelected(Graphics g)
{
g.DrawImage(Properties.Resources.down, ClientRectangle);
}
void DrawImage(Graphics g)
{
if (_bmp != null)
{
Bitmap bmp = ScaleZoom(_bmp);
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
三 要點
要點就在于各個按鈕之間焦點切換的問題,就如效果圖所示,當有一個按鈕獲取了焦點,其他按鈕將顯示為失去焦點的狀態。
MSDN上建議當控件得到焦點時使用Enter事件,也就表示焦點進入當前所選擇控件,當控件失去焦點時使用Leave事件。
所以要重載Control組件的OnEnter和OnLeave事件,在此控件中重寫了Focused屬性,當引發了控件的焦點事件后將觸發OnPaint事件重新繪制,而引發Enter事件的發起者來自于控件的Focus()方法。
//焦點進入
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
Focused = true;
}
//失去焦點
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
Focused = false;
}
附


浙公網安備 33010602011771號