当前位置: 首页 > news >正文

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 Propertiesprivate 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
}

}

http://www.lryc.cn/news/225171.html

相关文章:

  • 我用Devchat开发了公务员报名确认系统自动登录脚本,再也不用担心挤不进去了
  • 如何低门槛开发有趣实用的ZigBee产品?
  • ChatGPT和API发生重大中断!
  • SQL第五次上机实验
  • Matplotlib数据可视化综合应用Matplotlib图形配置在线闯关_头歌实践教学平台
  • CSS实现瀑布流的两种方式
  • Hadoop 视频分析系统
  • Flutter android和ios闪屏页配置
  • 30道高频Vue面试题快问快答
  • vue-前端实现模糊查询
  • QT:tcpSocket 报错The proxy type is invalid for this operation
  • PostgreSQL 技术内幕(十一)位图扫描
  • C# WebSocket 服务器
  • 自动化实战 - 测试个人博客系统
  • TCP/IP详解
  • 2023年的低代码:数字化、人工智能、趋势及未来展望
  • 【gogogo专栏】golang并发编程
  • 深入理解JVM虚拟机第二十二篇:详解JVM当中与操作数栈相关的字节码指令
  • Vue报错解决Error in v-on handler: “Error: 无效的节点选择器:#div1“
  • R | R包安装报错-github连接速度慢或无法访问 | metaboanalystR | Retip | rJava安装
  • 博阳精讯、凡得科技访问上海斯歌:共探BPM流程服务新高地
  • 响应式艺术作品展示前端html网站模板源码
  • 大语言模型(LLM)综述(六):大型语言模型的基准和评估
  • 【Python自学笔记】Flask调教方法Internel Server Error
  • 【AICFD案例教程】汽车外气动-AI加速
  • P1547 [USACO05MAR] Out of Hay S 题解
  • 2023.11.10联测总结
  • C++:list?自己模拟实现!
  • layui table合并相同的列
  • 【Spring】SpringBoot配置文件