如何通过C#在计算机上获取活动窗口的地址 [英] How do I get address my active window on computer in C#

查看:213
本文介绍了如何通过C#在计算机上获取活动窗口的地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi My Frinds

我很抱歉我没有良好的书面语言。

我想在c#
$获取地址我的活动窗口b $ b例如:

Window My Computer处于活动状态: - > C:\Program Files(x86)\ Adob​​e \ Photoshop。CS

现在我想要一个程序在C#中给出这个地址窗口或参考这个窗口。

请帮助我。

谢谢。



我尝试过:



请用c语言帮助我#

如果你有一个项目我将不胜感激。

谢谢。

Hi My Frinds
I'm Sorry That I Do Not Have Good Written Language.
I Want TO Get Address MY Active Window in c#
for example:
The Window My Computer is active: -> C:\Program Files (x86)\Adobe\Photoshop CS
Now I Want a Program To Give This Address Window in C# OR Refer To This Window.
Please Help Me.
Thank You.

What I have tried:

Please help me with language c# only
If you have a project II will be grateful submit.
thank you.

推荐答案

您可以所有进程并尝试使用 System.Diagnostics.Process.MainWindowHandle获取每个进程的主窗口句柄

处理.GetProcesses方法(System.Diagnostics)

Process.MainWindowHandle Property(System.Diagnostics)



注意: MainWindowHandle 可能会抛出异常N;所以你必须抓住它并忽略这种情况。



对于每个句柄,你可以 GetForegroundWindow 的方式你在解决方案3中写道(但这不是解决方案)。



-SA
You can get all processes and try to get a main window handle of each one using System.Diagnostics.Process.MainWindowHandle:
Process.GetProcesses Method (System.Diagnostics),
Process.MainWindowHandle Property (System.Diagnostics).

Pay attention: MainWindowHandle may throw and exception; so you have to catch it and just ignore this case.

For each handle, you can GetForegroundWindow the way you wrote in Solution 3 (but this is not a solution).

—SA


unsafe
{
  var notepadProcs = Process.GetProcessesByName("notepad");
  if (notepadProcs.Length > 0)
  {
    // Main window handle in IntPtr
    var mainWindowHandle = notepadProcs[0].MainWindowHandle;

    // Main window handle in unmanaged pointer
    var mainWindowPtr = (byte*) mainWindowHandle.ToPointer();
  }
}


[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();

...
IntPtr activeWindow = GetForegroundWindow();
byte *mainWindowPtr = (byte*) activeWindow.ToPointer();
...


这篇关于如何通过C#在计算机上获取活动窗口的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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