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<segment.Length; i++)
            {
                g.DrawString(segment[i], new Font("Yahei", 10f), new SolidBrush(Color.White), 5, 40+25*i);
                //g.DrawString(this.LatchCount, new Font("Yahei", 12f), new SolidBrush(Color.Blue), 100, 65);
            }
        }
    }

    #region Properties
    private string chipName;
    [Description("芯片名称"), Category("自定义")]
    public string ChipName
    {
        get { return chipName; }
        set { chipName = value; Invalidate(); }
    }

    private bool chipSwitch;
    [Description("芯片开关"), Category("自定义")]
    public bool ChipSwitch
    {
        get { return chipSwitch; }
        set { chipSwitch = value; Invalidate(); }
    }

    private string content;
    [Description("卡片内容"), Category("自定义")]
    public string Content
    {
        get { return  content; }
        set {  
            content = value;
            if(content != null)
            {
                segment = content.Split(',');
            }
            Invalidate();
        }
    }

    //private int statusColor;
    //[Description("背景颜色"), Category("自定义")]
    //public int StatusColor
    //{
    //    get { return statusColor; }
    //    set { 
    //        statusColor = value;
    //        switch (statusColor)
    //        {
    //            case 1:
    //                this.BackColor = Color.FromArgb(0xFFD965);
    //                break;
    //            case 2:
    //                this.BackColor = Color.FromArgb(0xC55A11); 
    //                break;
    //            default:
    //                this.BackColor = Color.FromArgb(0x70AD47);
    //                break;
    //        }
            
    //    }
    //}


    #endregion
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大浪淘沙胡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值