概述:
在公司,不能自己安装软件,也不能下载,但有时候需要截图。用PrintScreen键只能截取全屏,感觉很麻烦。于是决定自己编写一个截图工具 。
众所周知,QQ截图首先将截取全屏为一个图片,然后用在这个图片基础上截取需要的部分。本程序实现方法类似。
程序运行截图如下:

图中心矩形为即将截取区域
程序很粗糙,希望大家提出宝贵意见。
1,自定义矩形类MyRectangle
在QQ截图程序中,用户用鼠标绘制出的截图区域是可调整大小和位置的,在4个边的中点和4个顶点各有一个小矩形标记。(如图所示)
.NET Framework中本身没有这样的矩形,因此要自定义实现。
考虑到类的专用性,不必实现.Net Framework2.0中Rectangle的全部功能。
该MyRectangle类图如下:

MyRectangle需包含如下属性
int X 记录矩形左上角x坐标
int Y 记录矩形左上角y坐标
int DownPointX 绘制矩形时鼠标落点x坐标
int DownPointY 绘制矩形时鼠标落点y坐标
int Width 矩形宽
int Height 矩形高
int MinWidth 矩形最小宽度
int MinHeight 矩形最小高度
bool ChangeSizeMode 标识矩形当前绘制模式是否为“改变大小”
bool MoveMode 标识矩形当前绘制模式是否为“移动”
bool MouseOnLeftTop 标识鼠标当前位置是否在矩形左上角
bool MouseOnLeftMiddle 标识鼠标当前位置是否在矩形左边中点
bool MouseOnLeftBottom 标识鼠标当前位置是否在矩形左下角
bool MouseOnRightTop 标识鼠标当前位置是否在矩形右上角
bool MouseOnRightMiddle 标识鼠标当前位置是否在矩形右边中点
bool MouseOnRightBottom 标识鼠标当前位置是否在矩形右下角
bool MouseOnTopMiddle 标识鼠标当前位置是否在矩形顶边中点
bool MouseOnBottomMiddle 标识鼠标当前位置是否在矩形底边中点
bool MouseOnMiddle 标识鼠标当前位置是否在矩形中心
int LittleRectangleWidth 矩形周边8个小矩形的宽度
int LittleRectangleHeight 矩形周边8个小矩形的高度
Rectangle LeftTopRectangle 矩形左上角小矩形
Rectangle LeftMiddleRectangle 矩形左边中点小矩形
Rectangle LeftBottomRectangle 矩形左下角小矩形
Rectangle RightTopRectangle 矩形右上角小矩形
Rectangle RightMiddleRectangle 矩形右边中点小矩形
Rectangle RightBottomRectangle 矩形右下角小矩形
Rectangle TopMiddleRectangle 矩形顶边中点小矩形
Rectangle BottomMiddleRectangle 矩形底边中点小矩形
Rectangle Rect 主体矩形
Size Size 矩形大小
Image BackImage 背景图片
Cursor MyCursor 光标样式
矩形本身包含监测当前绘制模式和绘制方法,主要方法成员如下:
SetLittleRectangle() 设置8个小矩形
Draw(Color backColor) 绘制方法,+1重载
ChangeSize(MouseEventArgs e) 改变矩形大小
Move(int newX, int newY) 改变矩形位置
CheckMouseLocation(MouseEventArgs e) 判断鼠标当前落点
setAllModeFalse() 将所有模式设定为false
public bool onChangingMode() 判断当前绘制模式是否为“改变大小”或“移动”
Initialize(int x, int y, int width, int height) 根据给定参数初始化矩形
MyRectagle类代码实现如下:
class MyRectangle
...{
/**//// <summary>
/// The x-coordinate of the upper-left corner of the main rectangle
/// </summary>
private int x;

/**//// <summary>
/// The star x-coordinate when you draw the main rectangle
/// </summary>
private int downPointX;

/**//// <summary>
/// The y-coordinate of the upper-left corner of the main rectangle
/// </summary>
private int y;

/**//// <summary>
/// The star y-coordinate when you draw the main rectangle
/// </summary>
private int downPointY;

/**//// <summary>
/// The width of the main rectangle
/// </summary>
private int width;

/**//// <summary>
/// The height of the main rectangle
/// </summary>
private int height;

/**//// <summary>
/// The least width of the main rectangle
/// </summary>
private int minWidth;

/**//// <summary>
/// The least height of the main rectangle
/// </summary>
private int minHeight;

/**//// <summary>
/// Sign the main rectangle is on change size mode or not
/// </summary>
private bool changeSizeMode;

/**//// <summary>
/// Sign the main rectangle is on move mode or not
/// </summary>
private bool moveMode;

/**//// <summary>
/// Sign the current mouse position is on the upper-left corner of the main rectangle or not
/// </summary>
private bool mouseOnLeftTop;
/**//// <summary>
/// Sign the current mouse position is on the middle point of the left line of the main rectangle or not
/// </summary>
private bool mouseOnLeftMiddle;
/**//// <summary>
/// Sign the current mouse position is on the bottom-left corner of the main rectangle or not
/// </summary>
private bool mouseOnLeftBottom;

/**//// <summary>
/// Sign the current mouse position is on the upper-right corner of the mian rectangle or not
/// </summary>
private bool mouseOnRightTop;
/**//// <summary>
/// 鼠标落点在右中点标志
/// </summary>
private bool mouseOnRightMiddle;
/**//// <summary>
/// Sign the current mouse position is on the middle point of the right line of the main rectangle or not
/// </summary>
private bool mouseOnRightBottom;

/**//// <summary>
/// Sign the current mouse position is on the middle point of the top line of the main rectangle or not
/// </summary>
private bool mouseOnTopMiddle;
/**//// <summary>
/// Sign the current mouse position is on the middle point of the bottom line of the main rectangle or not
/// </summary>
private bool mouseOnBottomMiddle;

/**//// <summary>
/// Sign the current mouse position is in the main rectangle or not
/// </summary>
private bool mouseOnMiddle;
/**//// <summary>
/// The width of the 8 little rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle
/// </summary>
private int littleRectangleWidth;
/**//// <summary>
/// The height of the 8 little rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle
/// </summary>
private int littleRectangleHeight;

/**//// <summary>
/// The little rectangle on the upper-left corner of the main rectangle
/// </summary>
private Rectangle leftTopRectangle;
/**//// <summary>
/// The little rectangle on the middle point of the left line of the main rectangle
/// </summary>
private Rectangle leftMiddleRectangle;
/**//// <summary>
/// The little rectangle on the bottom-left corner of the main rectangle
/// </summary>
private Rectangle leftBottomRectangle;

/**//// <summary>
/// The little rectangle on the upper-right corner of the main rectangle
/// </summary>
private Rectangle rightTopRectangle;
/**//// <summary>
/// The little rectangle on the middle point of the right line of the main rectangle
/// </summary>
private Rectangle rightMiddleRectangle;
/**//// <summary>
/// The little rectangle on the bottom-right corner of the main rectangle
/// </summary>
private Rectangle rightBottomRectangle;

/**//// <summary>
/// The little rectangle on the middle point of the top line of the main rectangle
/// </summary>
private Rectangle topMiddleRectangle;
/**//// <summary>
/// The little rectangle on the middle point of the bottom line of the main rectangle
/// </summary>
private Rectangle bottomMiddleRectangle;

/**//// <summary>
/// The main rectangle
/// </summary>
private Rectangle rect;

/**//// <summary>
/// The size of the main rectangle
/// </summary>
private Size size;

/**//// <summary>
/// The background image of the screen
/// </summary>
private Image backImage;

/**//// <summary>
/// The cursor manner
/// </summary>
private Cursor myCursor;

/**//// <summary>
/// Gets of sets the x-coordinate of the upper-left corner of the main rectangle
/// </summary>
public int X
...{
get ...{ return x; }
set
...{
x = value;
rect.X = value;
}
}
/**//// <summary>
/// Gets of sets the y-coordinate of the upper-left corner of the main rectangle
/// </summary>
public int Y
...{
get ...{ return y; }
set
...{
y = value;
rect.Y = value;
}
}

/**//// <summary>
/// Gets of sets the star x-coordinate when you draw the main rectangle
/// </summary>
public int DownPointX
...{
get ...{ return downPointX; }
set ...{ downPointX = value; }
}
/**//// <summary>
/// Gets of sets the star y-coordinate when you draw the main rectangle
/// </summary>
public int DownPointY
...{
get ...{ return downPointY; }
set ...{ downPointY = value; }
}
/**//// <summary>
/// Gets of sets the width of the main rectangle
/// </summary>
public int Width
...{
get ...{ return width; }
set
...{
width = value;
rect.Width = value;
}
}
/**//// <summary>
/// Gets or sets the height of the main rectangle
/// </summary>
public int Height
...{
get ...{ return height; }
set
...{
height = value;
rect.Height = value;
}
}

/**//// <summary>
/// Gets or sets the least width of the main rectangle
/// </summary>
public int MinWidth
...{
get ...{ return minWidth; }
set ...{ minWidth = value; }
}

/**//// <summary>
/// Gets or sets the least height of the main rectangle
/// </summary>
public int MinHeight
...{
get ...{ return minHeight; }
set ...{ minHeight = value; }
}

/**//// <summary>
/// Gets or sets the sign of the change size mode of the main rectangle
/// </summary>
public bool ChangeSizeMode
...{
get ...{ return changeSizeMode; }
set
...{
changeSizeMode = value;
moveMode = !value;
}
}

/**//// <summary>
/// Gets or sets the sign of the move mode of the main rectangle
/// </summary>
public bool MoveMode
...{
get ...{ return moveMode; }
set
...{
moveMode = value;
changeSizeMode = !value;
}
}

/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the upper-left corner of the main rectangle or not)
/// </summary>
public bool MouseOnLeftTop
...{
get ...{ return mouseOnLeftTop; }
set
...{
mouseOnLeftTop = value;
if (value)
...{
mouseOnLeftMiddle = false;
mouseOnLeftBottom = false;
mouseOnRightTop = false;
mouseOnRightMiddle = false;
mouseOnRightBottom = false;
mouseOnTopMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
}
}
}
/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the middle point of the left line of the main rectangle or not)
/// </summary>
public bool MouseOnLeftMiddle
...{
get ...{ return mouseOnLeftMiddle; }
set
...{
mouseOnLeftMiddle = value;
if (value)
...{
mouseOnLeftTop = false;
mouseOnLeftBottom = false;
mouseOnRightTop = false;
mouseOnRightMiddle = false;
mouseOnRightBottom = false;
mouseOnTopMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
}
}
}
/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the bottom-left corner of the main rectangle or not)
/// </summary>
public bool MouseOnLeftBottom
...{
get ...{ return mouseOnLeftBottom; }
set
...{
mouseOnLeftBottom = value;
if (value)
...{
mouseOnLeftTop = false;
mouseOnLeftMiddle = false;
mouseOnRightTop = false;
mouseOnRightMiddle = false;
mouseOnRightBottom = false;
mouseOnTopMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
}
}
}

/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the upper-right corner of the main rectangle or not)
/// </summary>
public bool MouseOnRightTop
...{
get ...{ return mouseOnRightTop; }
set
...{
mouseOnRightTop = value;
if (value)
...{
mouseOnLeftTop = false;
MouseOnLeftMiddle = false;
mouseOnLeftBottom = false;
mouseOnRightMiddle = false;
mouseOnRightBottom = false;
mouseOnTopMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
}
}
}
/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the middle point of the right line of the main rectangle or not)
/// </summary>
public bool MouseOnRightMiddle
...{
get ...{ return mouseOnRightMiddle; }
set
...{
mouseOnRightMiddle = value;
if (value)
...{
mouseOnLeftTop = false;
mouseOnLeftBottom = false;
mouseOnLeftMiddle = false;
mouseOnRightTop = false;
mouseOnRightBottom = false;
mouseOnTopMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
}
}
}
/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the bottom-right corner of the main rectangle or not)
/// </summary>
public bool MouseOnRightBottom
...{
get ...{ return mouseOnRightBottom; }
set
...{
mouseOnRightBottom = value;
if (value)
...{
mouseOnLeftTop = false;
mouseOnLeftBottom = false;
mouseOnLeftMiddle = false;
mouseOnRightTop = false;
mouseOnRightMiddle = false;
mouseOnTopMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
}
}
}

/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the middle point of the top line of the main rectangle or not)
/// </summary>
public bool MouseOnTopMiddle
...{
get ...{ return mouseOnTopMiddle; }
set
...{
mouseOnTopMiddle = value;
if (value)
...{
mouseOnLeftTop = false;
mouseOnLeftBottom = false;
mouseOnLeftMiddle = false;
mouseOnRightTop = false;
mouseOnRightBottom = false;
mouseOnRightMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
}
}
}
/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is on the middle point of the middle line of the main rectangle or not)
/// </summary>
public bool MouseOnBottomMiddle
...{
get ...{ return mouseOnBottomMiddle; }
set
...{
mouseOnBottomMiddle = value;
if (value)
...{
mouseOnLeftTop = false;
mouseOnLeftBottom = false;
mouseOnLeftMiddle = false;
mouseOnRightTop = false;
mouseOnRightBottom = false;
mouseOnRightMiddle = false;
mouseOnTopMiddle = false;
mouseOnMiddle = false;
}
}
}

/**//// <summary>
/// Gets or sets the sign of current mouse position
/// (is in the main rectangle or not)
/// </summary>
public bool MouseOnMiddle
...{
get ...{ return mouseOnMiddle; }
set
...{
mouseOnMiddle = value;
if (value)
...{
mouseOnLeftTop = false;
mouseOnLeftBottom = false;
mouseOnLeftMiddle = false;
mouseOnRightTop = false;
mouseOnRightBottom = false;
mouseOnRightMiddle = false;
mouseOnTopMiddle = false;
MouseOnBottomMiddle = false;
}
}
}
/**//// <summary>
/// Gets or sets the width of the 8 little rectangles
/// (rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle)
/// </summary>
public int LittleRectangleWidth
...{
get ...{ return littleRectangleWidth; }
set ...{ littleRectangleWidth = value; }
}
/**//// <summary>
/// Gets or sets the height of the 8 little rectangles
/// (rectangles that on the 4 corners and the 4 middle points that of the 4 lines of the main rectangle)
/// </summary>
public int LittleRectangleHeight
...{
get ...{ return littleRectangleHeight; }
set ...{ littleRectangleHeight = value; }
}

/**//// <summary>
/// Gets or sets he little rectangle on the upper-left corner of the main rectangle
/// </summary>
public Rectangle LeftTopRectangle
...{
get ...{ return leftTopRectangle; }
set ...{ leftTopRectangle = value; }
}
/**//// <summary>
/// Gets or sets he little rectangle on the middle point of the left line of the main rectangle
/// </summary>
public Rectangle LeftMiddleRectangle
...{
get ...{ return leftMiddleRectangle; }
set ...{ leftMiddleRectangle = value; }
}
/**//// <summary>
/// Gets or sets he little rectangle on the bottom-left corner of the main rectangle
/// </summary>
public Rectangle LeftBottomRectangle
...{
get ...{ return leftBottomRectangle; }
set ...{ leftBottomRectangle = value; }
}

/**//// <summary>
/// Gets or sets he little rectangle on the upper-right corner of the main rectangle
/// </summary>
public Rectangle RightTopRectangle
...{
get ...{ return rightTopRectangle; }
set ...{ rightTopRectangle = value; }
}
/**//// <summary>
/// Gets or sets he little rectangle on the middle point of the right line of the main rectangle
/// </summary>
public Rectangle RightMiddleRectangle
...{
get ...{ return rightMiddleRectangle; }
set ...{ rightMiddleRectangle = value; }
}
/**//// <summary>
/// Gets or sets he little rectangle on the bottom-right corner of the main rectangle
/// </summary>
public Rectangle RightBottomRectangle
...{
get ...{ return rightBottomRectangle; }
set ...{ rightBottomRectangle = value; }
}

/**//// <summary>
/// Gets or sets he little rectangle on the middle point of the top line of the main rectangle
/// </summary>
public Rectangle TopMiddleRectangle
...{
get ...{ return topMiddleRectangle; }
set ...{ topMiddleRectangle = value; }
}
/**//// <summary>
/// Gets or sets he little rectangle on the middle point of the bottom line of the main rectangle
/// </summary>
public Rectangle BottomMiddleRectangle
...{
get ...{ return bottomMiddleRectangle; }
set ...{ bottomMiddleRectangle = value; }
}

/**//// <summary>
/// Gets or sets the main rectangle
/// </summary>
public Rectangle Rect
...{
get ...{ return rect; }
set
...{
rect = value;
x = value.X;
y = value.Y;
width = value.Width;
height = value.Height;
}
}

/**//// <summary>
/// Gets the size of the main rectangle
/// </summary>
public Size Size
...{
get ...{ return rect.Size; }
}

/**//// <summary>
/// Gets or sets the background image of the screen
/// </summary>
public Image BackImage
...{
get ...{ return backImage; }
set ...{ backImage = value; }
}


/**//// <summary>
/// Gets or sets the manner of the cursor
/// </summary>
public Cursor MyCursor
...{
get ...{ return myCursor; }
set ...{ myCursor = value; }
}


/**//// <summary>
/// Constructor function
/// </summary>
public MyRectangle()
...{
Rect = new Rectangle();
setAllModeFalse();
LittleRectangleWidth = 4;
LittleRectangleHeight = 4;
MinHeight = 5;
MinWidth = 5;
LeftTopRectangle = new Rectangle();
LeftMiddleRectangle = new Rectangle();
LeftBottomRectangle = new Rectangle();
RightTopRectangle = new Rectangle();
RightMiddleRectangle = new Rectangle();
RightBottomRectangle = new Rectangle();
TopMiddleRectangle = new Rectangle();
BottomMiddleRectangle = new Rectangle();
MyCursor = new Cursor(@"....Cursorshcross.cur");
}
public void SetLittleRectangle()
...{
int excursionX = LittleRectangleWidth / 2;
int excursionY = LittleRectangleHeight / 2;
LeftTopRectangle = new Rectangle(X - excursionX, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
leftMiddleRectangle = new Rectangle(X - excursionX, Y - excursionY + Height / 2, LittleRectangleWidth, LittleRectangleHeight);
leftBottomRectangle = new Rectangle(X - excursionX, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
rightTopRectangle = new Rectangle(X - excursionX + Width, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
rightMiddleRectangle = new Rectangle(X - excursionX + Width, Y - excursionY + Height / 2, LittleRectangleWidth, LittleRectangleHeight);
rightBottomRectangle = new Rectangle(X - excursionX + Width, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
topMiddleRectangle = new Rectangle(X - excursionX + Width / 2, Y - excursionY, LittleRectangleWidth, LittleRectangleHeight);
bottomMiddleRectangle = new Rectangle(X - excursionX + Width / 2, Y - excursionY + Height, LittleRectangleWidth, LittleRectangleHeight);
}

/**//// <summary>
/// draw rectangle function
/// </summary>
/// <param name="e">mouse event </param>
/// <param name="backColor">back color</param>
public void Draw(MouseEventArgs e, Color backColor)
...{
Draw(backColor);
if (e.X < DownPointX)
...{
Width = DownPointX - e.X;
X = e.X;
}
else
...{
Width = e.X - DownPointX;
}
if (e.Y < DownPointY)
...{
Height = DownPointY - e.Y;
Y = e.Y;
}
else
...{
Height = e.Y - DownPointY;
}
Draw(backColor);
}
/**//// <summary>
/// draw rectangle function
/// </summary>
/// <param name="backColor">back color</param>
public void Draw(Color backColor)
...{
//Initialize the 8 little rectangles
SetLittleRectangle();
//draw the main rectangle and the 8 little rectangles
ControlPaint.DrawReversibleFrame(rect, backColor, FrameStyle.Dashed);
ControlPaint.FillReversibleRectangle(leftTopRectangle, Color.White);
ControlPaint.FillReversibleRectangle(leftMiddleRectangle, Color.White);
ControlPaint.FillReversibleRectangle(leftBottomRectangle, Color.White);
ControlPaint.FillReversibleRectangle(rightTopRectangle, Color.White);
ControlPaint.FillReversibleRectangle(rightMiddleRectangle, Color.White);
ControlPaint.FillReversibleRectangle(rightBottomRectangle, Color.White);
ControlPaint.FillReversibleRectangle(topMiddleRectangle, Color.White);
ControlPaint.FillReversibleRectangle(bottomMiddleRectangle, Color.White);
}

/**//// <summary>
/// change size when the rectangle is on the change size mode
/// </summary>
/// <param name="e"></param>
public void ChangeSize(MouseEventArgs e)
...{
//change size according the mouse location
if (ChangeSizeMode)
...{
if (MouseOnLeftTop)
...{
if ((Rect.Right - e.X < MinWidth) || (Rect.Bottom - e.Y < MinHeight))
...{
return;
}
Width = Math.Abs(Rect.Right - e.X);
Height = Math.Abs(Rect.Bottom - e.Y);
X = e.X;
Y = e.Y;
}
else if (MouseOnLeftMiddle)
...{
if (Rect.Right - e.X < MinWidth)
...{
return;
}
else
...{
Width = Math.Abs(Rect.Right - e.X);
X = e.X;
}
}
else if (MouseOnLeftBottom)
...{
if (Rect.Right - e.X < MinWidth || e.Y - Rect.Top < MinHeight)
...{
return;
}
Width = Math.Abs(Rect.Right - e.X);
Height = Math.Abs(e.Y - Rect.Top);
X = e.X;
}
else if (MouseOnRightTop)
...{
if (e.X - Rect.Left < MinWidth || Rect.Bottom - e.Y < MinHeight)
...{
return;
}
Width = Math.Abs(e.X - X);
Height = Math.Abs(Rect.Bottom - e.Y);
Y = e.Y;
}
else if (MouseOnRightMiddle)
...{
if (e.X - Rect.Left < MinWidth)
...{
return;
}
Width = Math.Abs(e.X - X);
}
else if (MouseOnRightBottom)
...{
if (e.X - Rect.Left < MinWidth || e.Y - Rect.Top < MinHeight)
...{
return;
}
Width = Math.Abs(e.X - X);
Height = Math.Abs(e.Y - Y);
}
else if (MouseOnTopMiddle)
...{
if (Rect.Bottom - e.Y < MinHeight)
...{
return;
}
Height = Math.Abs(Rect.Bottom - e.Y);
Y = e.Y;
}
else if (MouseOnBottomMiddle)
...{
if (e.Y - Rect.Top < MinHeight)
...{
return;
}
Height = Math.Abs(e.Y - Y);
}
}
}

/**//// <summary>
/// move the location of the rectangle
/// </summary>
/// <param name="newX">The x-coordinate of the new location</param>
/// <param name="newY">The y-coordinate of the new location</param>
public void Move(int newX, int newY)
...{
X = newX;
Y = newY;
}

/**//// <summary>
/// check the current mouse location and set the cursor manner according the mouse location
/// </summary>
/// <param name="e">mouse</param>
public void CheckMouseLocation(MouseEventArgs e)
...{
if (leftTopRectangle.Contains(e.X, e.Y))
...{
if (!onChangingMode())
...{
MouseOnLeftTop = true;
myCursor = new Cursor(@"....Cursorssize2_m.cur");
}
if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (leftMiddleRectangle.Contains(e.X, e.Y))
...{
if (!onChangingMode())
...{
MouseOnLeftMiddle = true;
myCursor = new Cursor(@"....Cursorssize3_m.cur");
}
if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (leftBottomRectangle.Contains(e.X, e.Y))
...{
if (!onChangingMode())
...{
MouseOnLeftBottom = true;
myCursor = new Cursor(@"....Cursorssize1_m.cur");
}
if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (rightTopRectangle.Contains(e.X, e.Y))
...{
mouseOnLeftBottom = false;
if (!onChangingMode())
...{
MouseOnRightTop = true;
myCursor = new Cursor(@"....Cursorssize1_m.cur");
}
mouseOnMiddle = false;
if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (rightMiddleRectangle.Contains(e.X, e.Y))
...{
if (!onChangingMode())
...{
MouseOnRightMiddle = true;
myCursor = new Cursor(@"....Cursorssize3_m.cur");
}
if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (rightBottomRectangle.Contains(e.X, e.Y))
...{
if (!onChangingMode())
...{
MouseOnRightBottom = true;
myCursor = new Cursor(@"....Cursorssize2_m.cur");
}
if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (topMiddleRectangle.Contains(e.X, e.Y))
...{
if (!onChangingMode())
...{
MouseOnTopMiddle = true;
myCursor = new Cursor(@"....Cursorssize4_m.cur");
}

if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (bottomMiddleRectangle.Contains(e.X, e.Y))
...{
if (!onChangingMode())
...{
MouseOnBottomMiddle = true;
myCursor = new Cursor(@"....Cursorssize4_m.cur");
}
if (e.Button == MouseButtons.Left)
...{
if (!MoveMode)
...{
ChangeSizeMode = true;
}
}
else
...{
changeSizeMode = false;
moveMode = false;
}
}
else if (rect.Contains(e.X, e.Y))
...{
if (!changeSizeMode)
...{
MouseOnMiddle = true;
myCursor = new Cursor(@"....Cursorsmove_m.cur");
}
if (e.Button == MouseButtons.Left)
...{
if (!ChangeSizeMode)
...{
MoveMode = true;
}
}
else
...{
moveMode = false;
changeSizeMode = false;
}
}
else
...{
if (e.Button != MouseButtons.Left)
...{
setAllModeFalse();
myCursor = new Cursor(@"....Cursorshcross.cur");
}
}
}
public bool Contains(int x, int y)
...{
if (rect.Contains(x, y))
...{
return true;
}
else return false;
}

/**//// <summary>
/// set all mode false
/// (the sign of the mouse location and the change size mode ,move mode)
/// </summary>
public void setAllModeFalse()
...{
mouseOnLeftTop = false;
mouseOnLeftMiddle = false;
mouseOnLeftBottom = false;
mouseOnRightTop = false;
mouseOnRightMiddle = false;
mouseOnRightBottom = false;
mouseOnTopMiddle = false;
mouseOnBottomMiddle = false;
mouseOnMiddle = false;
changeSizeMode = false;
moveMode = false;
myCursor = new Cursor(@"....Cursorshcross.cur");
}

/**//// <summary>
/// check whether the rectangle is on change mode now
/// </summary>
/// <returns></returns>
public bool onChangingMode()
...{
return ((MouseOnLeftTop || MouseOnLeftMiddle || mouseOnLeftBottom || mouseOnRightTop || mouseOnRightMiddle || MouseOnRightBottom || mouseOnTopMiddle || MouseOnBottomMiddle) && changeSizeMode);
}

/**//// <summary>
/// Initialize the rectangle
/// </summary>
/// <param name="x">The x-coordinate of the rectangle</param>
/// <param name="y">The y-coordinate of the rectangle</param>
/// <param name="width">The width of the rectangle</param>
/// <param name="height">The height of the rectangle</param>
public void Initialize(int x, int y, int width, int height)
...{
X = x;
Y = y;
Width = width;
Height = height;
SetLittleRectangle();
}
}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.Drawing;
using System.Windows.Forms;
在ScreenBody类中添加如下私有成员:
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;
}添加ScreenBody窗口的DoubleClick、MouseDown、MouseUp、MouseMove及Load事件代码:
private void ScreenBody_DoubleClick(object sender, EventArgs e)
...{
if (((MouseEventArgs)e).Button == MouseButtons.Left && myRectangle.Contains(((MouseEventArgs)e).X, ((MouseEventArgs)e).Y))
...{
panel1.Visible = false;
MainPainter.DrawImage(baseImage, 0, 0);
Image memory = new Bitmap(myRectangle.Width, myRectangle.Height);
Graphics g = Graphics.FromImage(memory);
g.CopyFromScreen(myRectangle.X, myRectangle.Y, 0, 0, myRectangle.Size);
Clipboard.SetImage(memory);
this.Close();
}
}
private void ScreenBody_MouseDown(object sender, MouseEventArgs e)
...{
if (e.Button == MouseButtons.Left)
...{
isDowned = true;
if (!RectReady)
...{
myRectangle.DownPointX = e.X;
myRectangle.DownPointY = e.Y;
myRectangle.X = e.X;
myRectangle.Y = e.Y;
}
if (RectReady == true)
...{
moveModeDownPoint = new Point(e.X, e.Y);
}
}
if (e.Button == MouseButtons.Right)
...{
if (!RectReady)
...{
this.Close();
return;
}
MainPainter.DrawImage(baseImage, 0, 0);
myRectangle.Initialize(0, 0, 0, 0);
myRectangle.setAllModeFalse();
this.Cursor = myRectangle.MyCursor;
RectReady = false;
}
}
private void ScreenBody_MouseUp(object sender, MouseEventArgs e)
...{
if (e.Button == MouseButtons.Left)
...{
isDowned = false;
RectReady = true;
}
}
private void ScreenBody_MouseMove(object sender, MouseEventArgs e)
...{
labelWidth.Text = myRectangle.Width.ToString();
labelHeight.Text = myRectangle.Height.ToString();
labelLocation.Text = "X "+myRectangle.X.ToString() + ", Y " + myRectangle.Y.ToString();
if (!RectReady)
...{
if (isDowned)
...{
myRectangle.Draw(e, this.BackColor);
}
}
else
...{
myRectangle.CheckMouseLocation(e);
this.Cursor = myRectangle.MyCursor;
this.changeSizeMode = myRectangle.ChangeSizeMode;
this.moveMode = myRectangle.MoveMode&&myRectangle.Contains(moveModeDownPoint.X,moveModeDownPoint.Y);
if (changeSizeMode)
...{
this.moveMode = false;
myRectangle.Draw(BackColor);
myRectangle.ChangeSize(e);
myRectangle.Draw(BackColor);
}
if (moveMode)
...{
this.changeSizeMode = false;
myRectangle.Draw(BackColor);
myRectangle.X = myRectangle.X + e.X - moveModeDownPoint.X;
myRectangle.Y = myRectangle.Y + e.Y - moveModeDownPoint.Y;
moveModeDownPoint.X = e.X;
moveModeDownPoint.Y = e.Y;
myRectangle.Draw(this.BackColor);
}
}
}
private void ScreenBody_Load(object sender, EventArgs e)
...{
this.WindowState = FormWindowState.Maximized;
MainPainter = this.CreateGraphics();
isDowned = false;
baseImage = this.BackgroundImage;
panel1.Visible = true;
RectReady = false;
changeSizeMode = false;
moveMode = false;
}为了不至截到panel1,添加panel1的MouseEnter事件如下:
private void panel1_MouseEnter(object sender, EventArgs e)
...{
if (panel1.Location==new Point(this.Left,this.Top))
...{
panel1.Location = new Point(this.Right-panel1.Width, this.Top);
}
else
...{
panel1.Location = new Point(this.Left,this.Top);
}
}至此,ScreenBody窗口完成,QQ截图功能可以通过热键触发,下面为本程序添加热键
3,创建热键类
网上有许多这方面的资料,本程序中这段代码取自互联网,如有版权问题请给我留言,我会尽快删除。
添加类HotKey
HotKey.cs文件内容如下
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ScreenCutter
...{
class HotKey
...{
//如果函数执行成功,返回值不为0。
//如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd, //要定义热键的窗口的句柄
int id, //定义热键ID(不能与其它ID重复)
uint fsModifiers, //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
Keys vk //定义热键的内容
);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd, //要取消热键的窗口的句柄
int id //要取消热键的ID
);
//定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
[Flags()]
public enum KeyModifiers
...{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
}
}4,使用热键及托盘区图标
为了使程序更方便使用,程序启动的时候最下化到托盘区,在按下程序热键时会启动截图功能。这些功能在程序的主窗口MainForm类中实现。
为了在托盘区显示图标,为MainForm添加一个NotifyIcon控件,为其指定一Icon图标,并设定visable属性为true
为了实现可以更改热键,首先在项目属性的Setting中添加如下图成员:

MainForm.cs文件代码如下:
using System;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using ScreenCutter.Properties;
namespace ScreenCutter
...{
public enum KeyModifiers //组合键枚举
...{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
public partial class MainForm : Form
...{
private ContextMenu contextMenu1;
private MenuItem menuItem1;
private MenuItem menuItem2;
private ScreenBody body;
public MainForm()
...{
InitializeComponent();
this.components = new Container();
this.contextMenu1 = new ContextMenu();
this.menuItem2 = new MenuItem();
this.menuItem1 = new MenuItem();
this.contextMenu1.MenuItems.AddRange(
new MenuItem[] ...{ this.menuItem1,this.menuItem2 });
this.menuItem1.Index = 1;
this.menuItem1.Text = "E&xit";
this.menuItem1.Click += new EventHandler(this.menuItem1_Click);
this.menuItem2.Index = 0;
this.menuItem2.Text = "S&et HotKey";
this.menuItem2.Click += new EventHandler(this.menuItem2_Click);
notifyIcon1.ContextMenu = this.contextMenu1;
notifyIcon1.Text = "Screen Cutter";
notifyIcon1.Visible = true;
body = null;
}
private void MainForm_SizeChanged(object sender, EventArgs e)
...{
if (this.WindowState == FormWindowState.Minimized)
...{
this.Hide();
this.notifyIcon1.Visible = true;
}
}

private void CutScreen()
...{
Image img = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);
Graphics g = Graphics.FromImage(img);
g.CopyFromScreen(new Point(0, 0), new Point(0, 0), Screen.AllScreens[0].Bounds.Size);
body = new ScreenBody();
body.BackgroundImage = img;
body.Show();
}
private void ProcessHotkey(Message m) //按下设定的键时调用该函数
...{
IntPtr id = m.WParam; //IntPtr用于表示指针或句柄的平台特定类型
string sid = id.ToString();
switch (sid)
...{
case "100":
CutScreen();
break;
default:
break;
}
}
private void MainForm_Load(object sender, EventArgs e)
...{
uint ctrHotKey = (uint)KeyModifiers.Control;
if (Settings.Default.isAltHotKey)
...{
ctrHotKey =(uint)(KeyModifiers.Alt | KeyModifiers.Control);
}
HotKey.RegisterHotKey(Handle, 100, ctrHotKey, Settings.Default.HotKey);//这时热键为Alt+CTRL+A
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
...{
HotKey.UnregisterHotKey(Handle, 100);//卸载第1个快捷键
}
//重写WndProc()方法,通过监视系统消息,来调用过程
protected override void WndProc(ref Message m)//监视Windows消息
...{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值为0x0312那么表示用户按下了热键
switch (m.Msg)
...{
case WM_HOTKEY:
ProcessHotkey(m);//按下热键时调用ProcessHotkey()函数
break;
}
base.WndProc(ref m); //将系统消息传递自父类的WndProc
}
private void menuItem1_Click(object Sender, EventArgs e)
...{
HotKey.UnregisterHotKey(Handle, 100);//卸载第1个快捷键
this.notifyIcon1.Visible = false;
this.Close();
}
private void menuItem2_Click(object Sender, EventArgs e)
...{
SetHotKey setHotKey = new SetHotKey();
setHotKey.ShowDialog();
}
}
}5,添加设定热键功能:
新建窗口,命名为SetHotkey,该窗口样式及主要控件命名如下图所示

设定窗口主体FormBorderStyle属性值为FixedToolWindow,Text属性为SetHotKey,MaximizeBox和MinimizeBox属性为false。
添加checkBox1的(ApplicationSettings)-(PropertyBinding)-Checked为isCtrlHotKey,CheckState为Checked,Enable属性为false,Text属性为Ctrl
添加checkBox2的(ApplicationSettings)-(PropertyBinding)-Checked为isAltHotKey,CheckState为Checked,Enable属性为true,Text属性为Alt
comboBox1的Items值为
A
Z
X
为按钮btnDefault添加click事件
private void btnDefault_Click(object sender, EventArgs e)
...{
Settings.Default.HotKey = Keys.A;
Settings.Default.Save();
this.Close();
}为按钮btnOk添加click事件
private void btnOK_Click(object sender, EventArgs e)
...{
switch (comboBox1.SelectedIndex)
...{
case 0:
Settings.Default.HotKey = Keys.A;
break;
case 1:
Settings.Default.HotKey = Keys.Z;
break;
case 2:
Settings.Default.HotKey = Keys.X;
break;
default:
break;
}
Settings.Default.Save();
this.Close();
}为按钮btnCancel添加click事件
private void btnCancel_Click(object sender, EventArgs e)
...{
this.Close();
}为SetHotkey窗口添加load事件
{
comboBox1.Text = Settings.Default.HotKey.ToString();
}
6,防止程序多次运行
同样,网上有许多这方面的资料,本部分代码基本来自互联网,如有版权问题请给我留言,我将立即删除
为防止程序多次运行,修改Program.cs文件内容如下:
using System;
using System.Reflection;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace ScreenCutter
...{
static class Program
...{
[DllImport("User32.dll")]
private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
private const int WS_SHOWNORMAL = 1;
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
...{
Process instance = RunningInstance();
if (instance == null)
...{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
else
...{
HandleRunningInstance(instance);
}
}
public static Process RunningInstance()
...{
Process current = Process.GetCurrentProcess();
Process[] processes = Process.GetProcessesByName(current.ProcessName);
//Loop through the running processes in with the same name
foreach (Process process in processes)
...{
//Ignore the current process
if (process.Id != current.Id)
...{
//Make sure that the process is running from the exe file.
if (Assembly.GetExecutingAssembly().Location.Replace("/", "/") ==
current.MainModule.FileName)
...{
//Return the other process instance.
return process;
}
}
}
//No other instance was found, return null.
return null;
}
public static void HandleRunningInstance(Process instance)
...{
//Make sure the window is not minimized or maximized
ShowWindowAsync(instance.MainWindowHandle, WS_SHOWNORMAL);
//Set the real intance to foreground window
SetForegroundWindow(instance.MainWindowHandle);
}
}
}至此,该截图程序基本完成,实现了类似QQ截图的功能。(默认热键为Ctrl+Alt+A)
注意:程序中用到了一些图片,Icon文件和cur文件,请复制系统目录(C:/WINDOWS/Cursors)下的hcross.cur、move_m.cur、size1_m.cur、size2_m.cur、size3_m.cur、size4_m.cur文件到../ScreenCutter/ScreenCutter/Cursors目录下,在../ScreenCutter/ScreenCutter/Icons目录下添加相应图标,在../ScreenCutter/ScreenCutter/Images目录下添加相应图片。如路径不同,请在代码中自行更改。
感谢您的关注,愿您留下宝贵意见。
这篇博客介绍了一个使用C#实现的类似QQ截图功能的工具。通过自定义矩形类MyRectangle来处理用户绘制的截图区域,包括改变大小和移动的操作。主窗口ScreenBody包含了必要的组件和事件处理,同时利用热键和托盘图标实现便捷操作。程序还提供了设置热键的功能,并防止了程序多次运行的问题。
1016

被折叠的 条评论
为什么被折叠?



