【问题标题】:Get the process name of the window that is currently active and in focus using VB. Net使用 VB 获取当前活动且处于焦点的窗口的进程名称。网
【发布时间】:2013-05-21 22:11:28
【问题描述】:

我一直在寻找我问题的答案,但没有找到任何相关信息,所以我来找你看看你能不能帮忙。

有没有什么方法可以使用 VB 获取当前处于活动状态并处于焦点的窗口的进程名称。网?

提前致谢

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您必须使用 pinvoke。 GetForegroundWindow 获取前台窗口,GetWindowThreadProcessId 获取拥有它的进程的ID。剩下的就很简单了,Process.GetProcessById() 就可以找到进程了。访问 pinvoke.net 网站以获取声明。

    【讨论】:

      【解决方案2】:

      这是解决此问题的代码。我必须使用“GetForegroundWindow API”函数。

      ' The hWnd of the most recently found window.
      Private m_LastHwnd As Integer
      
      Private Sub tmrGetFgWindow_Tick(ByVal sender As _
          System.Object, ByVal e As System.EventArgs) Handles _
          tmrGetFgWindow.Tick
          ' Get the window's handle.
          Dim fg_hwnd As Long = GetForegroundWindow()
      
          ' If this is the same as the previous foreground window,
          ' let that one remain the most recent entry.
          If m_LastHwnd = fg_hwnd Then Exit Sub
          m_LastHwnd = fg_hwnd
      
          ' Display the time and the window's title.
          Dim list_item As System.Windows.Forms.ListViewItem
          list_item = _
              lvwFGWindow.Items.Add(Text:=Now.ToString("h:mm:ss"))
          list_item.SubItems.Add(GetWindowTitle(fg_hwnd))
          list_item.EnsureVisible()
      End Sub
      
      ' Return the window's title.
      Private Function GetWindowTitle(ByVal window_hwnd As _
          Integer) As String
          ' See how long the window's title is.
          Dim length As Integer
          length = GetWindowTextLength(window_hwnd) + 1
          If length <= 1 Then
              ' There's no title. Use the hWnd.
              Return "<" & window_hwnd & ">"
          Else
              ' Get the title.
              Dim buf As String = Space$(length)
              length = GetWindowText(window_hwnd, buf, length)
              Return buf.Substring(0, length)
          End If
      End Function
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-25
        相关资源
        最近更新 更多