How to make a window active in vb.net

后端 未结 1 1896
既然无缘
既然无缘 2021-01-26 09:30

So I\'m working on a program that types into an open application that has no api. So I need to select that window so my program will type into it. I\'m using this code and it ca

相关标签:
1条回答
  • 2021-01-26 09:56

    Try this:

    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
    

    To show the "Calculator" you can call FocusWindow("Calculator", Nothing) or FocusWindow(Nothing, "CalcFrame")

    0 讨论(0)
提交回复
热议问题