用C#实现截图功能(类似QQ截图)

这篇博客介绍了一个使用C#实现的类似QQ截图功能的工具。通过自定义矩形类MyRectangle来处理用户绘制的截图区域,包括改变大小和移动的操作。主窗口ScreenBody包含了必要的组件和事件处理,同时利用热键和托盘图标实现便捷操作。程序还提供了设置热键的功能,并防止了程序多次运行的问题。
摘要由CSDN通过智能技术生成

概述:

在公司,不能自己安装软件,也不能下载,但有时候需要截图。用PrintScreen键只能截取全屏,感觉很麻烦。于是决定自己编写一个截图工具 。

众所周知,QQ截图首先将截取全屏为一个图片,然后用在这个图片基础上截取需要的部分。本程序实现方法类似。

程序运行截图如下:

图1:程序运行截图

图中心矩形为即将截取区域

程序很粗糙,希望大家提出宝贵意见。

1,自定义矩形类MyRectangle

在QQ截图程序中,用户用鼠标绘制出的截图区域是可调整大小和位置的,在4个边的中点和4个顶点各有一个小矩形标记。(如图所示)

Rectangle.NET Framework中本身没有这样的矩形,因此要自定义实现。
考虑到类的专用性,不必实现.Net Framework2.0中Rectangle的全部功能。

 

 

 

该MyRectangle类图如下:

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
        
{
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值