VB.NET发送Tab键到另一个应用程序窗口 [英] VB.NET Send Tab key to another application window
问题描述
我想将"{TAB}"密钥发送到另一个应用程序窗口(将密钥发送到窗口而不是文本框).
I want to send "{TAB}" Key to another application window(send the key to the window not to textbox).
我尝试过:
SendMessage(hWnd, WM_SETHOTKEY, VK_TAB, 0)
什么都没发生.
我的目标是:
当应用程序窗口未聚焦时,将Tab键发送到我的应用程序或.
(我知道sendkey在这种情况下是不专业的(这是我第一次使用它).)
Nothing happened.
my goal is:
send tab key to my application Or other application when the application window is not in focus.
(i know that sendkey is not professional in this case there is no choice(This is the first time that I'm using it).)
我做了很多尝试,但我总是返回相同的结果:
I made many attempts and I always returned to the same result:
什么都没发生.
有人知道答案吗?
推荐答案
SendKeys 要求您要向其发送密钥的应用程序处于活动状态.
SendKeys requires the application that you are sending the Keys to, to be active.
从上方链接:
使用SendKeys将击键和击键组合发送到活动应用程序.
Use SendKeys to send keystrokes and keystroke combinations to the active application.
为了解决此限制,您将不得不使用WinApi函数.
I order to get around this limitation you will have to resort to using the WinApi Functions.
- FindWindow pInvoke.net
- FindWindowEx pInvoke.net
- sendMessage pInvoke.net
请参见 MSDN论坛帖子例如
这是该帖子的修改示例:
Here is a modified example from that Posting:
Public Class Form1
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd As IntPtr, ByVal hWndChildAfterA As IntPtr, ByVal lpszClass As String, ByVal lpszWindow As String) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As String) As IntPtr
Const WM_SETTEXT As Integer = &HC
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim destination As IntPtr = FindWindow(Nothing, "Untitled - Notepad")
Dim destControl As IntPtr = FindWindowEx(destination, IntPtr.Zero, "Edit", Nothing)
SendMessage(destControl, WM_SETTEXT, IntPtr.Zero, "Hello" & vbTab & "GoodBye" & vbCrLf)
End Sub
End Class
使用WM_KEYDOWN
添加了一个附加示例,我创建了另一个小应用程序,其窗口标题"设置为TestForm,并覆盖了WndProc方法以确定该应用程序是否获得了TabKey.
Added an Additional Example using WM_KEYDOWN
I created another small application with the Window Title set to TestForm and overrode the WndProc Method to determine if the application got the TabKey.
发送表单
Public Class Form1
Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As IntPtr
Const WM_KEYDOWN As Integer = &H100
Const VK_TAB As Integer = &H9
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim destination As IntPtr = FindWindow(Nothing, "TestForm")
SendMessage(destination, WM_KEYDOWN, VK_TAB, 0)
End Sub
End Class
测试表格
在MyBase.WndProc(m)上放置一个断点,然后查看m以查看已发送的内容.
Put a breakpoint on MyBase.WndProc(m) and look at m to see what has been sent.
Public Class Form1
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
MyBase.WndProc(m)
End Sub
End Class
这篇关于VB.NET发送Tab键到另一个应用程序窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!