diff --git a/CodeMatrix/FormMain.cs b/CodeMatrix/FormMain.cs index d9d5bd3..f4c8fc2 100644 --- a/CodeMatrix/FormMain.cs +++ b/CodeMatrix/FormMain.cs @@ -30,6 +30,10 @@ namespace CodeMatrix { Location = new Point(codeQueue.Location.X, y) }; + codeTargets[i].HoverCodeEvent += (_, value) => + { + codeMatrix.HighlightCode = value; + }; } codeMatrix.HoverValueChangedEvent += (_, value) => diff --git a/CodeMatrix/UCCodeMatrix.cs b/CodeMatrix/UCCodeMatrix.cs index 8ae7f61..cc7d487 100644 --- a/CodeMatrix/UCCodeMatrix.cs +++ b/CodeMatrix/UCCodeMatrix.cs @@ -26,6 +26,20 @@ namespace CodeMatrix } } public RectangleF CodeMatrixRect { get; private set; } + private byte _HighlightCode; + public byte HighlightCode + { + get => _HighlightCode; + set + { + if (_HighlightCode != value) + { + _HighlightCode = value; + Invalidate(); + } + } + } + public event EventHandler HoverValueChangedEvent; public event EventHandler CodeSelectedEvent; @@ -154,6 +168,14 @@ namespace CodeMatrix brush = Styles.Default.CodeBrush; code = Matrix[col, row].ToString("X2"); } + if (HighlightCode != 0 && HighlightCode == Matrix[col, row]) + { + e.Graphics.DrawRectangle(Styles.Default.SelectedCellBorderPen, + CodeMatrixRect.X+cellOffset.X+1, + CodeMatrixRect.Y+cellOffset.Y+1, + CellSize.Width - 3, + CellSize.Height - 3); + } 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); diff --git a/CodeMatrix/UCCodeTarget.cs b/CodeMatrix/UCCodeTarget.cs index de30884..38715bb 100644 --- a/CodeMatrix/UCCodeTarget.cs +++ b/CodeMatrix/UCCodeTarget.cs @@ -128,10 +128,46 @@ namespace CodeMatrix g.DrawString(codeStr, font, brush, codePoint); } + private int HoverIndex = -1; protected override void OnMouseMove(MouseEventArgs e) { - - //HoverCodeEvent?.Invoke(this, _HoverCode); + if (CurrState == State.Input) + { + int hoverIndex = -1; + //Point currPoint = new Point(e.X - Padding.Left, e.Y - Padding.Top - CellMargin.Top); + //if (currPoint.X >= 0 && currPoint.Y >= 0 && currPoint.Y < CellSize.Height) + //{ + // int cellfullwidth = CellSize.Width + CellMargin.Horizontal; + // int index = currPoint.X / cellfullwidth - OffsetIndex; + // int offset = currPoint.X % cellfullwidth; + // if (offset > CellMargin.Left && offset < cellfullwidth - CellMargin.Left) + // { + // if (index >= 0 && index < TargetLength) + // { + // hoverIndex = index; + // } + // } + //} + Point currPoint = new Point(e.X - Padding.Left, e.Y - Padding.Top); + if (currPoint.X >= 0 && currPoint.Y >= 0 && currPoint.Y < CellSize.Height) + { + int cellfullwidth = CellSize.Width + CellMargin.Horizontal; + int index = currPoint.X / cellfullwidth - OffsetIndex; + if (index >= 0 && index < TargetLength) + { + hoverIndex = index; + } + } + + if (HoverIndex != hoverIndex) + { + HoverIndex = hoverIndex; + if (HoverIndex >= 0) + HoverCodeEvent?.Invoke(this, TargetCodes[HoverIndex]); + else + HoverCodeEvent?.Invoke(this, 0); + } + } base.OnMouseMove(e); diff --git a/Images/CodeMatrix4.gif b/Images/CodeMatrix4.gif new file mode 100644 index 0000000..249cbee Binary files /dev/null and b/Images/CodeMatrix4.gif differ diff --git a/README.md b/README.md index 9c9aa07..ee9d328 100644 --- a/README.md +++ b/README.md @@ -4,4 +4,4 @@ 示例图片: -![演示动图](images/CodeMatrix3.gif) \ No newline at end of file +![演示动图](images/CodeMatrix4.gif) \ No newline at end of file