在 WinForm 应用程序中绘制选择区域 [英] Draw selection Area in WinForm Application
问题描述
在我的 WinForm 中,我需要在屏幕上绘制选择区域.用户应该能够在角落或边框上拖动选定的矩形来调整大小.如下:
In my WinForm I need to draw selection area on the screen. User should be able to drag selected rectangle on corners or border to resize. As below:
我可以用实心画笔绘制矩形.从边框或角落拖动时如何使其可调整大小?
I could draw the rectangle with solid brush. How can I make it resizeable when dragging from border or corner?
private void panel1_MouseDown(object sender, MouseEventArgs e) {
using (Graphics g = this.panel1.CreateGraphics()) {
Pen pen = new Pen(Color.Black, 2);
Brush brush = new SolidBrush(this.panel1.BackColor);
g.FillRectangle(brush, this.panel1.Bounds);
g.DrawRectangle(pen, e.X, e.Y, 20, 20);
pen.Dispose();
brush.Dispose();
}
}
推荐答案
我认为您可以使用 MouseMove
代替 MouseDown
.
Instead of using MouseDown
, I think you can use MouseMove
.
首先,这个事件检查左键是否被按下:
At first, this event checks if left button is pressed:
if(e.Button == MouseButtons.Left)) {
//Check for the position of your mouse
}
现在您将放置一些 if
语句以确保您将根据您的小调整框重新绘制矩形.每个 if
都会重新计算矩形,删除旧的并绘制最新的.希望这会有所帮助.
Now you will put some if
statement to be sure you will redraw your rectangle depending on your little resize boxes. Every if
will recalculate the rectangle, erasing the old one and plotting the newest.
Hope this helps.
这篇关于在 WinForm 应用程序中绘制选择区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!