如何获取Windows / C#顶层窗口的进程名称和标题

主题…或更好的 – 如何从顶部窗口更改事件时获取这些信息?
感谢提示.所以我应该使用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,//      __out     LPTSTR lpFilename,//      __in      DWORD nSize
        //  );
        [DllImport("psapi.dll")]
        private static extern uint GetmodulefileNameEx(IntPtr hWnd,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.Capacity);
            GetmodulefileNameEx(hProcess,text.Capacity);

            CloseHandle(hProcess); 

            return text.ToString();
        }

    }
}

相关文章

Windows2012R2备用域控搭建 前置操作 域控主域控的主dns:自...
主域控角色迁移和夺取(转载) 转载自:http://yupeizhi.blo...
Windows2012R2 NTP时间同步 Windows2012R2里没有了internet时...
Windows注册表操作基础代码 Windows下对注册表进行操作使用的...
黑客常用WinAPI函数整理之前的博客写了很多关于Windows编程的...
一个简单的Windows Socket可复用框架说起网络编程,无非是建...