• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#调用windowsapi操作鼠标、键盘、窗体合集...更新中

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

1.根据句柄查找窗体

引自http://www.2cto.com/kf/201410/343342.html

使用SPY++工具获取窗体

  首先打开spy++工具,同时点击"查找窗口"按钮(望远镜),再点击"查找程序工具"中按钮拖拽至要查看的窗体中,点击"确定"按钮.

IntPtr awin = MouseHookHelper.FindWindow("WeChatMainWndForPC", "微信");
if (awin == IntPtr.Zero)
{
    MessageBox.Show("没有找到窗体");
    return;
}

2.获取窗体坐标信息

MouseHookHelper.RECT rect = new MouseHookHelper.RECT();
MouseHookHelper.GetWindowRect(awin, ref rect);
int width = rect.Right - rect.Left;             //窗口的宽度
int height = rect.Bottom - rect.Top;            //窗口的高度
int x = rect.Left;
int y = rect.Top;

3.设置为当前窗体

MouseHookHelper.SetForegroundWindow(awin);
MouseHookHelper.ShowWindow(awin,MouseHookHelper.SW_SHOWNOACTIVATE);//4、5

4.点击某个坐标

LeftMouseClick(new MouseHookHelper.POINT()
{
    X = ppp.MsgX,
    Y = ppp.MsgY
});

private static void LeftMouseClick(MouseHookHelper.POINT pointInfo)
{

    //先移动鼠标到指定位置
    MouseHookHelper.SetCursorPos(pointInfo.X, pointInfo.Y);

    //按下鼠标左键
    MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTDOWN,
                pointInfo.X * 65536 / Screen.PrimaryScreen.Bounds.Width,
                pointInfo.Y * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);

    //松开鼠标左键
    MouseHookHelper.mouse_event(MouseHookHelper.MOUSEEVENTF_LEFTUP,
                pointInfo.X * 65536 / Screen.PrimaryScreen.Bounds.Width,
                pointInfo.Y * 65536 / Screen.PrimaryScreen.Bounds.Height, 0, 0);

}
        

5.复制粘贴操作

//复制到剪贴板
Clipboard.SetText("test");
//从剪贴板获取数据
Clipboard.GetText();
//粘贴
SendKeys.SendWait("^V");
//回车键
SendKeys.Send("{Enter}");

6.钩子的使用

private void button1_Click(object sender, EventArgs e)
{
    if (hHook == 0)
    {
        MyProcedure = new MouseHookHelper.HookProc(this.MouseHookProc);
        //这里挂节钩子
        hHook = MouseHookHelper.SetWindowsHookEx(WH_MOUSE_LL, MyProcedure, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
        if (hHook == 0)
        {
            MessageBox.Show("请以管理员方式打开");
            return;
        }
        button1.Text = "卸载钩子";
    }
    else
    {
        bool ret = MouseHookHelper.UnhookWindowsHookEx(hHook);
        if (ret == false)
        {
            MessageBox.Show("请以管理员方式打开");
            return;
        }
        hHook = 0;
        button1.Text = "安装钩子";
    }
}
        
        
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)
{

    MouseHookHelper.MouseHookStruct MyMouseHookStruct = (MouseHookHelper.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookHelper.MouseHookStruct));
    if (nCode < 0)
    {
        return MouseHookHelper.CallNextHookEx(hHook, nCode, wParam, lParam);
    }
    else
    {
        String strCaption = "x = " + MyMouseHookStruct.pt.X.ToString("d") + "  y = " + MyMouseHookStruct.pt.Y.ToString("d");
        this.Text = strCaption;
        return MouseHookHelper.CallNextHookEx(hHook, nCode, wParam, lParam);
    }
}

7.MouseHookHelper代码


public class MouseHookHelper
    {

        #region 根据句柄寻找窗体并发送消息

        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        //参数1:指的是类名。参数2,指的是窗口的标题名。两者至少要知道1个
        public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lclassName, string windowTitle);

        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, string lParam);

        [DllImport("user32.dll", EntryPoint = "SendMessage", SetLastError = true, CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hwnd, uint wMsg, int wParam, int lParam);

        #endregion

        #region 获取窗体位置
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool GetWindowRect(IntPtr hWnd, ref RECT lpRect);

        [StructLayout(LayoutKind.Sequential)]
        public struct RECT
        {
            public int Left;                             //最左坐标
            public int Top;                             //最上坐标
            public int Right;                           //最右坐标
            public int Bottom;                        //最下坐标
        }
        #endregion

        #region 设置窗体显示形式

        public enum nCmdShow : uint
        {
            SW_NONE,//初始值
            SW_FORCEMINIMIZE,//:在WindowNT5.0中最小化窗口,即使拥有窗口的线程被挂起也会最小化。在从其他线程最小化窗口时才使用这个参数。
            SW_MIOE,//:隐藏窗口并激活其他窗口。
            SW_MAXIMIZE,//:最大化指定的窗口。
            SW_MINIMIZE,//:最小化指定的窗口并且激活在Z序中的下一个顶层窗口。
            SW_RESTORE,//:激活并显示窗口。如果窗口最小化或最大化,则系统将窗口恢复到原来的尺寸和位置。在恢复最小化窗口时,应用程序应该指定这个标志。
            SW_SHOW,//:在窗口原来的位置以原来的尺寸激活和显示窗口。
            SW_SHOWDEFAULT,//:依据在STARTUPINFO结构中指定的SW_FLAG标志设定显示状态,STARTUPINFO 结构是由启动应用程序的程序传递给CreateProcess函数的。
            SW_SHOWMAXIMIZED,//:激活窗口并将其最大化。
            SW_SHOWMINIMIZED,//:激活窗口并将其最小化。
            SW_SHOWMINNOACTIVATE,//:窗口最小化,激活窗口仍然维持激活状态。
            SW_SHOWNA,//:以窗口原来的状态显示窗口。激活窗口仍然维持激活状态。
            SW_SHOWNOACTIVATE,//:以窗口最近一次的大小和状态显示窗口。激活窗口仍然维持激活状态。
            SW_SHOWNOMAL,//:激活并显示一个窗口。如果窗口被最小化或最大化,系统将其恢复到原来的尺寸和大小。应用程序在第一次显示窗口的时候应该指定此标志。
        }

        public const int SW_HIDE = 0;
        public const int SW_SHOWNORMAL = 1;
        public const int SW_SHOWMINIMIZED = 2;
        public const int SW_SHOWMAXIMIZED = 3;
        public const int SW_MAXIMIZE = 3;
        public const int SW_SHOWNOACTIVATE = 4;
        public const int SW_SHOW = 5;
        public const int SW_MINIMIZE = 6;
        public const int SW_SHOWMINNOACTIVE = 7;
        public const int SW_SHOWNA = 8;
        public const int SW_RESTORE = 9;

        [DllImport("User32.dll")]
        public static extern bool SetForegroundWindow(IntPtr hWnd);

        [DllImport("User32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        #endregion

        #region 控制鼠标移动

        //移动鼠标 
        public const int MOUSEEVENTF_MOVE = 0x0001;
        //模拟鼠标左键按下 
        public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
        //模拟鼠标左键抬起 
        public const int MOUSEEVENTF_LEFTUP = 0x0004;
        //模拟鼠标右键按下 
        public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
        //模拟鼠标右键抬起 
        public const int MOUSEEVENTF_RIGHTUP = 0x0010;
        //模拟鼠标中键按下 
        public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
        //模拟鼠标中键抬起 
        public const int MOUSEEVENTF_MIDDLEUP = 0x0040;
        //标示是否采用绝对坐标 
        public const int MOUSEEVENTF_ABSOLUTE = 0x8000;

        [Flags]
        public enum MouseEventFlag : uint
        {
            Move = 0x0001,
            LeftDown = 0x0002,
            LeftUp = 0x0004,
            RightDown = 0x0008,
            RightUp = 0x0010,
            MiddleDown = 0x0020,
            MiddleUp = 0x0040,
            XDown = 0x0080,
            XUp = 0x0100,
            Wheel = 0x0800,
            VirtualDesk = 0x4000,
            Absolute = 0x8000
        }

        //[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        [DllImport("user32.dll")]
        public static extern bool SetCursorPos(int X, int Y);
        [DllImport("user32.dll")]
        public static extern int mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

        #endregion

        #region 获取坐标钩子

        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int X;
            public int Y;
        }

        [StructLayout(LayoutKind.Sequential)]
        public class MouseHookStruct
        {
            public POINT pt;
            public int hwnd;
            public int wHitTestCode;
            public int dwExtraInfo;
        }

        public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);

        //安装钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
        //卸载钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);
        //调用下一个钩子
        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

        #endregion

    }
    

C# 系统应用之鼠标模拟技术及自动操作鼠标
c# 获取当前活动窗口句柄,获取窗口大小及位置
用Mouse_event()来控制鼠标操作


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
动态调用WebService(C#)(非常实用)发布时间:2022-07-13
下一篇:
【c++基础】vector中按照Point类型某一个变量进行排序发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap