• C#模拟键盘输入


      主要使用了Windows API 实现,你可以在你C盘下的system32文件夹中找到user32.dll,函数的说明在MSDN都有,只需要拿名字去搜一下就行

      根据窗口的类名和窗口名称获取窗口句柄,成功返回一个窗口的句柄,否则返回0:

            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

      在窗口列表中寻找与指定条件相符的第一个子窗口

            [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
            static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClass, string lpszWindow);

      找到句柄后向窗口发送消息,SendMessage方法有很多的重载

            [DllImport("user32.dll ", EntryPoint = "SendMessage")]
            public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
    
            [DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
            const int WM_CHAR = 0x0102;
            const int WM_SETTEXT = 0x000C;
            const int VK_RETURN = 0x0d;
    
            static void Main(string[] args)
            {
                IntPtr handle = FindWindow(null, "Hello.txt - 记事本");
                handle = FindWindowEx(handle, IntPtr.Zero, "Edit", null);
                if (handle == IntPtr.Zero)
                {
                    Console.WriteLine("没有找到句柄");
                    return;
                }
                SendMessage(handle, WM_SETTEXT, IntPtr.Zero, "Hello word!");
    SendMessage(handle, WM_CHAR, (IntPtr)VK_RETURN, IntPtr.Zero);
    //Enter
    }

    相关链接:

    虚拟键表:http://baike.baidu.com/view/555571.htm

    SendMessage消息类型:http://baike.baidu.com/view/1080187.htm

  • 相关阅读:
    Python学习摘要201802
    机器学习-梯度下降参数调优小结
    用尽洪荒之力学习Flask源码
    Flask类的属性和方法大全
    Flask第三方工具组件介绍
    Flask自带的常用组件介绍
    Centos下部署Flask
    Python Tips阅读摘要
    web程序设计关于我们
    软工实践总结
  • 原文地址:https://www.cnblogs.com/liunlls/p/user32_SendMessage.html
Copyright © 2020-2023  润新知