• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[转]C#winForm创建水晶风格的按钮

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
时下水晶风格比较流行,在天极网看到一篇制作水晶按钮控件的文章,
首先启动Visual Studio 2005,新建空白解决方案,我们取名为:TestCrystalButton,
然后在项目导航栏上右击鼠标添加新项目,为此解决方案添加新的C# Windows 控件库,取名为MyControls。
IDE会创建一个继承于UserControl名为UserControl1的类,修改代码使其继承自Button,
并将原文件中所有引用UserControl1名称的地方都更改为CrystalButton,在项目导航栏中把UserControl1.cs更名为CrystalButton.cs
 然后将InitializeComponent()方法中的
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
语句注释掉,因为从Button控件不可能有自动缩放功能,它必须依赖于其父控件。
在源文件头部添加对System.Drawing.Imaging和System.Drawing.Drawing2D程序集的引用。
首先需要创建一个枚举类型MouseActionType,当按钮需要绘制时会根据当前鼠标的位置进行不同状态的绘制,
代码如下:

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);
        }
 
    }
}

在Windows Application中添加对此dll的引用即可制作出漂亮的水晶控件啦!

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C#动态系统托盘图标发布时间:2022-07-10
下一篇:
C#.NETML.NET机器学习图像分类发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap