vba 将下一个窗口设置为活动窗口 (ALT+TAB)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14427929/
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-11 19:15:41  来源:igfitidea点击:

Set next window as active window (ALT+TAB)

vba

提问by Daan

I'm running a vbs file using a keyboard shortcut. Although the code runs fine, the problem with vbs shortcuts is that the window in the foreground loses its focus as soon as you press the keyboard shortcut. (You can try it for yourself by placing an empty vbs file somewhere, a shortcut to the file in e.g. the start menu folder, a keyboard shortcut assigned to the shortcut and pressing the keyboard shortcut.)

我正在使用键盘快捷键运行 vbs 文件。尽管代码运行良好,但 vbs 快捷键的问题在于,只要您按下键盘快捷键,前景中的窗口就会失去焦点。(您可以通过在某处放置一个空的 vbs 文件、在开始菜单文件夹中的文件快捷方式、分配给该快捷方式的键盘快捷方式并按下键盘快捷方式来亲自尝试。)

I found that by using ALT+TAB, I could get the foreground window to regain focus. However, I'm unable to repeat this functionality in VBA. Apparently, ShellObject.SendKeys("%{TAB}")doesn't work...

我发现通过使用 ALT+TAB,我可以让前景窗口重新获得焦点。但是,我无法在 VBA 中重复此功能。显然,ShellObject.SendKeys("%{TAB}")不起作用...

Is there any way to achieve the functionality of ALT+TAB in VBA? Thanks in advance.

有什么办法可以实现VBA中ALT+TAB的功能吗?提前致谢。

EDIT

编辑

In the meanwhile, I made a switch to AutoIt to see if it could get me further. This is what I got:

与此同时,我切换到 AutoIt,看看它是否能让我走得更远。这是我得到的:

ControlFocus("[CLASS:CabinetWClass]", "", "[CLASS:DirectUIHWND]")

I noticed that selecting the explorer window (i.e. CabinetWClass), is in some cases not enough. That's why I'm setting focus on the control that actually contains the files/folders.

我注意到在某些情况下选择资源管理器窗口(即CabinetWClass)是不够的。这就是为什么我将重点放在实际包含文件/文件夹的控件上。

It's working very well, but I'm still hoping for a VBA solution :)

它工作得很好,但我仍然希望有一个 VBA 解决方案:)

采纳答案by CuberChase

Can you use the Windows APIs to bring it back to the top? This works for me when run in Excel to bring the notepad to the top, so it should work. You would have to substitute the name of your window in the FindWindow call.

您可以使用 Windows API 将其带回顶部吗?当在 Excel 中运行将记事本带到顶部时,这对我有用,所以它应该可以工作。您必须在 FindWindow 调用中替换窗口的名称。

Note: You should be really carefull with some of the flags because you'll get some weird behaviour if you do some wrong combinations of them.

注意:您应该非常小心地使用某些标志,因为如果您将它们组合在一起,您会得到一些奇怪的行为。

Public Declare Function FindWindow Lib "user32" _
    Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long


Declare Function SetWindowPos Lib "user32.dll" (ByVal hwnd As _
        Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As _
        Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

'Either the handle of the window to position this window behind, or exactly one of the following flags stating where in the
'Z-order to put the window:
Private Const HWND_BOTTOM = 1             'Put the window at the bottom of the Z-order.
Private Const HWND_NOTOPMOST = -2         'Put the window below all topmost windows and above all non-topmost windows.
Private Const HWND_TOP = 0                'Put the window at the top of the Z-order.
Private Const HWND_TOPMOST = -1           'Make the window topmost (above all other windows) permanently.

'x: The x coordinate of where to put the upper-left corner of the window.
'y: The y coordinate of where to put the upper-left corner of the window.
'cx: The x coordinate of where to put the lower-right corner of the window.
'cy: The y coordinate of where to put the lower-right corner of the window.

'Flags: Zero or more of the following flags stating how to move the window:
Private Const SWP_DRAWFRAME = &H20            'Same as SWP_FRAMECHANGED.
Private Const SWP_FRAMECHANGED = &H20         'Fully redraw the window in its new position.
Private Const SWP_HIDEWINDOW = &H80           'Hide the window from the screen.
Private Const SWP_NOACTIVATE = &H10           'Do not make the window active after moving it unless it was already the active window.
Private Const SWP_NOCOPYBITS = &H100          'Do not redraw anything drawn on the window after it is moved.
Private Const SWP_NOMOVE = &H2                'Do not move the window.
Private Const SWP_NOSIZE = &H1                'Do not resize the window.
Private Const SWP_NOREDRAW = &H8              'Do not remove the image of the window in its former position, effectively leaving a ghost image on the screen.
Private Const SWP_NOZORDER = &H4              'Do not change the window's position in the Z-order.
Private Const SWP_SHOWWINDOW = &H40           'Show the window if it is hidden.


Sub ShowWindow()
    Dim hwnd As Long 'handle to get the window
    Dim flags As Long ' the flags specifying how to move the window
    Dim retval As Long ' return value

    hwnd = FindWindow(vbNullString, "Untitled - Notepad")
    flags = SWP_NOSIZE Or SWP_DRAWFRAME
    retval = SetWindowPos(hwnd, HWND_TOP, 0, 0, 1, 1, flags) ' move the window
End Sub

回答by CC Inc

Maybe try the AppActivatefunction, if you know the name of the item you are switching too, which you may not know in this case.

也许尝试AppActivate功能,如果您也知道要切换的项目的名称,在这种情况下您可能不知道。

Otherwise, try this:

否则,试试这个:

SendKeys "%{TAB}", True

回答by Alexei Martianov

ActiveWindow.ActivateNextand possibly similar might be useful.

ActiveWindow.ActivateNext并且可能类似的可能有用。