Excel VBA 使用 Windows API 控制其他窗口的显示状态

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long

Private hwnd As Long
Private key As String



Public Function GetPid(ByVal name As String) As Long
    Dim procs As Object, proc As Object
    Set procs = GetObject("WinMgmts:").InstancesOf("Win32_Process")
    GetPid = 0
    For Each proc In procs
        If proc.Caption = name Then
            GetPid = proc.processid
        End If
    Next
End Function

Public Function FindWindowLike(ByVal lpWindowName As String) As Long
        key = lpWindowName
        hwnd = 0
        EnumWindows AddressOf EnumWindowsProc, &O0
        FindWindowLike = hwnd
End Function

Private Function EnumWindowsProc(ByVal handle As Long, ByVal lParam As Long) As Boolean
        Dim tmpstr As String * 251
        
        If hwnd = 0 Then
                GetWindowText handle, tmpstr, 250
                If InStr(tmpstr, key) > 0 Then
                        hwnd = handle
                End If
        End If
        EnumWindowsProc = True
End Function

Public Sub MaximizeWindow(ByVal handle As Long)
        ShowWindow handle, 3
End Sub

 

posted on 2013-07-25 16:31  chillyrains  阅读(1902)  评论(0)    收藏  举报

导航