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;
}