1、创建点阵控件
在控件库添加用户控件(Windows窗体),命名为MatrixArray;
在属性/布局栏将Size设置为680,700。
2、创建数据模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;namespace UserControlLib.Models
{public class UnitData{public string Address { get; set; }public int SingleBitCount { get; set; }public int MultiBitsCount { get; set; }public int Status { get; set; }}
}
3、修改MatrixArray.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;
using UserControlLib.Models;namespace UserControlLib
{public partial class MemoryArray : UserControl{private Color[] unitColor = new Color[3] { Color.Green, Color.Blue, Color.Red };private string[] unitStatus = new string[3] { "状态正常", "单位翻转", "多位翻转" };InfoTip userTip = new InfoTip();public MemoryArray(){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;userTip.Content = "地址:0x00000-0x000FF,单位翻转:100,多位翻转:200,X:100,Y:200";userTip.Location = new System.Drawing.Point(1100, 500);userTip.Name = "userTip1";userTip.Size = new System.Drawing.Size(150, 120);userTip.Visible = true;this.Controls.Add(userTip);}void UcSwitch_MouseDown(object sender, MouseEventArgs e){int x = e.X;int y = e.Y;int unitWidth = (this.Width - 40) / 64;int unitHeigh = (this.Height - 50) / 64;if (x < 25 || x > (this.Width - 20) || y < 25 || y > (this.Height - 30)){return;}int column = (x - 25) / unitWidth;if ((x - 25) % unitWidth != 0){column++;}int row = (y - 25) / unitHeigh;if ((y - 25) % unitHeigh != 0){row++;}int i = 64 * (row - 1) + column - 1;//MessageBox.Show($"地址: {Data[i].Address}\n单位翻转: {Data[i].SingleBitCount}\n多位翻转: {Data[i].MultiBitsCount}");userTip.Visible = true;userTip.Content = $"地址:{Data[i].Address},单位翻转:{Data[i].SingleBitCount},多位翻转:{Data[i].MultiBitsCount},X:{x},Y:{y}";int locationX = 25 + column * unitWidth;if ((locationX + userTip.Width) > (this.Width - 25)){locationX -= userTip.Width;}int locationY = 25 + row * unitHeigh;if ((locationY + userTip.Height) > (this.Height - 20)){locationY -= userTip.Height;}userTip.Location = new System.Drawing.Point(locationX, locationY);}protected override void OnPaint(PaintEventArgs e){base.OnPaint(e);var g = e.Graphics;g.SmoothingMode = SmoothingMode.AntiAlias;Console.WriteLine(this.Width);Console.WriteLine(this.Height);int unitWidth = (this.Width - 40) / 64;int unitHeigh = (this.Height - 50) / 64;// 绘制横坐标for (int i = 0; i < 9; i++){g.DrawString((8 * i).ToString(), new Font("Yahei", 10f), new SolidBrush(Color.Black), 20 + 8 * unitWidth * i, 5);}// 绘制纵坐标for (int i = 0; i < 9; i++){g.DrawString((8 * i).ToString(), new Font("Yahei", 10f), new SolidBrush(Color.Black), 5, 20 + 8 * unitHeigh * i);}// 绘制阵列for (int i = 0; i < 64; i++){for (int j = 0; j < 64; j++){SolidBrush sb = new SolidBrush(Color.Green);if (Data != null){int k = Data[i * 64 + j].Status;sb = new SolidBrush(unitColor[k]);}g.FillRectangle(sb, new Rectangle(25 + unitWidth * j + 1, 25 + unitHeigh * i + 1, unitWidth - 2, unitHeigh - 2));}}// 绘制标注int locationX = (this.Width - 360) / 2;int locationY = this.Height - 30;for (int i = 0; i < 3; i++){SolidBrush sb = new SolidBrush(unitColor[i]);g.FillRectangle(sb, new Rectangle(locationX + 120 * i, locationY, 15, 15));g.DrawString(unitStatus[i], new Font("Yahei", 10f), new SolidBrush(Color.Black), locationX + 120 * i + 20, locationY);}}#region Propertiesprivate List<UnitData> data;public List<UnitData> Data{get { return data; }set { data = value; Invalidate(); }}//private int errorNumber;//[Description("通道名称"), Category("自定义")]//public int ErrorNumber//{// get { return errorNumber; }// set// {// errorNumber = value;// string name = "button" + ErrorNumber;// Control[] control = this.Controls.Find(name, true);// Button button = (Button)control[0];// button.BackColor = Color.Red;// }//}#endregion}
}