2,建立截圖主窗口
核心類MyRectangle已經完成,剩下的工作就是使用改類實現預想的截圖功能。
用VS2005 新建Project,命名為ScreenCutter。將主窗口命名為MainForm,新建一個窗口命名為ScreenBody,將其 ShowInTaskbar屬性設置為False,TopMost屬性設置為True,FormBorderStyle屬性設置為None,在 ScreenBody上添加一個panel控件panel1,設置BackColor屬性為藍色,在panel1上添加相應個數的label,如 labelLocation、labelWidth、labelHeight等,用於指示當前選區位置和大小,panel1最終樣式為:
修改ScreenBody的引用命名空間為:
using System;
using System.Drawing;
using System.Windows.Forms;
在ScreenBody類中添加如下私有成員:
private Graphics MainPainter; //the main painter
private bool isDowned; //check whether the mouse is down
private bool RectReady; //check whether the rectangle is finished
private Image baseImage; //the back ground of the screen
private Point moveModeDownPoint; //the mouse location when you move the rectangle
private MyRectangle myRectangle; //the rectangle
private bool moveMode; //check whether the rectangle is on move mode or not
private bool changeSizeMode; //check whether the rectangle is on change size mode or not
修改ScreenBody構造函數:
public ScreenBody()
{
InitializeComponent();
panel1.Location = new Point(this.Left, this.Top);
myRectangle = new MyRectangle();
moveModeDownPoint = new Point();
this.Cursor = myRectangle.MyCursor;
}