windows 使用 .NET (C#) 获取所有打开窗口的位置和大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3406888/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:58:19  来源:igfitidea点击:

Get location and size of all open windows using .NET (C#)

.netwindows

提问by Dan Harris

Is there a way to detect and store the location and size of all open windows, as well as their state (Minimized, Maximized etc)

有没有办法检测和存储所有打开的窗口的位置和大小,以及它们的状态(最小化、最大化等)

I've never developed anything that gets information from the actual operating system in this way. Does it require a call to a Windows API and involve unmanaged code?

我从未开发过任何以这种方式从实际操作系统获取信息的东西。它是否需要调用 Windows API 并涉及非托管代码?

If this is not clear please comment and I will try to elaborate.

如果这不清楚,请发表评论,我会尽力详细说明。

采纳答案by Hans Olsson

Call EnumWindowsto loop through all the windows, then call GetWindowPlacementto get out the information. It will require PInvoke to Windows API, but it's not that difficult, just can find all the information at the PInvokesite.

调用EnumWindows循环遍历所有窗口,然后调用GetWindowPlacement获取信息。它需要 PInvoke 到 Windows API,但并不难,只要可以在PInvoke站点上找到所有信息。

Btw, here's a codeprojectarticle for finding a specific window and getting/setting the show state of it, might be a good starting point (the code is in VB.Net, but you could probably just use one of the online VB.Net to C# converters if you don't know VB.Net)

顺便说一句,这里有一篇用于查找特定窗口并获取/设置它的显示状态的代码项目文章,可能是一个很好的起点(代码在 VB.Net 中,但您可能只使用在线 VB.Net 中的一个来C# 转换器(如果您不了解 VB.Net)

回答by Tergiver

Yes, you will begin with EnumWindows: http://msdn.microsoft.com/en-us/library/ms633497.aspx

是的,您将从 EnumWindows 开始:http: //msdn.microsoft.com/en-us/library/ms633497.aspx

See the Window Functions list for methods to gain access to the information you want: http://msdn.microsoft.com/en-us/library/ff468919.aspx

有关访问所需信息的方法,请参阅窗口函数列表:http: //msdn.microsoft.com/en-us/library/ff468919.aspx

回答by SaravananArumugam

System.Diagnostics.Process class gets you much of the information you need. You can try that.

System.Diagnostics.Process 类为您提供所需的大部分信息。你可以试试。

        Process[] currentProcesses = Process.GetProcesses();           
        Console.WriteLine("MainWindowTitle: {0}",currentProcesses[0].MainWindowTitle);
        Console.WriteLine(currentProcesses[0].StartInfo.WindowStyle);

I am not sure if it provides all that you need. But Process class is capable of providing so much information about currently running processes. It's good to give a look at Process class before moving to APIs.

我不确定它是否提供了您需要的一切。但是 Process 类能够提供有关当前正在运行的进程的大量信息。在转向 API 之前,最好先看看 Process 类。