【问题标题】:How to get process name and title of the top window on Windows / C#如何在 Windows / C# 上获取顶部窗口的进程名称和标题
【发布时间】:2010-04-14 06:52:04
【问题描述】:

如主题......或更好 - 当顶部窗口发生变化时如何从事件中获取此信息?

【问题讨论】:

  • 你的意思是如何获得有焦点的窗口?
  • 是的,有焦点的窗口。这可以在 WinAPI 中使用 GetForegroundWindow() 完成。

标签: c# windows process


【解决方案1】:

感谢您的提示。所以无论如何我都应该使用 P/Invoke。完整代码如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace CuckooCoach
{
    class Monitor
    {
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();

        [DllImport("user32.dll")]
        static extern int GetWindowTextLength(IntPtr hWnd);

        //  int GetWindowText(
        //      __in   HWND hWnd,
        //      __out  LPTSTR lpString,
        //      __in   int nMaxCount
        //  );
        [DllImport("user32.dll")]
        private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);

        //  DWORD GetWindowThreadProcessId(
        //      __in   HWND hWnd,
        //      __out  LPDWORD lpdwProcessId
        //  );
        [DllImport("user32.dll")]
        private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

        //HANDLE WINAPI OpenProcess(
        //  __in  DWORD dwDesiredAccess,
        //  __in  BOOL bInheritHandle,
        //  __in  DWORD dwProcessId
        //);
        [DllImport("kernel32.dll")]
        private static extern IntPtr OpenProcess(uint dwDesiredAccess, bool bInheritHandle, uint dwProcessId);

        [DllImport("kernel32.dll")]
        private static extern bool CloseHandle(IntPtr handle);

        //  DWORD WINAPI GetModuleBaseName(
        //      __in      HANDLE hProcess,
        //      __in_opt  HMODULE hModule,
        //      __out     LPTSTR lpBaseName,
        //      __in      DWORD nSize
        //  );
        [DllImport("psapi.dll")]
        private static extern uint GetModuleBaseName(IntPtr hWnd, IntPtr hModule, StringBuilder lpFileName, int nSize);

        //  DWORD WINAPI GetModuleFileNameEx(
        //      __in      HANDLE hProcess,
        //      __in_opt  HMODULE hModule,
        //      __out     LPTSTR lpFilename,
        //      __in      DWORD nSize
        //  );
        [DllImport("psapi.dll")]
        private static extern uint GetModuleFileNameEx(IntPtr hWnd, IntPtr hModule, StringBuilder lpFileName, int nSize);

        public static string GetTopWindowText()
        {
            IntPtr hWnd = GetForegroundWindow();
            int length = GetWindowTextLength(hWnd);
            StringBuilder text = new StringBuilder(length + 1);
            GetWindowText(hWnd, text, text.Capacity);
            return text.ToString();
        }

        public static string GetTopWindowName()
        {
            IntPtr hWnd = GetForegroundWindow();
            uint lpdwProcessId;
            GetWindowThreadProcessId(hWnd, out lpdwProcessId);

            IntPtr hProcess = OpenProcess(0x0410, false, lpdwProcessId);

            StringBuilder text = new StringBuilder(1000);
            //GetModuleBaseName(hProcess, IntPtr.Zero, text, text.Capacity);
            GetModuleFileNameEx(hProcess, IntPtr.Zero, text, text.Capacity);

            CloseHandle(hProcess); 

            return text.ToString();
        }

    }
}

【讨论】:

  • 请注意,您可以将自己的答案标记为有效答案
【解决方案2】:

您可以使用以下代码按进程名称查找窗口句柄:

 Process[] processes = Process.GetProcessesByName(m.ProcessName);
                                    if (processes != null && processes.Length > 0)
                                    {
                                        Process process = processes[0];
                                        process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                                        process.Refresh();
                                        if (process.MainWindowHandle != IntPtr.Zero)
                                        { // process.mainwindows handle is needed for you

你可以通过句柄 title = GetWindowTitle(process.MainWindowHandle); 找到窗口文本

private String GetWindowTitle(IntPtr hWn)
        {
            object LParam = new object();
            int WParam = 0;
            StringBuilder title = new StringBuilder(1024);
            SendMessage(hWn, WM_GETTEXT, WParam, LParam);
            GetWindowText(hWn, title, title.Capacity);
            return title.ToString();

        }

调用winapi函数需要以下声明:

 [DllImport("user32", CharSet = CharSet.Auto, SetLastError = true)]
        internal static extern int GetWindowText(IntPtr hWnd, [Out, MarshalAs(UnmanagedType.LPTStr)] StringBuilder lpString, int nMaxCount);
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, object lParam);

【讨论】:

    【解决方案3】:

    我认为没有.Net 方法可以做到这一点,所以我认为您将使用 PInvoke。

    这里有一些示例代码http://www.pinvoke.net/default.aspx/user32.getforegroundwindow

    从该链接中可以看出,如果您不想自己处理 PInvoke 代码,有一个项目(托管 Windows API)将其封装在托管代码中。

    【讨论】:

      【解决方案4】:

      对于窗口标题,您必须使用从 GetForegroundWindow() 返回的 HWND 执行 GetWindowText 的 P/Invoke。

      至于流程信息,我相信 P/Invoking GetWindowModuleFileName 应该可以工作。

      【讨论】:

      • GetWindowText 有效,但 GetWindowModuleFileName 给出了奇怪的结果 - 有时是自己的进程名称,有时是空字符串,从不是顶部窗口的进程名称。
      • 糟糕,请阅读 support.microsoft.com/kb/228469 那里 GetWindowModuleFileName 仅适用于 Win95/98。我会检查其他可能性。
      • 好的,发现这篇文章@StackOverflow stackoverflow.com/questions/277085/… 应该可以解决您的问题。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-30
      相关资源
      最近更新 更多