这是为了实现在项目中的功能实现的一个Demo,在完成这个Demo后,我将其直接移植到了项目中,进行数据合并后,一切完美运行,
废话不多说,直接上图:
这里给出部分主要代码,
构造函数:
public uc_layout() { InitializeComponent(); InitializeDrawResources(); StartRow = startRow; DrawAll(); this.SizeChanged += (s, w) => { DrawAll(); }; this.MouseDown += (s, e) => { lastRow = e.Y / BlockHeight - 1+startRow;//确定行的位置 lastCol = e.X / BlockWidth - 1; if (lastCol >= 0 && lastCol <= 7&&lastRow>=0&&lastRow<=63) { int index = 8 * (lastRow) + lastCol;//确定信号的startbit if (list.Contains(index))//此处判断是否按下了信号 { startBit = list.Min();//若是,则获取按下的信号 MouseIsDown = true; DrawAll(); } } }; this.MouseUp += (s, e) => { MouseIsDown = false; this.Cursor = Cursors.Default; }; this.MouseMove += uc_layout_MouseMove; }
鼠标移动时,数据重绘,用MouseMove事件来控制,如下:
void uc_layout_MouseMove(object sender, MouseEventArgs e) { int mouseRow = e.Y / BlockHeight - 1+startRow;//确定鼠标移动到的行 int mouseCol = e.X / BlockWidth - 1; if (MouseIsDown) { if (mouseRow!=lastRow||mouseCol!=lastCol) { //lastRow = 7; //lastCol = 6; int difRow = mouseRow - lastRow; int difCol = mouseCol - lastCol; //int oldFirst = list.Min(); //int newFirst = oldFirst + 8 * difRow + difCol; int newFirst = startBit + 8 * difRow + difCol; lastRow = mouseRow; lastCol = mouseCol; if (this.Cursor!=Cursors.SizeAll) { this.Cursor = Cursors.SizeAll; } startBit = newFirst; //if (startBit + list.Count > (startRow + 8) * 8 - 1) if (startBit > (startRow + 8) * 8 - 1) { if (startRow<maxRow-8) { startRow++; } } else if (startBit<startRow*8) { if (startRow>0) { startRow--; } } list.Clear(); for (int i = 0; i < 6; i++) { list.Add(startBit + i); } DrawAll(); } } }
以及画笔初始化代码
GrayBrush_G = new SolidBrush(Color.FromArgb(200, 200, 200)); OrangeBrush_G = new SolidBrush(Color.FromArgb(190, 165, 210)); DeepGrayBrush_G = new SolidBrush(Color.FromArgb(50, 50, 50)); BlackBrush_G = new SolidBrush(Color.Black); BigFont_G = new Font("宋体", 12, FontStyle.Bold); SmallFont_G = new Font("宋体", 10); OverLayFont_G = new Font("宋体", 10, FontStyle.Bold); GrayPen_G = new Pen(Color.FromArgb(200, 200, 200), 1); BlackPen_G = new Pen(Color.FromArgb(0, 0, 0), 1); CenterSF_G = new StringFormat() { LineAlignment = StringAlignment.Center, Alignment = StringAlignment.Center, FormatFlags = StringFormatFlags.NoWrap, Trimming = StringTrimming.Character };
想要进阶学习的,可以在这里C# GDI+实现网格绘制,并显示内容 进行下载,项目编译通过,vs2012编写,下载有问题的,可以联系我。
这里给出得是升级版,通过滚动条来控制显示的行数,使绘制的表格动态刷新,可下载得代码中包含前三篇中的代码案例。