加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:46:17 所属栏目:Windows 来源:网络整理
导读:在主题…或更好的 – 如何从顶部窗口更改事件时获取这些信息? 感谢提示.所以我应该使用P / Invoke.这是完整的代码: using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;namespace CuckooCoach{ class
在主题…或更好的 – 如何从顶部窗口更改事件时获取这些信息?
感谢提示.所以我应该使用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();
        }

    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读