我正在尝试使用C#重新创建一个Winform应用程序,具有与Windows提供的截图工具相同的功能。也就是说,允许用户在桌面上拖动一个矩形并捕捉其中的任何内容作为图像。
目前,我只能在winform中使用鼠标绘制一个矩形。有没有人可以指导我如何做到在整个桌面内实现这个功能?
我的绘制矩形的代码如下:
Rectangle rect;
public Form1()
{
InitializeComponent();
// set the cursor to be a + sign
this.Cursor = System.Windows.Forms.Cursors.Cross;
this.DoubleBuffered = true;
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
// e.X and e.Y are used to get the X and Y pos of the mouse
rect = new Rectangle(e.X, e.Y, 0, 0);
this.Invalidate();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// draw rectangle as mouse moves
rect = new Rectangle(rect.Left,rect.Top, e.X - rect.Left, e.Y - rect.Top);
}
this.Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
// Replace "Color.Red" with any color and repalce "2" with any size you like.
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, rect);
}
}
}
我已经在网上搜索了一些内容,但是还没有找到有用的资料。
如果有帮助,将不胜感激。