C# Winform 自定义带SWITCH的卡片

1、创建卡片用户控件
在控件库中添加用户控件(Windows窗体),命名为Card;
在属性/布局栏设置Size为148,128.
2、修改Card.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControlLib
{
public partial class Card : UserControl
{
public Card()
{
InitializeComponent();
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
this.SetStyle(ControlStyles.DoubleBuffer, true);
this.SetStyle(ControlStyles.ResizeRedraw, true);
this.SetStyle(ControlStyles.Selectable, true);
this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.MouseDown += UcSwitch_MouseDown;
}

    [Description("开关切换"), Category("自定义")]
    public event EventHandler CheckedChanged;
    void UcSwitch_MouseDown(object sender, MouseEventArgs e)
    {
        int xMin = switchLocationX - swichArcDiameter;
        int xMax = switchLocationX + swichLineLen + swichArcDiameter;
        int yMin= switchLocationY;
        int yMax = switchLocationY + swichArcDiameter;
        if (e.X > xMin && e.X < xMax && e.Y > yMin && e.Y < yMax)
        {
            ChipSwitch = !ChipSwitch;
            if (CheckedChanged != null)
            {
                CheckedChanged(this, EventArgs.Empty);
            }
        }
    }

    private const int SWITCH_ARC_DIAMETER = 24;
    private const int SWITCH_LINE_LEN = 15;
    private const int SWITCH_LOCATION_X = 85;
    private const int SWITCH_LOCATION_Y = 5;

    int swichArcDiameter = SWITCH_ARC_DIAMETER;
    int swichLineLen = SWITCH_LINE_LEN;
    int switchLocationX = SWITCH_LOCATION_X;
    int switchLocationY = SWITCH_LOCATION_Y;

    string[] segment;

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        
        var g = e.Graphics;
        g.SmoothingMode=SmoothingMode.AntiAlias;
        g.DrawString(this.ChipName,new Font("Yahei", 12f), new SolidBrush(Color.White),5,5);

        var fillColor = this.ChipSwitch ? Color.FromArgb(34, 163, 169) : Color.FromArgb(111, 122, 126);
        GraphicsPath path = new GraphicsPath();
        
        path.AddLine(new Point(switchLocationX, switchLocationY), new Point(switchLocationX + swichLineLen, switchLocationY));
        //g.DrawPath(new Pen(Color.Red,2),path);
        path.AddArc(new Rectangle(switchLocationX + swichLineLen, switchLocationY, swichArcDiameter, swichArcDiameter), -90, 180);
        //g.DrawPath(new Pen(Color.Red, 2), path);
        path.AddLine(new Point(switchLocationX + swichLineLen, switchLocationY + swichArcDiameter), new Point(switchLocationX, switchLocationY + swichArcDiameter));
        //g.DrawPath(new Pen(Color.Red, 2), path);
        path.AddArc(new Rectangle(switchLocationX - swichArcDiameter, switchLocationY, swichArcDiameter, swichArcDiameter), 90, 180);
        //g.DrawPath(new Pen(Color.Red, 2), path);
        g.FillPath(new SolidBrush(fillColor), path);
        int switchCircleDiameter = swichArcDiameter - 4;
        int switchTextY = (swichArcDiameter - 10) / 2+ switchLocationY;
        if (this.ChipSwitch)
        {
            g.FillEllipse(Brushes.White, new Rectangle(switchLocationX + swichLineLen+2, switchLocationY+2, switchCircleDiameter, switchCircleDiameter));
            g.DrawString("开", new Font("Yahei", 10f), Brushes.White, new Point(switchLocationX, switchTextY));
        }
        else
        {
            g.FillEllipse(Brushes.White, new Rectangle(switchLocationX - swichArcDiameter+2, switchLocationY+2, switchCircleDiameter, switchCircleDiameter));
            g.DrawString("关", new Font("Yahei", 10f), Brushes.White, new Point(switchLocationX, switchTextY));
        }
        if (segment!=null && segment.Length>0)
        {
            for(int i=0;i

}

你可能感兴趣的:(C#,c#,开发语言,卡片控件)