如何在vb dot net中显示excel文件的所有线程或实例.我想显示excel文件的所有线程和一个主进程 [英] How to show all the threads or instances of excel file in vb dot net.I want to show all the threads and one main process of excel files

查看:19
本文介绍了如何在vb dot net中显示excel文件的所有线程或实例.我想显示excel文件的所有线程和一个主进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 vb dot net 中显示 Excel 文件的所有线程或实例?

How can I show all the threads or instances of Excel file in vb dot net?

我想展示Excel文件的所有线程和一个主要进程.我已经看到了进程和进程 ID,但没有显示 Excel 的所有线程,这意味着如果我打开三个 Excel 文件,我可以在列表框中显示三个文件.

I want to show all the threads and one main process of Excel files. I have seen the process and process id but not a way to show all threads of Excel, meaning if I open three Excel files I can show three files in listbox.

推荐答案

我不确定是否有一种简单的方法可以做到这一点.这是我从类似情况修改的一些代码.首先这是一个从指定进程 id 获取子窗口标题的类:

I am not sure there is an easy way to do this. Here is some code that I have modified from a similar situation. Firstly here is a class that gets the child window titles from a specified process id:

Class ChildWindowManager
    Delegate Function EnumThreadDelegate(ByVal hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean

    <DllImport("user32.dll")>
    Private Shared Function EnumThreadWindows(ByVal dwThreadId As Integer, ByVal lpfn As EnumThreadDelegate, ByVal lParam As IntPtr) As Boolean
    End Function

    <DllImport("user32.dll")>
    Public Shared Function GetWindowText(ByVal hwnd As Integer, ByVal lpString As System.Text.StringBuilder, ByVal cch As Integer) As Integer
    End Function

    <DllImport("user32.dll")>
    Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
    End Function

    Private Shared Function EnumerateProcessWindowHandles(ByVal processId As Integer) As List(Of IntPtr)
        Dim windowHandles = New List(Of IntPtr)()

        For Each thread As ProcessThread In Process.GetProcessById(processId).Threads
            EnumThreadWindows(thread.Id, Function(hWnd, lParam)
                                             windowHandles.Add(hWnd)
                                             Return True
                                         End Function, IntPtr.Zero)
        Next

        Return windowHandles
    End Function

    Private Shared Function GetWindowTitle(ByVal hWnd As IntPtr) As String
        Dim length As Integer = GetWindowTextLength(hWnd)
        If length = 0 Then Return Nothing

        Dim titleStringBuilder As New Text.StringBuilder("", length)

        GetWindowText(hWnd, titleStringBuilder, titleStringBuilder.Capacity + 1)
        Return titleStringBuilder.ToString()
    End Function

    Public Shared Function GetChildWindowTitles(processId As Integer) As List(Of String)
        Dim windowTitles As New List(Of String)

        For Each handle In EnumerateProcessWindowHandles(processId)
            Dim windowText = GetWindowTitle(handle)
            If windowText <> Nothing Then
                windowTitles.Add(windowText)
            End If
        Next

        Return windowTitles
    End Function

End Class

该类使用EnumThreadWindows 函数获取子窗口句柄列表,然后使用GetWindowTextLengthGetWindowText 获取这些子窗口的实际标题(如果有的话).

The class works by using the EnumThreadWindows function to get a list of the handles of child windows and then uses GetWindowTextLength and GetWindowText to get the actual title of those child windows (if they have one).

这是一个简单的控制台应用程序,展示了如何使用该类:

Here is a simple console app showing how to use the class:

Imports System.Runtime.InteropServices

Sub Main()
    Dim allProcesses = Process.GetProcesses().Where(Function(p) p.ProcessName.Contains("EXCEL"))
    Dim windowTitles = ChildWindowManager.GetChildWindowTitles(allProcesses.First().Id)

    For Each title In windowTitles
        If (title.Contains("- Excel")) Then
            Console.WriteLine(title)
        End If
    Next

    Console.ReadLine()
End Sub

此代码获取包含名称 EXCEL 的进程.通常只有其中之一.然后它使用它来获取所有包含文本-Excel"的子窗口.这应该会为您提供所需的列表.

This code gets the processes that contain the name EXCEL. There is normally only one of these. It then uses that get all the child windows that have the text '- Excel' in them. This should give you the list you require.

这篇关于如何在vb dot net中显示excel文件的所有线程或实例.我想显示excel文件的所有线程和一个主进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆