首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将下一个窗口设置为活动窗口(ALT+TAB)

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

Stack Overflow用户
提问于 2013-01-20 18:45:07
回答 3查看 9.6K关注 0票数 1

我正在使用键盘快捷方式运行一个vbs文件。虽然代码运行良好,但vbs快捷方式的问题是,只要按下键盘快捷方式,前台窗口就会失去焦点。(您可以自己尝试,方法是在某个地方放置一个空的vbs文件,在“开始”菜单文件夹中放置该文件的快捷方式,指定给该快捷方式的键盘快捷方式,并按下键盘快捷方式。)

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

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

编辑

与此同时,我转到了AutoIt,看看它是否能让我更进一步。这就是我得到的:

代码语言:javascript
复制
ControlFocus("[CLASS:CabinetWClass]", "", "[CLASS:DirectUIHWND]")

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-20 23:14:01

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

注意:你真的应该小心一些旗帜,因为如果你做了一些错误的组合,你会得到一些奇怪的行为。

代码语言:javascript
复制
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
票数 0
EN

Stack Overflow用户

发布于 2013-01-20 18:48:59

如果您也知道要切换的项的名称,请尝试AppActivate函数,在本例中您可能不知道这个名称。

否则,尝试如下:

代码语言:javascript
复制
SendKeys "%{TAB}", True
票数 0
EN

Stack Overflow用户

发布于 2019-04-04 08:55:38

ActiveWindow.ActivateNext和可能类似的可能是有用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14427929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档