From c26e8724b9001af8306ab66d25283ce6931600ce Mon Sep 17 00:00:00 2001
From: jie65535 <840465812@qq.com>
Date: Fri, 8 Jan 2021 21:02:24 +0800
Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0UI=E6=A0=B7=E5=BC=8F=E5=85=AC?=
=?UTF-8?q?=E5=85=B1=E7=B1=BB=20=E5=A2=9E=E5=8A=A0=E4=BB=A3=E7=A0=81?=
=?UTF-8?q?=E9=98=9F=E5=88=97=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=AE=9E=E7=8E=B0?=
=?UTF-8?q?=E5=9F=BA=E6=9C=AC=E7=BB=93=E6=9E=84?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
CodeMatrix/CodeMatrix.csproj | 4 ++
CodeMatrix/Styles.cs | 30 ++++++++++++
CodeMatrix/UCCodeMatrix.cs | 89 ++++++++++++++++--------------------
CodeMatrix/UCCodeQueue.cs | 49 ++++++++++++++++++++
4 files changed, 123 insertions(+), 49 deletions(-)
create mode 100644 CodeMatrix/Styles.cs
create mode 100644 CodeMatrix/UCCodeQueue.cs
diff --git a/CodeMatrix/CodeMatrix.csproj b/CodeMatrix/CodeMatrix.csproj
index 875a9ec..078ea8d 100644
--- a/CodeMatrix/CodeMatrix.csproj
+++ b/CodeMatrix/CodeMatrix.csproj
@@ -54,9 +54,13 @@
+
Component
+
+ Component
+
FormMain.cs
diff --git a/CodeMatrix/Styles.cs b/CodeMatrix/Styles.cs
new file mode 100644
index 0000000..d3add40
--- /dev/null
+++ b/CodeMatrix/Styles.cs
@@ -0,0 +1,30 @@
+using System.Drawing;
+
+namespace CodeMatrix
+{
+ internal class Styles
+ {
+ public static Styles Default { get; } = new Styles();
+
+ public Styles()
+ {
+ Font = new Font("微软雅黑", 14);
+ BackColor = Color.FromArgb(18, 13, 25);
+ ForeColor = Color.FromArgb(208, 236, 92);
+ CodeBrush = new SolidBrush(Color.FromArgb(208, 236, 92));
+ EmptyCellBrush = new SolidBrush(Color.FromArgb(65, 52, 76));
+ DefaultLineBackColor = new SolidBrush(Color.FromArgb(42, 43, 60));
+ SelectLineBackColor = new SolidBrush(Color.FromArgb(32, 31, 28));
+ SelectCellBorderPen = new Pen(Color.FromArgb(109, 232, 228), 1);
+ }
+
+ public Font Font { get; set; }
+ public Color BackColor { get; set; }
+ public Color ForeColor { get; set; }
+ public Brush CodeBrush { get; set; }
+ public Brush EmptyCellBrush { get; set; }
+ public Brush DefaultLineBackColor { get; set; }
+ public Brush SelectLineBackColor { get; set; }
+ public Pen SelectCellBorderPen { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/CodeMatrix/UCCodeMatrix.cs b/CodeMatrix/UCCodeMatrix.cs
index cc97802..66320ee 100644
--- a/CodeMatrix/UCCodeMatrix.cs
+++ b/CodeMatrix/UCCodeMatrix.cs
@@ -1,18 +1,14 @@
using System;
-using System.Collections.Generic;
using System.Drawing;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
using System.Windows.Forms;
namespace CodeMatrix
{
- class UCCodeMatrix : Control
+ internal class UCCodeMatrix : Control
{
- static readonly byte[] Codes = new byte[] { 0x55, 0x1C, 0xBD, 0xE9, 0x7A };
+ private static readonly byte[] Codes = new byte[] { 0x55, 0x1C, 0xBD, 0xE9, 0x7A };
- static readonly Random Random = new Random();
+ private static readonly Random Random = new Random();
public SizeF CellSize { get; set; }
public int Columns { get; set; }
public int Rows { get; set; }
@@ -24,7 +20,7 @@ namespace CodeMatrix
///
/// 方向
///
- enum Directions
+ private enum Directions
{
///
/// 垂直方向
@@ -41,25 +37,30 @@ namespace CodeMatrix
public UCCodeMatrix()
{
- Font = new Font("微软雅黑", 14);
- BackColor = Color.FromArgb(18, 13, 25);
- ForeColor = Color.FromArgb(208, 236, 92);
- CodeBrush = new SolidBrush(ForeColor);
- EmptyCellBrush = new SolidBrush(Color.FromArgb(65, 52, 76));
- DefaultLineBackColor = new SolidBrush(Color.FromArgb(42, 43, 60));
- SelectLineBackColor = new SolidBrush(Color.FromArgb(32, 31, 28));
- SelectCellBorderPen = new Pen(Color.FromArgb(109, 232, 228), 1);
+ InitData();
+ InitComponent();
+ }
+
+ private void InitData()
+ {
Rows = 5;
Columns = 5;
CellSize = new SizeF(40, 40);
- Width = (int)(CellSize.Width * Columns + 100);
- Height = (int)(CellSize.Height * Rows + 10);
- Margin = Padding.Empty;
_currDir = Directions.Horizontal;
- DoubleBuffered = true;
}
- bool _IsLoaded;
+ private void InitComponent()
+ {
+ MinimumSize = new Size((int)(CellSize.Width * Columns + 100), (int)(CellSize.Height * Rows + 10));
+ Margin = Padding.Empty;
+ DoubleBuffered = true;
+
+ Font = Styles.Default.Font;
+ BackColor = Styles.Default.BackColor;
+ ForeColor = Styles.Default.ForeColor;
+ }
+
+ private bool _IsLoaded;
protected override void OnVisibleChanged(EventArgs e)
{
@@ -71,7 +72,7 @@ namespace CodeMatrix
base.OnVisibleChanged(e);
}
- void Load()
+ private void Load()
{
Matrix = new byte[Columns, Rows];
for (int col = 0; col < Columns; col++)
@@ -83,28 +84,15 @@ namespace CodeMatrix
CodeMatrixRect = new RectangleF(blockOffset, blockSize);
}
- protected override void OnForeColorChanged(EventArgs e)
- {
- CodeBrush = new SolidBrush(ForeColor);
- base.OnForeColorChanged(e);
- }
-
- Brush CodeBrush;
- Brush EmptyCellBrush;
- Brush DefaultLineBackColor;
- Brush SelectLineBackColor;
- Pen SelectCellBorderPen;
-
protected override void OnPaint(PaintEventArgs e)
{
if (!_IsLoaded) return;
var offset = new PointF(SelectPoint.X*CellSize.Width, SelectPoint.Y*CellSize.Height);
if (_currDir == Directions.Horizontal)
- e.Graphics.FillRectangle(DefaultLineBackColor, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
+ e.Graphics.FillRectangle(Styles.Default.DefaultLineBackColor, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
else if (_currDir == Directions.Vertical)
- e.Graphics.FillRectangle(DefaultLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
-
+ e.Graphics.FillRectangle(Styles.Default.DefaultLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
Cursor = Cursors.Default;
if (CursorPosition.X >= 0 && Matrix[CursorPosition.X, CursorPosition.Y] != 0)
@@ -113,8 +101,8 @@ namespace CodeMatrix
{
if (CursorPosition.Y == SelectPoint.Y)
{
- offset.X = CursorPosition.X*CellSize.Width;
- e.Graphics.FillRectangle(SelectLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
+ offset.X = CursorPosition.X * CellSize.Width;
+ e.Graphics.FillRectangle(Styles.Default.SelectLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height);
Cursor = Cursors.Hand;
}
}
@@ -122,8 +110,8 @@ namespace CodeMatrix
{
if (CursorPosition.X == SelectPoint.X)
{
- offset.Y = CursorPosition.Y*CellSize.Height;
- e.Graphics.FillRectangle(SelectLineBackColor, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
+ offset.Y = CursorPosition.Y * CellSize.Height;
+ e.Graphics.FillRectangle(Styles.Default.SelectLineBackColor, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height);
Cursor = Cursors.Hand;
}
}
@@ -132,24 +120,28 @@ namespace CodeMatrix
{
offset.X += CodeMatrixRect.X;
offset.Y += CodeMatrixRect.Y;
- e.Graphics.DrawRectangle(SelectCellBorderPen, offset.X, offset.Y, CellSize.Width-1, CellSize.Height - 1);
- e.Graphics.DrawRectangle(SelectCellBorderPen, offset.X+4, offset.Y+4, CellSize.Width-8 - 1, CellSize.Height-8 - 1);
+ e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, offset.X, offset.Y, CellSize.Width - 1, CellSize.Height - 1);
+ e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, offset.X + 4, offset.Y + 4, CellSize.Width - 8 - 1, CellSize.Height - 8 - 1);
}
}
-
for (int col = 0; col < Columns; col++)
{
for (int row = 0; row < Rows; row++)
{
var cellOffset = new PointF(col*CellSize.Width, row*CellSize.Height);
//var cellRect = new RectangleF(cellOffset, CellSize);
- Brush brush = CodeBrush;
- string code = Matrix[col, row].ToString("X2");
+ Brush brush;
+ string code;
if (Matrix[col, row] == 0)
{
code = "[ ]";
- brush = EmptyCellBrush;
+ brush = Styles.Default.EmptyCellBrush;
+ }
+ else
+ {
+ brush = Styles.Default.CodeBrush;
+ code = Matrix[col, row].ToString("X2");
}
var codeSize = e.Graphics.MeasureString(code, Font);
var codeOffset = new PointF((CellSize.Width-codeSize.Width)/2, (CellSize.Height-codeSize.Height)/2);
@@ -211,8 +203,7 @@ namespace CodeMatrix
}
}
-
base.OnMouseClick(e);
}
}
-}
+}
\ No newline at end of file
diff --git a/CodeMatrix/UCCodeQueue.cs b/CodeMatrix/UCCodeQueue.cs
new file mode 100644
index 0000000..b20f5a6
--- /dev/null
+++ b/CodeMatrix/UCCodeQueue.cs
@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Drawing;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows.Forms;
+
+namespace CodeMatrix
+{
+ class UCCodeQueue : Control
+ {
+ public SizeF CellSize { get; private set; }
+ public Padding CellMargin { get; set; }
+ public int BufferSize { get; set; }
+ public int CurrIndex { get; private set; }
+ public byte[] Buffer { get; } = new byte[32];
+
+ public UCCodeQueue()
+ {
+ InitData();
+ InitComponent();
+ }
+
+ private void InitData()
+ {
+ CellSize = new SizeF(40, 40);
+ BufferSize = 7;
+ CurrIndex = 0;
+ }
+
+ private void InitComponent()
+ {
+ Margin = Padding.Empty;
+ CellMargin = new Padding(3);
+ MinimumSize = new Size((int)((CellSize.Width + CellMargin.Horizontal) * BufferSize), (int)(CellSize.Height + CellMargin.Vertical));
+ DoubleBuffered = true;
+
+ Font = Styles.Default.Font;
+ BackColor = Styles.Default.BackColor;
+ ForeColor = Styles.Default.ForeColor;
+ }
+
+ protected override void OnPaint(PaintEventArgs e)
+ {
+ base.OnPaint(e);
+ }
+ }
+}