Skip to content

Commit abb90d6

Browse files
committed
方向键移动范围区域
1 parent ab9a9f2 commit abb90d6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

DrawKit/Shapes/RectangularSelection.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Drawing.Drawing2D;
66
using System.Drawing.Imaging;
77
using System.Linq;
8+
using System.Runtime.InteropServices;
89
using System.Text;
910
using System.Threading.Tasks;
1011
using System.Windows.Forms;
@@ -16,6 +17,10 @@ namespace DrawKit.Shapes
1617
/// </summary>
1718
public class RectangularSelection : Shape
1819
{
20+
[DllImport("user32.dll")]
21+
private static extern short GetAsyncKeyState(Keys vKey);
22+
23+
private int _moveStep = 2;
1924
private Bitmap _selectedBitmap;
2025
private Bitmap _copySelectedBitmap;
2126
private Rectangle _rectBeforeAdjust;
@@ -445,6 +450,27 @@ public override void KeyDown(KeyEventArgs e)
445450
{
446451
PasteBitmapFromClipboard();
447452
}
453+
else
454+
{
455+
bool isUp = (GetAsyncKeyState(Keys.Up) & 0x8000) != 0;
456+
bool isDown = (GetAsyncKeyState(Keys.Down) & 0x8000) != 0;
457+
bool isLeft = (GetAsyncKeyState(Keys.Left) & 0x8000) != 0;
458+
bool isRight = (GetAsyncKeyState(Keys.Right) & 0x8000) != 0;
459+
460+
int dx = 0, dy = 0;
461+
462+
if (isLeft && !isRight) dx = -_moveStep;
463+
else if (isRight && !isLeft) dx = _moveStep;
464+
465+
if (isUp && !isDown) dy = -_moveStep;
466+
else if (isDown && !isUp) dy = _moveStep;
467+
468+
if (dx != 0 || dy != 0)
469+
{
470+
MoveSelectedArea(dx, dy);
471+
}
472+
}
473+
448474
}
449475

450476
private void CopySelectionRectToClipboard()
@@ -515,5 +541,14 @@ private void PasteBitmapFromClipboard()
515541
panel.Refresh();
516542
}
517543

544+
private void MoveSelectedArea(int deltaX,int deltaY)
545+
{
546+
if (SelectionRect == Rectangle.Empty) return;
547+
if (_selectedBitmap == null) return;
548+
drawStatus = DrawStatus.CanMove;
549+
SelectionRect.Offset(deltaX, deltaY);
550+
panel.Invalidate();
551+
}
552+
518553
}
519554
}

0 commit comments

Comments
 (0)