diff --git a/CodeMatrix.sln b/CodeMatrix.sln new file mode 100644 index 0000000..9deb291 --- /dev/null +++ b/CodeMatrix.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30804.86 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CodeMatrix", "CodeMatrix\CodeMatrix.csproj", "{D68B388B-9C99-47A4-A1CD-FA955C2905B5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D68B388B-9C99-47A4-A1CD-FA955C2905B5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D68B388B-9C99-47A4-A1CD-FA955C2905B5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D68B388B-9C99-47A4-A1CD-FA955C2905B5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D68B388B-9C99-47A4-A1CD-FA955C2905B5}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {9FA73845-2C28-4AA2-996C-FA28600CFBD9} + EndGlobalSection +EndGlobal diff --git a/CodeMatrix/App.config b/CodeMatrix/App.config new file mode 100644 index 0000000..56efbc7 --- /dev/null +++ b/CodeMatrix/App.config @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CodeMatrix/CodeMatrix.csproj b/CodeMatrix/CodeMatrix.csproj new file mode 100644 index 0000000..875a9ec --- /dev/null +++ b/CodeMatrix/CodeMatrix.csproj @@ -0,0 +1,86 @@ + + + + + Debug + AnyCPU + {D68B388B-9C99-47A4-A1CD-FA955C2905B5} + WinExe + CodeMatrix + CodeMatrix + v4.7.2 + 512 + true + true + + + AnyCPU + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + AnyCPU + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + + + + + + + + + + + + + + Form + + + FormMain.cs + + + + + Component + + + FormMain.cs + + + ResXFileCodeGenerator + Resources.Designer.cs + Designer + + + True + Resources.resx + + + SettingsSingleFileGenerator + Settings.Designer.cs + + + True + Settings.settings + True + + + + + + + \ No newline at end of file diff --git a/CodeMatrix/FormMain.Designer.cs b/CodeMatrix/FormMain.Designer.cs new file mode 100644 index 0000000..d56dffb --- /dev/null +++ b/CodeMatrix/FormMain.Designer.cs @@ -0,0 +1,53 @@ + +namespace CodeMatrix +{ + partial class FormMain + { + /// + /// 必需的设计器变量。 + /// + private System.ComponentModel.IContainer components = null; + + /// + /// 清理所有正在使用的资源。 + /// + /// 如果应释放托管资源,为 true;否则为 false。 + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows 窗体设计器生成的代码 + + /// + /// 设计器支持所需的方法 - 不要修改 + /// 使用代码编辑器修改此方法的内容。 + /// + private void InitializeComponent() + { + this.SuspendLayout(); + // + // FormMain + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.AutoSize = true; + this.BackColor = System.Drawing.Color.White; + this.ClientSize = new System.Drawing.Size(672, 323); + this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134))); + this.Margin = new System.Windows.Forms.Padding(0); + this.Name = "FormMain"; + this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; + this.Text = "Code Matrix"; + this.ResumeLayout(false); + + } + + #endregion + } +} + diff --git a/CodeMatrix/FormMain.cs b/CodeMatrix/FormMain.cs new file mode 100644 index 0000000..f7d9f3a --- /dev/null +++ b/CodeMatrix/FormMain.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace CodeMatrix +{ + public partial class FormMain : Form + { + public FormMain() + { + InitializeComponent(); + + var matrix = new UCCodeMatrix(); + Controls.Add(matrix); + ClientSize = matrix.Size; + } + + } +} diff --git a/CodeMatrix/FormMain.resx b/CodeMatrix/FormMain.resx new file mode 100644 index 0000000..1af7de1 --- /dev/null +++ b/CodeMatrix/FormMain.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CodeMatrix/Program.cs b/CodeMatrix/Program.cs new file mode 100644 index 0000000..c0991c0 --- /dev/null +++ b/CodeMatrix/Program.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace CodeMatrix +{ + static class Program + { + /// + /// 应用程序的主入口点。 + /// + [STAThread] + static void Main() + { + Application.EnableVisualStyles(); + Application.SetCompatibleTextRenderingDefault(false); + Application.Run(new FormMain()); + } + } +} diff --git a/CodeMatrix/Properties/AssemblyInfo.cs b/CodeMatrix/Properties/AssemblyInfo.cs new file mode 100644 index 0000000..7aedafd --- /dev/null +++ b/CodeMatrix/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// 有关程序集的一般信息由以下 +// 控制。更改这些特性值可修改 +// 与程序集关联的信息。 +[assembly: AssemblyTitle("CodeMatrix")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("CodeMatrix")] +[assembly: AssemblyCopyright("Copyright © 2020")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// 将 ComVisible 设置为 false 会使此程序集中的类型 +//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型 +//请将此类型的 ComVisible 特性设置为 true。 +[assembly: ComVisible(false)] + +// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID +[assembly: Guid("d68b388b-9c99-47a4-a1cd-fa955c2905b5")] + +// 程序集的版本信息由下列四个值组成: +// +// 主版本 +// 次版本 +// 生成号 +// 修订号 +// +//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值 +//通过使用 "*",如下所示: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/CodeMatrix/Properties/Resources.Designer.cs b/CodeMatrix/Properties/Resources.Designer.cs new file mode 100644 index 0000000..a91a582 --- /dev/null +++ b/CodeMatrix/Properties/Resources.Designer.cs @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// 此代码由工具生成。 +// 运行时版本: 4.0.30319.42000 +// +// 对此文件的更改可能导致不正确的行为,如果 +// 重新生成代码,则所做更改将丢失。 +// +//------------------------------------------------------------------------------ + + +namespace CodeMatrix.Properties +{ + /// + /// 强类型资源类,用于查找本地化字符串等。 + /// + // 此类是由 StronglyTypedResourceBuilder + // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。 + // 若要添加或删除成员,请编辑 .ResX 文件,然后重新运行 ResGen + // (以 /str 作为命令选项),或重新生成 VS 项目。 + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class Resources + { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal Resources() + { + } + + /// + /// 返回此类使用的缓存 ResourceManager 实例。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager + { + get + { + if ((resourceMan == null)) + { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("CodeMatrix.Properties.Resources", typeof(Resources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// 重写当前线程的 CurrentUICulture 属性,对 + /// 使用此强类型资源类的所有资源查找执行重写。 + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture + { + get + { + return resourceCulture; + } + set + { + resourceCulture = value; + } + } + } +} diff --git a/CodeMatrix/Properties/Resources.resx b/CodeMatrix/Properties/Resources.resx new file mode 100644 index 0000000..af7dbeb --- /dev/null +++ b/CodeMatrix/Properties/Resources.resx @@ -0,0 +1,117 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/CodeMatrix/Properties/Settings.Designer.cs b/CodeMatrix/Properties/Settings.Designer.cs new file mode 100644 index 0000000..51b64e9 --- /dev/null +++ b/CodeMatrix/Properties/Settings.Designer.cs @@ -0,0 +1,29 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +namespace CodeMatrix.Properties +{ + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")] + internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase + { + + private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); + + public static Settings Default + { + get + { + return defaultInstance; + } + } + } +} diff --git a/CodeMatrix/Properties/Settings.settings b/CodeMatrix/Properties/Settings.settings new file mode 100644 index 0000000..3964565 --- /dev/null +++ b/CodeMatrix/Properties/Settings.settings @@ -0,0 +1,7 @@ + + + + + + + diff --git a/CodeMatrix/UCCodeMatrix.cs b/CodeMatrix/UCCodeMatrix.cs new file mode 100644 index 0000000..cc97802 --- /dev/null +++ b/CodeMatrix/UCCodeMatrix.cs @@ -0,0 +1,218 @@ +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 + { + static readonly byte[] Codes = new byte[] { 0x55, 0x1C, 0xBD, 0xE9, 0x7A }; + + static readonly Random Random = new Random(); + public SizeF CellSize { get; set; } + public int Columns { get; set; } + public int Rows { get; set; } + public byte[,] Matrix { get; set; } + public Point SelectPoint { get; private set; } + public Point CursorPosition { get; private set; } + public RectangleF CodeMatrixRect { get; private set; } + + /// + /// 方向 + /// + enum Directions + { + /// + /// 垂直方向 + /// + Vertical, + + /// + /// 水平方向 + /// + Horizontal, + } + + private Directions _currDir; + + 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); + 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; + + protected override void OnVisibleChanged(EventArgs e) + { + if (Visible && !_IsLoaded) + { + Load(); + _IsLoaded = true; + } + base.OnVisibleChanged(e); + } + + void Load() + { + Matrix = new byte[Columns, Rows]; + for (int col = 0; col < Columns; col++) + for (int row = 0; row < Rows; row++) + Matrix[col, row] = Codes[Random.Next(Codes.Length)]; + + var blockSize = new SizeF(Columns*CellSize.Width, Rows*CellSize.Height); + var blockOffset = new PointF((Width-blockSize.Width)/2, (Height-blockSize.Height)/2); + 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); + else if (_currDir == Directions.Vertical) + e.Graphics.FillRectangle(DefaultLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height); + + + Cursor = Cursors.Default; + if (CursorPosition.X >= 0 && Matrix[CursorPosition.X, CursorPosition.Y] != 0) + { + if (_currDir == Directions.Horizontal) + { + if (CursorPosition.Y == SelectPoint.Y) + { + offset.X = CursorPosition.X*CellSize.Width; + e.Graphics.FillRectangle(SelectLineBackColor, offset.X + CodeMatrixRect.X, 0, CellSize.Width, Height); + Cursor = Cursors.Hand; + } + } + else if (_currDir == Directions.Vertical) + { + if (CursorPosition.X == SelectPoint.X) + { + offset.Y = CursorPosition.Y*CellSize.Height; + e.Graphics.FillRectangle(SelectLineBackColor, 0, offset.Y + CodeMatrixRect.Y, Width, CellSize.Height); + Cursor = Cursors.Hand; + } + } + + if (Cursor == Cursors.Hand) + { + 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); + } + } + + + 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"); + if (Matrix[col, row] == 0) + { + code = "[ ]"; + brush = EmptyCellBrush; + } + var codeSize = e.Graphics.MeasureString(code, Font); + var codeOffset = new PointF((CellSize.Width-codeSize.Width)/2, (CellSize.Height-codeSize.Height)/2); + var codePoint = new PointF(codeOffset.X+cellOffset.X+CodeMatrixRect.X, codeOffset.Y+cellOffset.Y+CodeMatrixRect.Y); + e.Graphics.DrawString(code, Font, brush, codePoint); + } + } + } + + protected override void OnMouseMove(MouseEventArgs e) + { + if (CodeMatrixRect.Left <= e.X + && CodeMatrixRect.Top <= e.Y + && CodeMatrixRect.Right > e.X + && CodeMatrixRect.Bottom > e.Y) + { + var offset = new PointF(e.X-CodeMatrixRect.X, e.Y-CodeMatrixRect.Y); + var current = new Point((int)(offset.X / CellSize.Width), (int)(offset.Y / CellSize.Height)); + if (CursorPosition != current) + { + CursorPosition = current; + Invalidate(); + } + } + else + { + if (CursorPosition.X >= 0) + { + CursorPosition = new Point(-1, -1); + Invalidate(); + } + } + base.OnMouseMove(e); + } + + protected override void OnMouseClick(MouseEventArgs e) + { + if (CursorPosition.X >= 0 && Matrix[CursorPosition.X, CursorPosition.Y] != 0) + { + if (_currDir == Directions.Horizontal) + { + if (CursorPosition.Y == SelectPoint.Y) + { + Matrix[CursorPosition.X, CursorPosition.Y] = 0; + _currDir = Directions.Vertical; + SelectPoint = CursorPosition; + Invalidate(); + } + } + else if (_currDir == Directions.Vertical) + { + if (CursorPosition.X == SelectPoint.X) + { + Matrix[CursorPosition.X, CursorPosition.Y] = 0; + _currDir = Directions.Horizontal; + SelectPoint = CursorPosition; + Invalidate(); + } + } + } + + + base.OnMouseClick(e); + } + } +}