扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:窦瑞欣 来源:天极开发 2007年11月11日
关键字:
namespace MyControls { public partial class CrystalButton: Button { public CrystalButton () { InitializeComponent(); } } } |
...... private enum MouseActionType { None, Hover, Click } private MouseActionType mouseAction; |
protected override void OnPaint(PaintEventArgs e) { Graphics g = e.Graphics; g.Clear(SystemColors.ButtonFace ); Color clr = this.BackColor; 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); //绘制顶部白色泡泡 //设定内存位图对象,进行二级缓存绘图操作 buttonBitmapRectangle = new Rectangle(rc.Location, rc.Size); 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); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者