【发布时间】: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