VB.NET 将 Tab 键发送到另一个应用程序窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13147529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 10:57:27  来源:igfitidea点击:

VB.NET Send Tab key to another application window

vb.netwinformssendmessagepostmessagewindows-messages

提问by Nmmmm

I want to send "{TAB}" Key to another application window(send the key to the window not to textbox).

我想将“{TAB}”键发送到另一个应用程序窗口(将键发送到窗口而不是文本框)。

I tried:

我试过:

SendMessage(hWnd, WM_SETHOTKEY, VK_TAB, 0)

Nothing happened.
my goal is:
send tab key to my application Orother application when the application window is notin 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).)

什么都没有发生。
我的目标是: 当应用程序窗口处于焦点时,
将 Tab 键发送到我的应用程序其他应用程序。 (我知道在这种情况下 sendkey 不专业,别无选择(这是我第一次使用它)。)

I made many attempts and I always returned to the same result:

我做了很多尝试,但总是返回相同的结果:

Nothing happened.

什么都没有发生。

Does anyone know the answer?

有人知道答案吗?

回答by Mark Hall

SendKeysrequires the application that you are sending the Keys to, to be active.

SendKeys要求您将密钥发送到的应用程序处于活动状态。

From above Link:

从上面的链接:

Use SendKeys to send keystrokes and keystroke combinations to the active application.

使用 SendKeys 将击键和击键组合发送到活动应用程序。

I order to get around this limitation you will have to resort to using the WinApi Functions.

我为了绕过这个限制,你将不得不求助于使用 WinApi 函数。

  1. FindWindowpInvoke.net
  2. FindWindowExpInvoke.net
  3. sendMessagepInvoke.net
  1. FindWindow pInvoke.net
  2. FindWindowEx pInvoke.net
  3. sendMessage pInvoke.net

See this MSDN Forum Postfor an example

有关示例,请参阅此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


Added an Additional Example using WM_KEYDOWNI created another small application with the Window Title set to TestForm and overrode the WndProc Method to determine if the application got the TabKey.

添加了一个附加示例,使用WM_KEYDOWN我创建了另一个小应用程序,并将窗口标题设置为 TestForm 并覆盖 WndProc 方法以确定应用程序是否获得了 TabKey。

Sending Form

发送表格

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

Test Form

测试表

Put a breakpoint on MyBase.WndProc(m) and look at m to see what has been sent.

在 MyBase.WndProc(m) 上放置一个断点并查看 m 以查看已发送的内容。

Public Class Form1

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)
    End Sub

End Class

回答by Steve

Having struggled with this type of this a few times before, i would suggest a couple of things to look at.

之前在这种类型的情况下挣扎过几次,我建议您看几件事情。

The 1st is autoitwhich includes a dll you can reference from vb.net, and is very simple you use, and well documented. I tend to use that whenever i need to control a 3rd party program.

第一个是autoit,它包含一个可以从 vb.net 引用的 dll,使用起来非常简单,并且有据可查。每当我需要控制第 3 方程序时,我都倾向于使用它。

The other is the ui automation classesSee this for an example:

另一个是ui自动化类,看这个例子:

http://blog.functionalfun.net/2009/06/introduction-to-ui-automation-with.html

http://blog.functionalfun.net/2009/06/introduction-to-ui-automation-with.html

回答by urlreader

you need make the other window active first. check Change focus to another window in VB.NET. then use send key.

您需要先激活另一个窗口。检查将焦点更改为 VB.NET 中的另一个窗口。然后使用发送密钥。