首页 文章

如何将应用程序连接到最顶层的窗口?

提问于
浏览
2

我的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是最顶层的窗口,因为名称在Window的 Headers 栏中 . 但是,如果用户在Outlook中创建新邮件,则窗口 Headers 为“无 Headers 消息”,而不显示窗口中正在运行的应用程序的提示 .

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

感谢帮助!

1 回答

  • 2

    你需要pinvoke GetWindowThreadProcessId() . 这将获取拥有该窗口的进程的ID . 回到托管代码,Process.GetProcessById()为您提供了该过程的详细信息 .

相关问题