当我按某个键时,如何获得活动窗口的路径? [英] How do I get path of active window when I press some key?

查看:74
本文介绍了当我按某个键时,如何获得活动窗口的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获得最佳窗口的完整路径。

我的应用程序在后台,当用户按特定键时,我想知道当前窗口的路径在顶部。



我尝试使用以下代码,但此代码显示了我自己的应用程序路径。



我尝试了什么:



I am trying to get full path of window which is on top.
My application is in background and when user press specific key then i want to know the path of current window which is On top.

I try following code but this code is showing my own application path.

What I have tried:

protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x0312)
            {
                Process currentProcess = Process.GetCurrentProcess();
                MessageBox.Show(currentProcess.MainModule.FileName);
            }
        }

推荐答案

嗯......是的,它会。

这里有两个问题。首先,为了让你的应用程序被一个键激活,找出哪个窗口位于顶部,它必须具有输入焦点。这意味着...它位于顶部......:笑:



其次,如果你看一下Process.GetCurrentProcess Method(System.Diagnostics) [ ^ ]它返回与当前执行代码相关的进程:即试图找出哪个进程最常见的代码 - 这将始终是您的应用程序!



差不多,你不能做你想做的事情 - 虽然链接会告诉你如何获得你系统上的所有进程 - 除非你可以安装一个Global Hook,它可以将密钥捕获到远高于你的应用程序的级别。虽然可以这样做(在C#中处理全局鼠标和键盘挂钩 [ ^ ]显示如何)这对初学者来说不是尝试 - 错误会让你的系统严重不稳定!
Well...yes, it will.
There are two problems here. The first is that in order for you application to be activated by a key to find out which window is on top, it has to have the input focus. which means...it's on top...:laugh:

Secondly, if you look at the documentation for Process.GetCurrentProcess Method (System.Diagnostics)[^] it returns a process associated withteh currently executing code: i.e. the code that is trying to find out which process is tip most - this will always be your application!

Pretty much, you can't do what you want - though the link will show you how to get all processes on your system - unless you can install a Global Hook which can trap out the key at a level much, much higher than your application. While this can be done (Processing Global Mouse and Keyboard Hooks in C#[^] shows how) it's not something for a beginner to attempt - mistakes can leave your system seriously unstable!


要获得前景窗口处理:



.net - 如何使用c#获取当前活动窗口的标题? - 堆栈溢出 [ ^ ]
To get the foreground window handle:

.net - How do I get the title of the current active window using c#? - Stack Overflow[^]


这篇关于当我按某个键时,如何获得活动窗口的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆