【问题标题】:How to get what application is connected to the topmost window?如何获取连接到最顶层窗口的应用程序?
【发布时间】:2010-08-25 10:15:40
【问题描述】:

我的 VB.NET 应用程序应该在最上面的窗口中监视当前正在运行的应用程序。我使用计时器尝试了以下方法:

Declare Function GetActiveWindow Lib "user32" () As System.IntPtr
Declare Function GetForegroundWindow Lib "user32" () As System.IntPtr
Public Declare Auto Function GetWindowText Lib "user32" _
(ByVal hWnd As System.IntPtr, _
ByVal lpString As System.Text.StringBuilder, _
ByVal cch As Integer) As Integer 

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
   Dim Caption As New System.Text.StringBuilder(256)
   Dim hWnd As IntPtr = GetForegroundWindow()
   GetWindowText(hWnd, Caption, Caption.Capacity)
   'Caption now holds the title of the topmost window
End Sub 

由此我可以看到,例如 Outlook 或 Internet Explorer 是最顶层的窗口,因为名称位于窗口的标题栏中。但是,如果用户在 Outlook 中创建了一封新邮件,则窗口的标题是“未命名的邮件”,不会提示窗口中正在运行什么应用程序。

如何获取连接到最顶层窗口的应用程序?p>

感谢您的帮助!

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您需要调用 GetWindowThreadProcessId()。这将为您提供拥有该窗口的进程的 ID。回到托管代码,Process.GetProcessById() 为您提供流程的详细信息。

    【讨论】:

      猜你喜欢
      • 2018-10-06
      • 1970-01-01
      • 2010-11-10
      • 2019-09-22
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多