mirror of
https://github.com/jie65535/CodeMatrix.git
synced 2025-07-31 18:19:13 +08:00
优化 重绘逻辑,减少重绘次数
This commit is contained in:
parent
c26e8724b9
commit
532909ed1a
@ -10,7 +10,7 @@ using System.Runtime.InteropServices;
|
|||||||
[assembly: AssemblyConfiguration("")]
|
[assembly: AssemblyConfiguration("")]
|
||||||
[assembly: AssemblyCompany("")]
|
[assembly: AssemblyCompany("")]
|
||||||
[assembly: AssemblyProduct("CodeMatrix")]
|
[assembly: AssemblyProduct("CodeMatrix")]
|
||||||
[assembly: AssemblyCopyright("Copyright © 2020")]
|
[assembly: AssemblyCopyright("© 2020 jie65535@github.com")]
|
||||||
[assembly: AssemblyTrademark("")]
|
[assembly: AssemblyTrademark("")]
|
||||||
[assembly: AssemblyCulture("")]
|
[assembly: AssemblyCulture("")]
|
||||||
|
|
||||||
|
@ -14,7 +14,8 @@ namespace CodeMatrix
|
|||||||
public int Rows { get; set; }
|
public int Rows { get; set; }
|
||||||
public byte[,] Matrix { get; set; }
|
public byte[,] Matrix { get; set; }
|
||||||
public Point SelectPoint { get; private set; }
|
public Point SelectPoint { get; private set; }
|
||||||
public Point CursorPosition { get; private set; }
|
public Point CursorPoint { get; private set; }
|
||||||
|
public Point HoverPoint { get; private set; }
|
||||||
public RectangleF CodeMatrixRect { get; private set; }
|
public RectangleF CodeMatrixRect { get; private set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -43,10 +44,11 @@ namespace CodeMatrix
|
|||||||
|
|
||||||
private void InitData()
|
private void InitData()
|
||||||
{
|
{
|
||||||
Rows = 5;
|
Rows = 9;
|
||||||
Columns = 5;
|
Columns = 16;
|
||||||
CellSize = new SizeF(40, 40);
|
CellSize = new SizeF(40, 40);
|
||||||
_currDir = Directions.Horizontal;
|
_currDir = Directions.Horizontal;
|
||||||
|
HoverPoint = new Point(-1, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void InitComponent()
|
private void InitComponent()
|
||||||
@ -84,9 +86,11 @@ namespace CodeMatrix
|
|||||||
CodeMatrixRect = new RectangleF(blockOffset, blockSize);
|
CodeMatrixRect = new RectangleF(blockOffset, blockSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//int _paintCount;
|
||||||
protected override void OnPaint(PaintEventArgs e)
|
protected override void OnPaint(PaintEventArgs e)
|
||||||
{
|
{
|
||||||
if (!_IsLoaded) return;
|
if (!_IsLoaded) return;
|
||||||
|
//_paintCount++;
|
||||||
|
|
||||||
var offset = new PointF(SelectPoint.X*CellSize.Width, SelectPoint.Y*CellSize.Height);
|
var offset = new PointF(SelectPoint.X*CellSize.Width, SelectPoint.Y*CellSize.Height);
|
||||||
if (_currDir == Directions.Horizontal)
|
if (_currDir == Directions.Horizontal)
|
||||||
@ -94,35 +98,28 @@ namespace CodeMatrix
|
|||||||
else if (_currDir == Directions.Vertical)
|
else if (_currDir == Directions.Vertical)
|
||||||
e.Graphics.FillRectangle(Styles.Default.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 (HoverPoint.X >= 0)
|
||||||
if (CursorPosition.X >= 0 && Matrix[CursorPosition.X, CursorPosition.Y] != 0)
|
|
||||||
{
|
{
|
||||||
|
Cursor = Cursors.Hand;
|
||||||
if (_currDir == Directions.Horizontal)
|
if (_currDir == Directions.Horizontal)
|
||||||
{
|
{
|
||||||
if (CursorPosition.Y == SelectPoint.Y)
|
offset.X = HoverPoint.X * CellSize.Width;
|
||||||
{
|
e.Graphics.FillRectangle(Styles.Default.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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (_currDir == Directions.Vertical)
|
else if (_currDir == Directions.Vertical)
|
||||||
{
|
{
|
||||||
if (CursorPosition.X == SelectPoint.X)
|
offset.Y = HoverPoint.Y * CellSize.Height;
|
||||||
{
|
e.Graphics.FillRectangle(Styles.Default.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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Cursor == Cursors.Hand)
|
offset.X += CodeMatrixRect.X;
|
||||||
{
|
offset.Y += CodeMatrixRect.Y;
|
||||||
offset.X += CodeMatrixRect.X;
|
e.Graphics.DrawRectangle(Styles.Default.SelectCellBorderPen, offset.X, offset.Y, CellSize.Width - 1, CellSize.Height - 1);
|
||||||
offset.Y += CodeMatrixRect.Y;
|
e.Graphics.DrawRectangle(Styles.Default.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);
|
else
|
||||||
}
|
{
|
||||||
|
Cursor = Cursors.Default;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int col = 0; col < Columns; col++)
|
for (int col = 0; col < Columns; col++)
|
||||||
@ -149,6 +146,8 @@ namespace CodeMatrix
|
|||||||
e.Graphics.DrawString(code, Font, brush, codePoint);
|
e.Graphics.DrawString(code, Font, brush, codePoint);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//e.Graphics.DrawString(_paintCount.ToString(), Font, Styles.Default.CodeBrush, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected override void OnMouseMove(MouseEventArgs e)
|
protected override void OnMouseMove(MouseEventArgs e)
|
||||||
@ -160,17 +159,30 @@ namespace CodeMatrix
|
|||||||
{
|
{
|
||||||
var offset = new PointF(e.X-CodeMatrixRect.X, e.Y-CodeMatrixRect.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));
|
var current = new Point((int)(offset.X / CellSize.Width), (int)(offset.Y / CellSize.Height));
|
||||||
if (CursorPosition != current)
|
if (CursorPoint != current)
|
||||||
{
|
{
|
||||||
CursorPosition = current;
|
CursorPoint = current;
|
||||||
Invalidate();
|
var hoverPoint = new Point(-1, -1);
|
||||||
|
if (Matrix[CursorPoint.X, CursorPoint.Y] != 0)
|
||||||
|
{
|
||||||
|
if (_currDir == Directions.Horizontal && CursorPoint.Y == SelectPoint.Y)
|
||||||
|
hoverPoint = CursorPoint;
|
||||||
|
else if (_currDir == Directions.Vertical && CursorPoint.X == SelectPoint.X)
|
||||||
|
hoverPoint = CursorPoint;
|
||||||
|
}
|
||||||
|
if (HoverPoint != hoverPoint)
|
||||||
|
{
|
||||||
|
HoverPoint = hoverPoint;
|
||||||
|
Invalidate();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (CursorPosition.X >= 0)
|
if (HoverPoint.X >= 0)
|
||||||
{
|
{
|
||||||
CursorPosition = new Point(-1, -1);
|
HoverPoint = CursorPoint = new Point(-1, -1);
|
||||||
Invalidate();
|
Invalidate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -179,28 +191,13 @@ namespace CodeMatrix
|
|||||||
|
|
||||||
protected override void OnMouseClick(MouseEventArgs e)
|
protected override void OnMouseClick(MouseEventArgs e)
|
||||||
{
|
{
|
||||||
if (CursorPosition.X >= 0 && Matrix[CursorPosition.X, CursorPosition.Y] != 0)
|
if (HoverPoint.X >= 0)
|
||||||
{
|
{
|
||||||
if (_currDir == Directions.Horizontal)
|
SelectPoint = HoverPoint;
|
||||||
{
|
HoverPoint = new Point(-1, -1);
|
||||||
if (CursorPosition.Y == SelectPoint.Y)
|
Matrix[SelectPoint.X, SelectPoint.Y] = 0;
|
||||||
{
|
_currDir = _currDir == Directions.Horizontal ? Directions.Vertical : Directions.Horizontal;
|
||||||
Matrix[CursorPosition.X, CursorPosition.Y] = 0;
|
Invalidate();
|
||||||
_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);
|
base.OnMouseClick(e);
|
||||||
|
Loading…
Reference in New Issue
Block a user