每个 XL 过程的 hWnd;每个过程都位于 Z 顺序的顶部

发布于 2024-11-29 17:51:17 字数 837 浏览 0 评论 0原文

这会连续激活每个正在运行的 XL 进程:

    Public Sub Test()
     Dim varAry()
     Dim iInstances As Long
     Dim hWndDesk As Long
     Dim hWndXL As Long
     Dim x As Long
     Dim var As Variant

     On Error Resume Next

    '---
     hWndDesk = GetDesktopWindow

     Do
      iInstances = iInstances + 1
      hWndXL = FindWindowEx(GetDesktopWindow, hWndXL, "XLMAIN", vbNullString)

      If hWndXL <> 0 Then
       ReDim Preserve varAry(iInstances)
    'Get the next Excel window
       varAry(iInstances) = hWndXL
      Else
       Exit Do
      End If
     Loop

    '---
     For x = 1 To UBound(varAry)
      MsgBox varAry(x)
      var = SwitchToThisWindow(hwnd:=varAry(x), BOOL:=False)
     Next x
exit_Sub
    End Sub

但是 GetObject() 不能连续应用于每个被激活的进程。我想使用这样的对象来计算每个进程下打开的工作簿的数量。

感谢您的帮助。

This successively activates every XL process that happens to be running:

    Public Sub Test()
     Dim varAry()
     Dim iInstances As Long
     Dim hWndDesk As Long
     Dim hWndXL As Long
     Dim x As Long
     Dim var As Variant

     On Error Resume Next

    '---
     hWndDesk = GetDesktopWindow

     Do
      iInstances = iInstances + 1
      hWndXL = FindWindowEx(GetDesktopWindow, hWndXL, "XLMAIN", vbNullString)

      If hWndXL <> 0 Then
       ReDim Preserve varAry(iInstances)
    'Get the next Excel window
       varAry(iInstances) = hWndXL
      Else
       Exit Do
      End If
     Loop

    '---
     For x = 1 To UBound(varAry)
      MsgBox varAry(x)
      var = SwitchToThisWindow(hwnd:=varAry(x), BOOL:=False)
     Next x
exit_Sub
    End Sub

But a GetObject() can't be applied successively to each of the processes that are activated. I'd like to use such an object to count the number of workbooks that are open under each process.

Thank you for any assistance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

新雨望断虹 2024-12-06 17:51:17

下面的代码将计算找到的“父”类的数量。

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                      ByVal childAfter As IntPtr, _
                      ByVal lclassName As String, _
                      ByVal windowTitle As String) As IntPtr
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim handleCount As Integer
        Dim hWnd As IntPtr
        'ConsoleWindowClass is for "Command Prompt" / cmd.exe in Windows.
        hWnd = FindWindow("ConsoleWindowClass", vbNullString)
        If hWnd.ToInt32 <> 0 Then handleCount += 1

        Do
            hWnd = FindWindowEx(0, hWnd, "ConsoleWindowClass", vbNullString)
            If hWnd.ToInt32 <> 0 Then
                handleCount += 1
            Else
                Exit Do
            End If

        Loop

        MessageBox.Show("Found " & handleCount.ToString() & " handles...", "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

End Class

从历史上看,我一直使用 Pat 或 JK 的 API Spy(从 98 年左右开始)来获取关于如何获取窗口句柄的 VBA/6 友好视图(为您生成 vb 代码,但建议对其进行编辑)

pInvoke.net 是所有 Win32 API 的重要资源,例如 FindWindowFindWindowEx 用于上面的例子。检查每个链接,因为它们是有关如何正确实现这些功能的大量示例。

The code below will calculate the number of "Parent" classes that are found.

Imports System.Runtime.InteropServices

Public Class Form1
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
    End Function
    <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
    Private Shared Function FindWindowEx(ByVal parentHandle As IntPtr, _
                      ByVal childAfter As IntPtr, _
                      ByVal lclassName As String, _
                      ByVal windowTitle As String) As IntPtr
    End Function
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim handleCount As Integer
        Dim hWnd As IntPtr
        'ConsoleWindowClass is for "Command Prompt" / cmd.exe in Windows.
        hWnd = FindWindow("ConsoleWindowClass", vbNullString)
        If hWnd.ToInt32 <> 0 Then handleCount += 1

        Do
            hWnd = FindWindowEx(0, hWnd, "ConsoleWindowClass", vbNullString)
            If hWnd.ToInt32 <> 0 Then
                handleCount += 1
            Else
                Exit Do
            End If

        Loop

        MessageBox.Show("Found " & handleCount.ToString() & " handles...", "Results", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

End Class

Historically I've always used Pat or JK's API Spy (since '98 or so) to obtain a VBA/6 friendly view of how to get the window handle (generates vb code for you, would recommend editing it though)

pInvoke.net is a great resource for all Win32 API's such as FindWindow and FindWindowEx used in the example above. Check the links for each as they're are extensive examples on how to properly implement the functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文