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