C# winForm 建立水晶風格的按鈕
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
語句註釋掉,因為從Button控制元件不可能有自動縮放功能,它必須依賴於其父控制元件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
namespace TestCrystalButton
{
public partial class CrystalButton : Button
{
public CrystalButton()
{
InitializeComponent();
}
private enum MouseActionType
{
None,
Hover,
Click
}
private MouseActionType mouseAction;
private GraphicsPath GetGraphicsPath(Rectangle rc, int r)
{
int x = rc.X, y = rc.Y, w = rc.Width, h = rc.Height;
GraphicsPath path = new GraphicsPath();
path.AddArc(x, y, r, r, 180, 90);
//Upper left corner
path.AddArc(x + w - r, y, r, r, 270, 90);
//Upper right cornr
path.AddArc(x + w - r, y + h - r, r, r, 0, 90);
//Lower right corner
path.AddArc(x, y + h - r, r, r, 90, 90);
//Lower left corner
path.CloseFigure();
return path;
}
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(SystemColors.ButtonFace);
Color clr = Color.Wheat;//BtnColor;
int shadowOffset = 8;
int btnOffset = 0;
switch(mouseAction)
{
case MouseActionType.Click :
shadowOffset = 4;
clr = Color.LightGray;
btnOffset = 2;
break;
case MouseActionType.Hover:
clr = Color.LightGray;
break;
}
g.SmoothingMode = SmoothingMode.AntiAlias;
// 建立按鈕本身的圖形
Rectangle rc = new Rectangle(btnOffset, btnOffset, this.ClientSize.Width - 8 - btnOffset, this.ClientSize.Height - 8 - btnOffset);
GraphicsPath path1 = this.GetGraphicsPath(rc, 20);
LinearGradientBrush br1 = new LinearGradientBrush(new Point(0, 0), new Point(0, rc.Height + 6), clr, Color.White);
// 建立按鈕陰影
Rectangle rc2 = rc;
rc2.Offset(shadowOffset, shadowOffset);
GraphicsPath path2 = this.GetGraphicsPath(rc2, 20);
PathGradientBrush br2 = new PathGradientBrush(path2);
br2.CenterColor = Color.Black;
br2.SurroundColors = new Color[] { SystemColors.ButtonFace };
//為了更逼真,我們將漸變結束顏色設定為窗體前景顏色,可以根據視窗的前景顏色適當調整
//建立按鈕頂部白色漸變
Rectangle rc3 = rc;
rc3.Inflate(-5, -5);
rc3.Height = 15;
GraphicsPath path3 = GetGraphicsPath(rc3, 20);
LinearGradientBrush br3 = new LinearGradientBrush(rc3, Color.FromArgb(255, Color.White), Color.FromArgb(0, Color.White), LinearGradientMode.Vertical);
// 繪製圖形
g.FillPath(br2, path2); //繪製陰影
g.FillPath(br1, path1); //繪製按鈕
g.FillPath(br3, path3); //繪製頂部白色泡泡
//設定記憶體點陣圖物件,進行二級快取繪圖操作
Rectangle buttonBitmapRectangle = new Rectangle(rc.Location, rc.Size);
Bitmap buttonBitmap = new Bitmap(buttonBitmapRectangle.Width, buttonBitmapRectangle.Height);
Graphics g_bmp = Graphics.FromImage(buttonBitmap);
g_bmp.SmoothingMode = SmoothingMode.AntiAlias;
g_bmp.FillPath(br1, path1);
g_bmp.FillPath(br3, path3);
//將region賦值給button
Region rgn = new Region(path1);
rgn.Union(path2);
this.Region = rgn;
// 繪製按鈕的文字
GraphicsPath path4 = new GraphicsPath();
RectangleF path1bounds = path1.GetBounds();
Rectangle rcText = new Rectangle((int)path1bounds.X + btnOffset, (int)path1bounds.Y + btnOffset, (int)path1bounds.Width, (int)path1bounds.Height);
StringFormat strformat = new StringFormat();
strformat.Alignment = StringAlignment.Center;
strformat.LineAlignment = StringAlignment.Center;
path4.AddString(this.Text, this.Font.FontFamily, (int)this.Font.Style, this.Font.Size, rcText, strformat);
Pen txtPen = new Pen(this.ForeColor, 1);
g.DrawPath(txtPen, path4);
g_bmp.DrawPath(txtPen, path4);
}
}
}
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-466020/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 建立SWING風格的按鈕控制元件 (轉)控制元件
- 仿switch風格滑動按鈕
- 純CSS Material Design風格按鈕CSSMaterial Design
- 從“按鈕”看設計風格的演變
- 自繪實現半透明水晶按鈕
- 用CSS Houdini實現一個Material風格的按鈕CSS
- 使用WPF建立炫亮按鈕
- Winform下的Datagrid的列風格(4)—DataGridComboBoxTableViewColumnORMView
- SAP CRM note建立按鈕被禁用的原因分析
- 建立WINDOWS XP樣式的ActiveX按鈕 (轉)Windows
- 按鈕上面的按鈕 (轉)
- 建立漂亮的 CSS 按鈕的 10 個程式碼片段CSS
- 建立 SysV 風格的 linux daemon 程式Linux
- c# winform之單選鈕2狀態變換程式碼C#ORM
- 動態建立具有刪除行按鈕的table表格
- SVG 建立 Material Design 波紋效果按鈕SVGMaterial Design
- SVG建立Material Design波紋效果按鈕SVGMaterial Design
- 按鈕
- C#自定義控制元件—旋轉按鈕C#控制元件
- Fiori Elements List Report table 裡的普通按鈕,Global 按鈕 和 Determining 按鈕
- Java GUI之建立一個只有兩個按鈕的框架JavaGUI框架
- 如何在鬆弛中建立互動式按鈕
- 凸出按鈕的TabBartabBar
- SAP: ALV GRID 控制之 單元格按鈕
- 按鈕禁用
- Bootstrap 按鈕boot
- 實踐基於REST風格的Webservice(PHP,C#)RESTWebPHPC#
- adobe flash建立按鈕並新增動作【Z】
- Android 學習筆記四:建立工具欄按鈕Android筆記
- Godot遍歷目錄下檔案,並建立按鈕Go
- ABAP ALV TOOLBAR 自定義按鈕的型別以及listmenu按鈕型別
- 按鈕置灰的思考
- 窗體(文字框,按鈕,單選按鈕,標籤)
- Unity使用小劇場—建立的按鈕On Click()只有MonoScript怎麼辦UnityMono
- 小程式按鈕
- Flutter Button(按鈕)Flutter
- iOS 按鈕動畫iOS動畫
- 復飛按鈕