C#制作QQ截图的自动框选功能的个人思路(三)<自动框选>

本文介绍了一款软件实现全屏截图并允许用户通过鼠标操作选择区域进行高亮绘制的功能,包括点击开始绘制、拖动绘制矩形以及右键结束绘制。通过底层API操作,实现对桌面窗口的精确定位与交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果图:

OH、、

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using System.Runtime.InteropServices;

namespace AutoDrawRect
{
    public partial class Form2 : Form
    {
        [DllImport("user32.dll")]//获取桌面的句柄
        public static extern IntPtr GetDesktopWindow();
        [DllImport("user32.dll")]//在桌面找寻子窗体
        public static extern IntPtr ChildWindowFromPointEx(IntPtr pHwnd, Point pt, uint uFlgs);
        private const int CWP_SKIPDISABLED = 0x2;   //忽略不可用窗体
        private const int CWP_SKIPINVISIBL = 0x1;   //忽略隐藏的窗体
        private const int CWP_All = 0x0;    //一个都不忽略
        
        [DllImport("user32.dll")]//获得句柄对象的位置
        public static extern bool GetWindowRect(IntPtr hWnd, out LPRECT lpRect);
        public struct LPRECT {//位置信息的结构体
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        [DllImport("user32.dll")]//进行坐标转换 (再窗体内部进行查找)
        public static extern bool ScreenToClient(IntPtr hWnd, out LPPOINT lpPoint);
        public struct LPPOINT {//要转换的坐标信息的结构体
            public int x;
            public int y;
        }

        public Form2() {
            InitializeComponent();
            this.FormBorderStyle = FormBorderStyle.None;
            this.Location = new Point(0, 0);
            this.Size = new Size(Screen.PrimaryScreen.Bounds.Width,
                Screen.PrimaryScreen.Bounds.Height);
            pictureBox1.Dock = DockStyle.Fill;
        }

        ~Form2() {//窗体关闭时卸载Hook
            mHook.UnLoadMouseHook();
        }

        Bitmap screenBmp;   //保存全屏的图像
        bool isDrawed;      //是否已经画出了一个区域
        bool isDraw;        //是否允许绘制
        IntPtr hWnd;        //保存相应对象的句柄
        int sx, sy;         //鼠标点下时的鼠标坐标
        
        MouseHook mHook = new MouseHook();//创建一个Hook
        
        private void Form2_Load(object sender, EventArgs e) {
            screenBmp = GetScreen();
            pictureBox1.Image = GetScreen();
            using (Graphics g = Graphics.FromImage(pictureBox1.Image)) {
                SolidBrush sb = new SolidBrush(Color.FromArgb(125, 0, 0, 0));
                g.FillRectangle(sb, 0, 0, this.Width, this.Height);
                sb.Dispose();
            }
            this.Enabled = false;       //禁用此窗体(这样在查找的时候才能把当前窗体忽略)
            mHook.HooKMouseEvent +=new MouseHook.MEventhandler(mHook_HooKMouseEvent);
            mHook.SetMouseHook();       //启用Hook
        }

        public Bitmap GetScreen() {//获得全屏图像
            Bitmap bmp = new Bitmap(this.Width, this.Height);
            using (Graphics g = Graphics.FromImage(bmp)) {
                g.CopyFromScreen(0, 0, 0, 0, this.Size);
            }
            return bmp;
        }
        
        private void mHook_HooKMouseEvent(object sender,MouseInfoEventArys e) {//绑定到Hook上面的事件
            if (e.MBtn == Btn.LeftDowm) {//判断左键是否点下
                this.Enabled = true;    //点下的时候恢复窗体的禁用
                sx = MousePosition.X;   //记录下当前鼠标位置
                sy = MousePosition.Y;
                DrawRect("Normal");     //然后把当前区域绘制出来
                isDrawed = true;        //已经绘制
                isDraw = true;          //允许绘制
                return;//返回
            } else if(e.MBtn == Btn.RightUp) {//如果右键抬起
                if (isDrawed) {             //判断是否已经绘制 已经绘制取消绘制
                    hWnd = IntPtr.Zero;     //句柄清空 重新找寻
                    isDrawed = false;       //没有绘制
                    this.Enabled = false;   //继续禁用窗体 (查找时忽略禁用的窗体)
                } else {
                    this.Enabled = true;        //如果没有绘制 那关闭窗体 先恢复窗体
                    new Thread(new ThreadStart(ThreadCloseForm)).Start();
                    /*起一个线程来关闭窗体 目的是为了让当前窗体 接受到这个右键信息 
                     不然这个现在关闭了 那么这个右键信息就传递到下面去了
                     */
                }
            }
            if (isDrawed) {     //如果已经绘制了 就不用继续自动框选区域了
                return;
            }
            FoundRectToDraw();  //找寻框选区域并且绘制
        }
        public void FoundRectToDraw() {
            //在桌面根据鼠标位置去找寻鼠标下面的控件句柄
            IntPtr thWnd = ChildWindowFromPointEx(GetDesktopWindow(),
                    MousePosition, CWP_SKIPDISABLED | CWP_SKIPINVISIBL);//忽略桌面上隐藏或者禁用的窗体
            //thWnd是找到的桌面的窗体的句柄 再次 根据鼠标坐标在此窗体里面找寻控件
            LPPOINT lp = new LPPOINT();     //注意这里的鼠标不是屏幕坐标 而是 窗体内部坐标
            lp.x = MousePosition.X; lp.y = MousePosition.Y;
            ScreenToClient(thWnd, out lp);  //所以把屏幕坐标转换为客户区内部坐标
            //再次用刚才的thWnd句柄去找寻 这次一个都不忽略
            IntPtr temphWnd = ChildWindowFromPointEx(thWnd, new Point(lp.x, lp.y),CWP_All);
            //判断这个句柄是否为空
            if (temphWnd == IntPtr.Zero) {  //如果没有找到 就用刚才那个句柄(这里我只查找了两层)
                temphWnd = thWnd;
            }
            if (temphWnd == hWnd) {     //如果这个句柄 和 上一个区域的句柄没有变化 那么久返回吧
                return;
            } else {
                hWnd = temphWnd;        //不然就把这个新找到的句柄赋值给它 
            }
            DrawRect("Normal");         //然后在这个新的句柄外面绘制一圈
        }
        
        public void DrawRect(string type) {//绘制区域
            using (Graphics g = pictureBox1.CreateGraphics()) {//建立画布
                using (Pen p = new Pen(Color.FromArgb(255, 24, 219, 255), 2)) {//画笔
                    pictureBox1.Refresh();  //绘制前先清空画布
                    switch (type) {
                        case "Normal"://绘制找到的区域
                            LPRECT rect = new LPRECT();
                            GetWindowRect(hWnd, out rect);
                            g.DrawRectangle(p, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top);
                            Rectangle drawRect = new Rectangle(new Point(rect.left, rect.top),
                                new Size(rect.right - rect.left, rect.bottom - rect.top));
                            g.DrawImage(screenBmp, drawRect, drawRect, GraphicsUnit.Pixel);
                            break;
                        case "Rectangle"://鼠标点下并且拖动的时候 绘制矩形(也就是自定义区域)
                            g.DrawRectangle(p, sx, sy, MousePosition.X - sx + 1, MousePosition.Y - sy + 1);
                            break;
                        case "MouseUp"://鼠标抬起的时候 把相应区域的图片也画上去(正常的不是黑色的图)
                            Rectangle imgRect = new Rectangle(new Point(sx, sy), 
                                new Size(MousePosition.X - sx + 1, MousePosition.Y - sy + 1));
                            g.DrawRectangle(p, sx, sy, MousePosition.X - sx + 1, MousePosition.Y - sy + 1);
                            g.DrawImage(screenBmp, imgRect, imgRect, GraphicsUnit.Pixel);
                            break;
                    }
                }
            }
        }
        public delegate void CloseFormDel();//创建委托 因为要跨线程操作
        //Form.CheckForIllegalCrossThreadCalls = false;这样也可以就可以直接跨线程操作了 不过 话说不好
        public void ThreadCloseForm() {
            Thread.Sleep(100);//暂停100毫秒再关闭窗体 (让改窗体接受到鼠标)
            try {//好吧 我承认 这里我也不知道为什么 有时候提示改 不能再该句柄创建前调用Invoke(我很郁闷我的窗体不是创建了么)
                this.Invoke(new CloseFormDel(CloseForm));
            } catch {
                this.Close();
            } 
        }
        public void CloseForm() {//关闭窗体
            this.Close();
        }

        private void pictureBox1_MouseMove(object sender, MouseEventArgs e) {
            //判断鼠标是否点下 如果点下没有松开 那么移动的时候就绘制区域
            if (isDraw) {
                //因为窗体在恢复禁用的时候就会马上出发Move事件 所以判断一下 如果鼠标在点下的时候 位置没用动那么什么都不做
                if (sx == MousePosition.X && sy == MousePosition.Y) {
                    return;
                }
                DrawRect("Rectangle");//还有自己判断 反方向拖动鼠标的情况 - -!、、这里就不写了 代码太多看着麻烦
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e) {
            isDraw = false;//取消鼠标移动的绘制
            //因为第一次点下鼠标 默认是 绘制自动框选的区域 所以 坐标没有变的话 什么都不做
            if (sx == MousePosition.X && sy == MousePosition.Y) {
                return;
            }
            DrawRect("MouseUp");
        }
    }
}

值得注意的是这里

        public void FoundRectToDraw() {
            //在桌面根据鼠标位置去找寻鼠标下面的控件句柄
            IntPtr thWnd = ChildWindowFromPointEx(GetDesktopWindow(),
                    MousePosition, CWP_SKIPDISABLED | CWP_SKIPINVISIBL);//忽略桌面上隐藏或者禁用的窗体
            //thWnd是找到的桌面的窗体的句柄 再次 根据鼠标坐标在此窗体里面找寻控件
            LPPOINT lp = new LPPOINT();     //注意这里的鼠标不是屏幕坐标 而是 窗体内部坐标
            lp.x = MousePosition.X; lp.y = MousePosition.Y;
            ScreenToClient(thWnd, out lp);  //所以把屏幕坐标转换为客户区内部坐标
            //再次用刚才的thWnd句柄去找寻 这次一个都不忽略
            IntPtr temphWnd = ChildWindowFromPointEx(thWnd, new Point(lp.x, lp.y),CWP_All);
            //判断这个句柄是否为空
            if (temphWnd == IntPtr.Zero) {  //如果没有找到 就用刚才那个句柄(这里我只查找了两层)
                temphWnd = thWnd;
            }
            if (temphWnd == hWnd) {     //如果这个句柄 和 上一个区域的句柄没有变化 那么久返回吧
                return;
            } else {
                hWnd = temphWnd;        //不然就把这个新找到的句柄赋值给它 
            }
            DrawRect("Normal");         //然后在这个新的句柄外面绘制一圈
        }

这里 我只进行了两层的查找 第一层桌面的窗体 第二层窗体里面的控件 (如果控件中还有控件 那么久得进行第三层的查找 比如窗体里面有一个panel上面有一个button那个只能把panel框选出来 那么久得得再次根据坐标去panel里面查找)

注意查找的时候 坐标都是用的窗体的内部坐标 而不是 屏幕的坐标 (第一成就用屏幕的坐标 因为桌面也是一个窗体 屏幕坐标就是桌面窗体的内部坐标!)

源码在第一篇文章中有下载连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值