在 Windows 窗体中切换切换控件 [英] Toggle Switch Control in Windows Forms

查看:27
本文介绍了在 Windows 窗体中切换切换控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CheckBox 设计一个 Toggle Switch 控件,但目前我的控件只绘制一个圆圈.如何绘制如下图所示的圆形,以及如何根据控件的值更改圆圈的位置以表示如下图所示的选中和未选中状态:

这是我的代码:

公共类 MyCheckBox:CheckBox{公共 MyCheckBox(){this.Appearance = System.Windows.Forms.Appearance.Button;this.BackColor = Color.Transparent;this.TextAlign = ContentAlignment.MiddleCenter;this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;this.FlatAppearance.BorderColor = Color.RoyalBlue;this.FlatAppearance.BorderSize = 2;}protected override void OnPaint(PaintEventArgs e){this.OnPaintBackground(e);使用 (var path = new GraphicsPath()){var c = e.Graphics.ClipBounds;var r = this.ClientRectangle;r.Inflate(-FlatAppearance.BorderSize, -FlatAppearance.BorderSize);path.AddEllipse(r);e.Graphics.SetClip(path);base.OnPaint(e);e.Graphics.SetClip(c);e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;如果(this.Checked){使用 (var p = new Pen(FlatAppearance.BorderColor,FlatAppearance.BorderSize)){e.Graphics.DrawEllipse(p, r);}}}}}

解决方案

我知道这是一个

示例

该示例的重要之处在于它完全是一个CheckBox 控件,支持使用鼠标和键盘进行检查,还支持数据绑定和CheckBox 的所有其他标准功能.代码并不完美,但对于使用是/否切换开关来说是一个很好的起点:

使用 System.Drawing;使用 System.Drawing.Drawing2D;使用 System.Windows.Forms;公共类 MyCheckBox : CheckBox{公共 MyCheckBox(){SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);填充 = 新填充(6);}protected override void OnPaint(PaintEventArgs e){this.OnPaintBackground(e);e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;使用 (var path = new GraphicsPath()){var d = Padding.All;var r = this.Height - 2 * d;path.AddArc(d, d, r, r, 90, 180);path.AddArc(this.Width - r - d, d, r, r, -90, 180);path.CloseFigure();e.Graphics.FillPath(Checked ? Brushes.DarkGray : Brushes.LightGray, path);r = 高度 - 1;var rect = 已检查?新矩形(宽度 - r - 1, 0, r, r): 新矩形(0, 0, r, r);e.Graphics.FillEllipse(Checked ? Brushes.Green : Brushes.WhiteSmoke, rect);}}}

I am designing a Toggle Switch control using CheckBox, but currently my control only draws a circle. How can I draw round shapes like below image and how can I change the location of the circle based on value of the control to represent checked and unchecked states like below image:

Here is my code:

public class MyCheckBox:CheckBox
{
    public MyCheckBox()
    {
        this.Appearance = System.Windows.Forms.Appearance.Button;
        this.BackColor = Color.Transparent;
        this.TextAlign = ContentAlignment.MiddleCenter;
        this.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.FlatAppearance.BorderColor = Color.RoyalBlue;
        this.FlatAppearance.BorderSize = 2;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        this.OnPaintBackground(e);
        using (var path = new GraphicsPath())
        {
            var c = e.Graphics.ClipBounds;
            var r = this.ClientRectangle;
            r.Inflate(-FlatAppearance.BorderSize, -FlatAppearance.BorderSize);
            path.AddEllipse(r);
            e.Graphics.SetClip(path);
            base.OnPaint(e);
            e.Graphics.SetClip(c);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            if (this.Checked)
            {
                using (var p = new Pen(FlatAppearance.BorderColor,
                                       FlatAppearance.BorderSize))
                {
                    e.Graphics.DrawEllipse(p, r);
                }
            }
        }
    }
}

解决方案

I know this is a question. But you may want to take a look at Toggle Switches or read more about Universal Windows App Components.

Anyway, here is an answer for Windows forms developers. It shows how we can customize rendering of a check box to have such appearance.

Currently you are drawing only an ellipse, it's quiet a toggle button. But if you want to show it like the below image, you should first draw a round shape for background, then based on Checked value draw the check circle. Using the codes in Example part of the answer you can have a CheckBox with such UI:

Example

The important thing about this sample is it's completely a CheckBox control and supports check using mouse and keyboard, also supports data-binding and all other standard features of CheckBox. The code is not perfect, but is a good start point to have a yes/no toggle switch:

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class MyCheckBox : CheckBox
{
    public MyCheckBox()
    {
        SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
        Padding = new Padding(6);
    }
    protected override void OnPaint(PaintEventArgs e)
    {
        this.OnPaintBackground(e);
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        using (var path = new GraphicsPath())
        {
            var d = Padding.All;
            var r = this.Height - 2 * d;
            path.AddArc(d, d, r, r, 90, 180);
            path.AddArc(this.Width - r - d, d, r, r, -90, 180);
            path.CloseFigure();
            e.Graphics.FillPath(Checked ? Brushes.DarkGray : Brushes.LightGray, path);
            r = Height - 1;
            var rect = Checked ? new Rectangle(Width - r - 1, 0, r, r)
                               : new Rectangle(0, 0, r, r);
            e.Graphics.FillEllipse(Checked ? Brushes.Green : Brushes.WhiteSmoke, rect);
        }
    }
}

这篇关于在 Windows 窗体中切换切换控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆