0%

美化单选按钮控件

  • 来源:明日科技
  • 美化单选按钮控件

    实例说明

    用户使用C#自带的控件制作应用程序时,为了使界面更加美观,更具有特色,可以对控件的样式进行重绘,本实例将对单选按钮控件进行重绘,以用户自定义样式进行显示。实例运行效果如图1所示。

    图1 美化单选按钮控件

    关键技术

    本实例在美化单选按钮控件时,主要是通过重绘单选框实现的,具体实现时,首先需要创建用户控件,然后在用户控件中使用Graphics类的DrawEllipse方法和FillEllipse方法实现重绘单选框操作。下面对本实例中用到的关键技术进行详细讲解。

    (1)创建用户控件并使用 创建用户控件的步骤如下:
    ①选中当前项目,单击右键,在弹出的快捷菜单中选择”添加”/“新建项”命令,弹出如图2所示的”添加新项”对话框。

    图2 “添加新项”对话框
    ②在图2所示对话框中找到”用户控件”并选中,在”名称”文本框中输入用户控件的名称,单击”添加”按钮,即可在当前项目中添加一个用户控件,如图3所示。

    图3 添加的用户控件
    ③在用户控件中,如果需要添加Windows标准控件,可以从”工具箱”中直接拖放使用;如果需要编写代码,则单击”单击此处切换到代码视图”超级链接,进入后台代码视图,以便编写所需的代码。
    ④用户控件制作完成后,选中制作完成的用户控件,用鼠标将其拖拽到工具箱中,如图4所示。

    图4 将用户控件拖拽到工具箱中
    ⑤最后就可以像使用Windows标准控件一样,对用户控件进行拖拽使用了。
    (2)DrawEllipse方法
    DrawEllipse方法用来绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆,该方法有4种重载形式,本实例中用到的它的重载形式如下:

    public void DrawEllipse(Pen pen,Rectangle rect)

    参数说明 R pen:Pen对象,它确定曲线的颜色、宽度和样式。
    rect:Rectangle结构,它定义椭圆的边界。
    (3)FillEllipse方法 FillEllipse方法用来填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定,该方法有4种重载形式,本实例中用到的它的重载形式如下:

    public void FillEllipse(Brush brush,Rectangle rect)

    参数说明 R brush:确定填充特性的Brush。
    rect:Rectangle结构,它表示定义椭圆的边框。

    设计过程

    (1)打开Visual Studio 2008开发环境,新建一个Windows窗体应用程序,命名为BeautifulRadioButton。
    (2)在当前项目中添加一个用户控件,将其命名为GlorifyRadioButton。将用户控件继承的UserControl类改为RadioButton类。
    (3)程序主要代码如下: 在GlorifyRadioButton控件中定义变量,代码如下:

    1
    2
    3
    4
    5
    6
    7
    private bool FontAspect = false; //判断字体的方向
    private int Measurement = 255; //设置渐变的初始值
    LinearGradientBrush Periphery_br; //外圆的颜色
    LinearGradientBrush Central_br; //移入控件时中圆的颜色
    LinearGradientBrush NoCentral_br; //无操作时中圆的颜色
    LinearGradientBrush Stipple_br; //内圆选中的颜色
    LinearGradientBrush NoStipple_br; //内圆无选中的颜色

    在GlorifyRadioButton控件的OnPaint事件中对控件的样式进行重绘,主要是通过SystemInformation类的SmallIconSize属性来获取单选按钮左边单选框的大小及位置,然后对单选框进行重绘。实现代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    /// <summary>
    /// 控件在需要重绘时触发
    /// </summary>
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
    e.Graphics.FillRectangle(SystemBrushes.Control, e.ClipRectangle); //填充矩形
    //清除锯齿
    e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
    Rectangle boxrect = new Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height); //获取左面图标的区域
    //获取绘制的文本的区域
    Rectangle strrect = new Rectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width, e.ClipRectangle.Y, e.ClipRectangle.Width + 2 - SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
    if (FontAspect) //判断字体的读取方式
    {
    //设置椭圆的位置
    boxrect.X = e.ClipRectangle.X + e.ClipRectangle.Width - SystemInformation.SmallIconSize.Width;
    strrect.X = e.ClipRectangle.X; //设置字体位置
    }
    Point MousePos = this.PointToClient(Control.MousePosition); //获取鼠标的位置
    bool Above = e.ClipRectangle.Contains(MousePos); //获取鼠标是否在当前控件上
    DrawBox(e.Graphics, boxrect, Above); //绘制单选图案
    DrawText(e.Graphics, strrect); //绘制文字
    if (!Enabled)
    e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(127, SystemColors.Control)), e.ClipRectangle);
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
      自定义方法DrawBox主要通过LinearGradientBrush类来设置单选框的绘制颜色,然后用Graphics类进行绘制。实现代码如下:

    /// <summary>
    /// 绘制单选控件的图案
    /// </summary>
    /// <param g="Graphics">封装一个绘图的对象</param>
    /// <param rect="Rectangle">单选图案的绘制区域</param>
    /// <param Above="bool">断判鼠标是否在控件上方</param>
    private void DrawBox(Graphics g, Rectangle rect, bool Above)
    {
    //设置外椭圆的渐变色
    int opacity = Measurement;
    Periphery_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 2, PeripheryColor), Color.FromArgb(opacity, PeripheryColor), LinearGradientMode.ForwardDiagonal);
    //设置中间椭圆形选中时的渐变色
    opacity = (int)(.4f * opacity + .5f);
    Central_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, CentralColor), Color.FromArgb(opacity, CentralColor), LinearGradientMode.ForwardDiagonal);
    //设置中间椭圆形无操作时的渐变色
    opacity = (int)(.4f * opacity + .5f);
    NoCentral_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, NoCentralColor), Color.FromArgb(opacity, NoCentralColor), LinearGradientMode.ForwardDiagonal);
    //设置内圆形选中时的渐变色
    opacity = Measurement;
    Stipple_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 5, StippleColor), Color.FromArgb(opacity, StippleColor), LinearGradientMode.BackwardDiagonal);
    //设置内圆形无操作时的渐变色
    opacity = (int)(.4f * opacity + .5f);
    NoStipple_br = new LinearGradientBrush(rect, Color.FromArgb(opacity / 10, NoStippleColor), Color.FromArgb(opacity, NoStippleColor), LinearGradientMode.BackwardDiagonal);
    int size = this.Font.Height; //获取字体的高度
    //获取外椭圆的区域
    Rectangle box = new Rectangle(rect.X + ((rect.Width - size) / 2), rect.Y + ((rect.Height - size) / 2), size - 2, size - 2);
    Rectangle glyph = new Rectangle(box.X + 3, box.Y + 3, box.Width - 6, box.Height - 6); //设置内圆的绘制区域
    g.FillEllipse(new SolidBrush(SystemColors.Window), box); //以白色填充单选图案
    if (Above && this.Enabled) //如果鼠标移入该控件
    {
    g.DrawEllipse(new Pen(Central_br, (float)(3)), new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //绘制中心椭圆
    }
    else
    {
    g.DrawEllipse(new Pen(NoCentral_br, (float)(3)), new Rectangle(box.X + 2, box.Y + 2, box.Width - 4, box.Height - 4)); //绘制中心椭圆
    }
    g.DrawEllipse(new Pen(Periphery_br, (float)(1.5)), box); //绘制外椭圆
    g.FillEllipse(new SolidBrush(SystemColors.Window), glyph); //以白色填充内圆
    if (this.Checked) //如果选中当前控件
    g.FillEllipse(Stipple_br, glyph); //填充内圆
    else
    g.FillEllipse(NoStipple_br, glyph); //填充内圆
    }

    自定义方法DrawText主要是通过判断单选按钮控件的显示方向,并在控件的指定位置绘制文本信息。实现代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /// <summary>
    /// 绘制文本
    /// </summary>
    /// <param g="Graphics">封装一个绘图的对象</param>
    /// <param rect="Rectangle">绘制文本的区域</param>
    private void DrawText(Graphics g, Rectangle rect)
    {
    StringFormat tem_StringF = new StringFormat(); //创建StringFormat对象
    tem_StringF.Alignment = StringAlignment.Near; //指定文本靠近布局对齐
    tem_StringF.LineAlignment = StringAlignment.Center; //文本居中对齐
    if (FontAspect) //如果控件是原始方向显示
    tem_StringF.FormatFlags = StringFormatFlags.DirectionRightToLeft; //按从右到左的顺序显示文本
    if (!FontAspect) //如果控件是反方向显示
    //绘制文本(从左端开始绘制)
    g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);
    else
    {
    rect.X = rect.X - SystemInformation.SmallIconSize.Width / 2 + 2; //设置文本的起始位置(在小图标后)
    //绘制文本
    g.DrawString(this.Text, this.Font, SystemBrushes.ControlText, rect, tem_StringF);
    }
    }

    自定义方法getAspect主要用于获取单选按钮控件的显示方向,实现代码如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    /// <summary>
    /// 获取文本的读取方向
    /// </summary>
    /// <return>布尔型</return>
    private bool getAspect()
    {
    bool tem_Aspect = false;
    if (this.RightToLeft == RightToLeft.Yes) //从右到左进行读取
    tem_Aspect = true;
    if (this.RightToLeft == RightToLeft.No) //从左到右进行读取
    tem_Aspect = false;
    return tem_Aspect;
    }

    秘笈心法

    心法领悟002:RadioButton控件的使用。
    RadioButton控件,又称为单选按钮控件,它为用户提供由两个或多个互斥选项组成的选项集,当用户选中某单选按钮时,同一组中的其他单选按钮不能同时选定。例如,在开发考试管理系统时,如果考试题中有单项选择题,则可以使用RadioButton控件来表示单项选择题的各个选项。

    觉得文章有用?请我喝杯咖啡~