C#封装User32类 findwindow sendmessage等等

这篇博客介绍了如何在C#中封装User32类,便于重复使用win32 API,如findwindow查找窗口句柄,findwindowex查找子窗口,sendmessage发送消息,getwindowrect获取窗口边框,getcaptureimage截取窗口图像,以及getpixel获取像素颜色等。这些功能对于进行窗口交互和自动化操作非常实用。
摘要由CSDN通过智能技术生成

因为经常会用到这些东西 每次都重新写太过麻烦 所以打算卸载一起 方便 以后调用就好

封装一些常用的 win32 的API  刚是试了下新的类 发下不行  现在只好折叠到一起了

using System.Runtime.InteropServices; 这个别忘啦 

#region 各种API
        const int WM_MOUSEWHEEL = 0x020A; //鼠标滚轮
        const int WM_LBUTTONDOWN = 0x0201;//鼠标左键
        const int WM_LBUTTONUP = 0x0202;
        const int WM_KEYDOWN = 0x0100;//模拟按键
        const int WM_KEYUP = 0x0101;
        const int MOUSEEVENTF_MOVE = 0x0001;//用于琴台鼠标移动
        const int MOUSEEVENTF_LEFTDOWN = 0x0002;//前台鼠标单击
        const int MOUSEEVENTF_LEFTUP = 0x0004;
        const int WM_SETTEXT = 0x000C;//设置文字
        const int WM_GETTEXT = 0x000D;//读取文字
        [DllImport("User32")]
        private static extern int FindWindow(string className, string windowName);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        private static extern int SendMessage(int hWnd, int Msg, int wParam, string lParam);
        [DllImport("user32.dll")]//在窗口列表中寻找与指定条件相符的第一个子窗口
        private static extern int FindWindowEx(int hwndParent, // handle to parent window
                                                int hwndChildAfter, // handle to child window
                                                string className, //窗口类名            
                                                string windowName);
        [DllImport("gdi32.dll")]
        private static extern uint GetPixel(int hdc, int nXPos, int nYPos);
       
        [System.Runtime.InteropServices.DllImport("user32")]
        private static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);
        [DllImport("user32.dll")]
        static extern bool SetCursorPos(int X, int Y);

        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateDC(
         string lpszDriver,         // driver name驱动名
         string lpszDevice,         // device name设备名
         string lpszOutput,         // not used; should be NULL
         IntPtr lpInitData   // optional printer data
         );
        [DllImport("gdi32.dll")]
        public static extern int BitBlt(
         IntPtr hdcDest, // handle to destination DC目标设备的句柄
         int nXDest,   // x-coord of destination upper-left corner目标对象的左上角的X坐标
         int nYDest,   // y-coord of destination upper-left corner目标对象的左上角的Y坐标
         int nWidth,   // width of destination rectangle目标对象的矩形宽度
         int nHeight, // height of destination rectangle目标对象的矩形长度
         IntPtr hdcSrc,   // handle to source DC源设备的句柄
         int nXSrc,    // x-coordinate of source upper-left corner源对象的左上角的X坐标
         int nYSrc,    // y-coordinate of source upper-left corner源对象的左上角的Y坐标
         UInt32 dwRop   // raster operation code光栅的操作值
         );
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleDC(
         IntPtr hdc // handle to DC
         );
        [DllImport("gdi32.dll")]
        public static extern IntPtr CreateCompatibleBitmap(
         IntPtr hdc,         // handle to DC
         int nWidth,      // width of bitmap, in pixels
         int nHeight      // height of bitmap, in pixels
         );
        [DllImport("gdi32.dll")]
        public static extern IntPtr SelectObject(
         IntPtr hdc,           // handle to DC
         IntPtr hgdiobj    // handle to object
         );
        [DllImport("gdi32.dll")]
        public static extern int DeleteDC(
         IntPtr hdc           // handle to DC
         );
        [DllImport("user32.dll")]
        public static extern bool PrintWindow(
         IntPtr hwnd,                // Window to copy,Handle to the window that will be copied.
         IntPtr hdcBlt,              // HDC to print into,Handle to the device context.
         UInt32 nFlags               // Optional flags,Specifies the drawing options. It can be one of the following values.
         );
        [DllImport("user32.dll")]
        public static extern IntPtr GetWindowDC(
         IntPtr hwnd
         );
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool GetWindowRect(int hWnd, ref RECT lpRect);
        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left; //最左坐标
            public int Top; //最上坐标
            public int Right; //最右坐标
            public int Bottom; //最下坐标
        }
        public static Bitmap GetWindowCapture(IntPtr hWnd)
        {
            IntPtr hscrdc = GetWindowDC(hWnd);
            RECT windowRect = new RECT();
            GetWindowRect(hWnd, ref windowRect);
            int width = windowRect.Right - windowRect.Left;
            int height = windowRect.Bottom - windowRect.Top;
            IntPtr hbitmap = CreateCompatibleBitmap(hscrdc, width, height);
            IntPtr hmemdc = CreateCompatibleDC(hscrdc);
            SelectObject(hmemdc, hbitmap);
            PrintWindow(hWnd, hmemdc, 0);
            Bitmap bmp = Bitmap.FromHbitmap(hbitmap);
            DeleteDC(hscrdc);//删除用过的对象
            DeleteDC(hmemdc);//删除用过的对象
            return bmp;
        }

        #endregion

上述代码包含内容

findwindow 查找窗口句柄   参数1 类型 参数2标题

findwindowex 查找子窗口句柄  参数1 父窗口句柄  参数2从某个子窗口开始 参数3类型 参数4标题

sendmessage 附加消息 int 和string 两种   这个函数很强大 支持很多参数  详情百度

假如发送的鼠标单击消息的话    最后一个参数的   的356是Y坐标  834为X坐标 SendMessage(num1, WM_LBUTTONUP, 0, ((356) << 16 | (834)));

getwindowrect 查找窗口边框  返回RECT

getwubdiwcapture 截取某个窗口图像  参数 句柄  返回BMP

movse_event 鼠标的相关操作(前台的) 

getpixel 获取某个句柄 某个点的颜色


这个只是一部分 将来会慢慢补充的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值