我試圖在C#中重新創建一個Winform應用程序,該應用程序與剪切工具窗口提供的功能相同。也就是說,允許用戶在桌面上拖動一個矩形並捕捉圖像中的內容。桌面屏幕捕獲目標區域
此刻我只能用鼠標繪製一個矩形,這是在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);
}
}
}
我一直在網上四處尋找,但我的搜索還不能提供使用的任何東西。
任何幫助將不勝感激。
另請參閱[Greenshot]的源代碼(http://sourceforge.net/projects/greenshot/) – ja72