多次调用BringWindowToTop无法正常工作

时间:2013-07-18 06:14:57

标签: vba findwindow

我无法理解以下代码的问题 -

i = 15
While (i < 100)
    If i Mod 2 = 0 Then
        handle = FindWindow(vbNullString, "My Details - Windows Internet Explorer")
        Range("A1").Value = handle
        BringWindowToTop handle
        Application.Wait DateAdd("s", 1, Now)
    Else
        handle1 = FindWindow(vbNullString, "Codeomnitrix - Outlook Web App - Mozilla Firefox")
        Range("A2").Value = handle1
        BringWindowToTop handle1
        Application.Wait DateAdd("s", 1, Now)
    End If

    i = i + 15
Wend

它应该在两个窗口之间切换并让它们聚焦到1秒,但实际发生的是它只是将Firefox置于顶部然后没有切换。

由于

2 个答案:

答案 0 :(得分:1)

如果您考虑使用Autoit,这可能非常简单。

添加对autoit dll的引用。

enter image description here

Download Autoit

Sub test()

   Dim oAutoit As New AutoItX3
   oAutoit.Opt "WinTitleMatchMode", 2  ' Match any substring in the title
   oAutoit.WinActivate "My Details - Windows Internet Explorer" ' Activates (gives focus to) a window.

End Sub

答案 1 :(得分:0)

我发现激活时不会弹出最小化窗口。

Private Declare Function GetWindowPlacement Lib _
        "user32" (ByVal hwnd As Integer, _
        ByRef lpwndpl As WINDOWPLACEMENT) As Integer
Private Declare Function SetWindowPlacement Lib "user32" _
       (ByVal hwnd As Integer, ByRef lpwndpl As WINDOWPLACEMENT) As Integer

Private Const SW_SHOWMINIMIZED As Short = 2
Private Const SW_SHOWMAXIMIZED As Short = 3
Private Const SW_SHOWNORMAL As Short = 1

Private Structure WINDOWPLACEMENT
    Dim length As Integer
    Dim flags As Integer
    Dim showCmd As Integer
    Dim ptMinPosition As POINTAPI
    Dim ptMaxPosition As POINTAPI
    Dim rcNormalPosition As RECT
End Structure

[...]

Dim handle As Integer
Dim wp As WINDOWPLACEMENT

handle = FindWindow(vbNullString, "My Details - Windows Internet Explorer")
GetWindowPlacement(handle, wp)
wp.showCmd = SW_SHOWNORMAL
SetWindowPlacement(handle, wp)

所有这些都从Codeproject article复制以恢复窗口。