C# 如何获取当前重点关注的应用程序名称

C# 如何获取当前重点关注的应用程序名称,c#,winforms,C#,Winforms,我使用的是windows窗体,我想知道用户是否可以获得其他应用程序的名称,这些应用程序是否可以获得焦点。 e、 g如果我运行一个应用程序,那么我应该得到该应用程序的名称,该应用程序最近启动并且当前处于焦点状态。 我在谷歌上搜索,但没有找到任何合适的解决方案 谢谢 [DllImport(“user32.dll”)] [DllImport("user32.dll")] static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint

我使用的是windows窗体,我想知道用户是否可以获得其他应用程序的名称,这些应用程序是否可以获得焦点。 e、 g如果我运行一个应用程序,那么我应该得到该应用程序的名称,该应用程序最近启动并且当前处于焦点状态。 我在谷歌上搜索,但没有找到任何合适的解决方案

谢谢

[DllImport(“user32.dll”)]
[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

public string GetActiveWindowTitle()
{
    var handle = GetForegroundWindow();
    string fileName = "";
    string name = "";
    uint pid = 0;
    GetWindowThreadProcessId(handle, out pid);

    Process p = Process.GetProcessById((int)pid);
    var processname = p.ProcessName;

    switch (processname)
    {
        case "explorer": //metro processes
        case "WWAHost":
            name = GetTitle(handle);
            return name;
        default:
            break;
    }
    string wmiQuery = string.Format("SELECT ProcessId, ExecutablePath FROM Win32_Process WHERE ProcessId LIKE '{0}'", pid.ToString());
    var pro = new ManagementObjectSearcher(wmiQuery).Get().Cast<ManagementObject>().FirstOrDefault();
    fileName = (string)pro["ExecutablePath"];
    // Get the file version
    FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(fileName);
    // Get the file description
    name = myFileVersionInfo.FileDescription;
    if (name == "")
        name = GetTitle(handle);

 return name;
}

public string GetTitle(IntPtr handle)
{
string windowText = "";
    const int nChars = 256;
    StringBuilder Buff = new StringBuilder(nChars);
    if (GetWindowText(handle, Buff, nChars) > 0)
    {
        windowText = Buff.ToString();
    }
    return windowText;
}
静态外部单元GetWindowThreadProcessId(IntPtr hWnd,out单元lpdwProcessId); 公共字符串GetActiveWindowTitle() { var handle=GetForegroundWindow(); 字符串fileName=“”; 字符串名称=”; uint-pid=0; GetWindowThreadProcessId(句柄,输出pid); 进程p=Process.GetProcessById((int)pid); var processname=p.processname; 开关(进程名) { 案例“资源管理器”://metro进程 案例“WWAHost”: name=GetTitle(句柄); 返回名称; 违约: 打破 } string wmiQuery=string.Format(“从Win32_进程中选择ProcessId,ExecutablePath,其中ProcessId类似于“{0}”,pid.ToString()); var pro=新的ManagementObjectSearcher(wmiQuery).Get().Cast().FirstOrDefault(); fileName=(string)pro[“ExecutablePath”]; //获取文件版本 FileVersionInfo myFileVersionInfo=FileVersionInfo.GetVersionInfo(文件名); //获取文件描述 name=myFileVersionInfo.FileDescription; 如果(名称==“”) name=GetTitle(句柄); 返回名称; } 公共字符串GetTitle(IntPtr句柄) { 字符串windowText=“”; 常数int nChars=256; StringBuilder Buff=新的StringBuilder(nChars); 如果(GetWindowText(句柄、Buff、nChars)>0) { windowText=Buff.ToString(); } 返回窗口文本; }
[DllImport(“user32.dll”)]
静态外部单元GetWindowThreadProcessId(IntPtr hWnd,out单元lpdwProcessId);
公共字符串GetActiveWindowTitle()
{
var handle=GetForegroundWindow();
字符串fileName=“”;
字符串名称=”;
uint-pid=0;
GetWindowThreadProcessId(句柄,输出pid);
进程p=Process.GetProcessById((int)pid);
var processname=p.processname;
开关(进程名)
{
案例“资源管理器”://metro进程
案例“WWAHost”:
name=GetTitle(句柄);
返回名称;
违约:
打破
}
string wmiQuery=string.Format(“从Win32_进程中选择ProcessId,ExecutablePath,其中ProcessId类似于“{0}”,pid.ToString());
var pro=新的ManagementObjectSearcher(wmiQuery).Get().Cast().FirstOrDefault();
fileName=(string)pro[“ExecutablePath”];
//获取文件版本
FileVersionInfo myFileVersionInfo=FileVersionInfo.GetVersionInfo(文件名);
//获取文件描述
name=myFileVersionInfo.FileDescription;
如果(名称==“”)
name=GetTitle(句柄);
返回名称;
}
公共字符串GetTitle(IntPtr句柄)
{
字符串windowText=“”;
常数int nChars=256;
StringBuilder Buff=新的StringBuilder(nChars);
如果(GetWindowText(句柄、Buff、nChars)>0)
{
windowText=Buff.ToString();
}
返回窗口文本;
}

谢谢!你能解释一下
getforegroundindow()吗
方法和类
ManagementObjectSearcher
?谢谢!你能解释一下
getforegroundindow()吗方法和类
ManagementObjectSearcher