简体   繁体   English

在 Windows 窗体中切换切换控件

[英]Toggle Switch Control in Windows Forms

I am designing a Toggle Switch control using CheckBox , but currently my control only draws a circle.我正在使用CheckBox设计一个 Toggle Switch CheckBox ,但目前我的控件只绘制一个圆圈。 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 .但您可能需要查看Toggle Switches或阅读有关Universal Windows App Components 的更多信息。

Anyway, here is an answer for Windows forms developers.无论如何,这是 Windows 窗体开发人员的答案。 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.但是如果你想像下图一样显示它,你应该先画一个圆形作为背景,然后根据Checked值绘制选中的圆圈。 Using the codes in Example part of the answer you can have a CheckBox with such UI:使用答案示例部分中的代码,您可以拥有一个带有此类 UI 的CheckBox

在此处输入图片说明

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 .这个示例的重要之处在于它完全是一个CheckBox控件,支持使用鼠标和键盘进行检查,还支持数据绑定和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);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM