【问题标题】:How to make a window active in vb.net如何在 vb.net 中激活一个窗口
【发布时间】:2014-01-09 23:59:47
【问题描述】:

所以我正在开发一个程序,该程序可以键入一个没有 api 的开放应用程序。所以我需要选择那个窗口,这样我的程序就会在里面输入。我正在使用此代码,但找不到该过程。

Dim windowHandle As IntPtr = FindWindow("LolClient", "League of Legends (TM) Client")
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As IntPtr
SetForegroundWindow(windowHandle)

    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait("{TAB}")
    SendKeys.SendWait(ComboBox1.Text)
    SendKeys.SendWait("{Enter}")

【问题讨论】:

  • 您应该尝试查看 Process Class,在许多情况下它更容易使用。

标签: vb.net windows


【解决方案1】:

试试这个:

Public Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Integer) As Integer
Public Declare Auto Function FindWindow Lib "user32.dll" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Public Declare Function IsIconic Lib "user32.dll" (ByVal hwnd As Integer) As Boolean
Public Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer

Public Const SW_RESTORE As Integer = 9
Public Const SW_SHOW As Integer = 5


Sub FocusWindow(ByVal strWindowCaption As String, ByVal strClassName As String)
    Dim hWnd As Integer
    hWnd = FindWindow(strClassName, strWindowCaption)

    If hWnd > 0 Then
        SetForegroundWindow(hWnd)

        If IsIconic(hWnd) Then  'Restore if minimized
            ShowWindow(hWnd, SW_RESTORE)
        Else
            ShowWindow(hWnd, SW_SHOW)
        End If
    End If
End Sub

要显示“计算器”,您可以致电FocusWindow("Calculator", Nothing)FocusWindow(Nothing, "CalcFrame")

【讨论】:

  • 谢谢!这是我的项目缺少的链接。
  • 你如何通过进程名称而不是窗口标题来选择窗口?
猜你喜欢
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多