wpf中怎么编辑用键盘来下达指令
1个回答
2017-06-15
展开全部
现在网上的库大部分都是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");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
这里现在也还有很多我还不是很了解的地方,等以后有时间再说吧,现在要赶项目。
最后补充一下,它们其实就是很多个WIN32的API
[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");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
这里现在也还有很多我还不是很了解的地方,等以后有时间再说吧,现在要赶项目。
最后补充一下,它们其实就是很多个WIN32的API
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询