WPF向指定窗口发送键盘指令

来源:互联网 发布:淘宝热卖排行 编辑:程序博客网 时间:2024/06/07 20:22

现在网上的库大部分都是VB的那个发送键盘指令的库,那个在WPF里面是用不了的,WPF的话可以用System.Windows.Forms中的其实跟VB中的Microsoft.VisualBasic.Devices这个里面的SendKeys用法差不多一样,吐槽一下,新的这个也可以在VB中使用好像,我看MSDN中Forms中的SendKeys也能在 VB中用,真是这一个小问题浪费了我一下午的时间。贴上代码

[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);        [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;        private void xin(object sender, RoutedEventArgs e)        {            Thread.Sleep(3000);            IntPtr handle = FindWindow(null, "vshost32.exe");            handle = FindWindowEx(handle, IntPtr.Zero, "Edit", null);            if (handle == IntPtr.Zero)            {                Console.WriteLine("没有找到句柄");            //    return;            }            SendMessage(handle, WM_SETTEXT, IntPtr.Zero, "d");  //          SendKeys.Send("{TAB}");            //SendMessage(handle, WM_CHAR, (IntPtr)VK_RETURN, IntPtr.Zero);//Enter            SendKeys.SendWait("{d}");           // SendMessage(handle, WM_SETTEXT, IntPtr.Zero, "w");        }

这里现在也还有很多我还不是很了解的地方,等以后有时间再说吧,现在要赶项目。
最后补充一下,它们其实就是很多个WIN32的API

0 0
原创粉丝点击