帮助GetWindowRect() [英] Help with GetWindowRect()

查看:52
本文介绍了帮助GetWindowRect()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个需要检查外部应用程序窗口的边界矩形的程序。我找到了多种API方法,GetWindowRect(),GetWindowPlacement()和GetWindowInfo()等等。在大多数情况下,他们的工作非常好。我将MainWindowHandle传递给方法,它们返回有关窗口的一些信息。我将解释每个方法返回的内容,所以如果你想跳过它,滚动直到你看到我的问题以粗体显示。


GetWindowRect()返回一个 RECT 对象,不是与矩形混淆。 RECT包含顶部,左侧,右侧和底部坐标,而不是顶部,左侧,宽度和高度。 (我知道Rectangle 确实包含右边和底部,但它返回宽度和高度)


GetWindowPlacement()返回 WINDOWPLACEMENT 对象,其中包含更多信息,包括用于确定窗口是否已最大化,最小化或正常以及将其还原到哪种状态的标志。它还包含一个RECT,它告诉窗口的正常位置是什么,也就是窗口返回到正常(非最大化或最小化)状态时的大小和位置。


最后,GetWindowInfo()返回 WINDOWINFO 对象。这包含了最多的信息。它仍然有一个包含窗口大小和位置的RECT对象,但它还有另一个RECT,其中包含窗口客户区的大小和位置。还有两个值可以保持窗口边框的大小。

我的问题


好​​的,对不起,我想确保你熟悉我正在使用的东西。


这是我的问题,当窗口最大化时它的边界不可见,但所有返回窗口矩形的方法似乎仍然考虑到这些。我有两个显示器,每个显示器的分辨率为1920x1200,总共3840x1200,设置为双屏幕。当我在左侧监视器中最大化窗口并获得它的RECT时,我得到了这个(假设没有任务栏):

{Left = -8,Top = -8,Right = 1928年,Bottom = 1208}


我应该得到:

{Left = 0,Top = 0,Right = 1920, Bottom = 1200}


现在它很简单,可以检测Left和Top的值是否为负值并反转并从右侧和底部减去它们。但是,如果我在正确的显示器中最大化窗口怎么办?在这种情况下,我得到这个RECT(再次假设没有任务栏):

{Left = 1912,Top = -8,Right = 3848,Bottom = 1208}


我应该得到:

{Left = 1920,Top = 0,Right = 3840,Bottom = 1200}


在这种情况下,-8表示-8。像素实际上是左侧显示器的一部分,因此它们不会被视为负片,尽管它们超出了屏幕的范围。


我已经完成了关于我如何发现并解决这个问题的失败,所以我希望这里一些聪明有帮助的人能够......好吧,帮助我。


谢谢事先,我为帖子的长度道歉,我只是认为彻底的解释会帮助我得到我需要的答案。

I''m working on a program that needs to check the bounding rectangle of external application windows. I''ve found multiple API methods that do just that, GetWindowRect(), GetWindowPlacement(), and GetWindowInfo() to name a few. For the most part they works very well. I pass a MainWindowHandle to the method and they returns some information about the window. I''m gonna explain what each of the methods return, so if you want to skip it, scroll til you see MY QUESTION in bold.

GetWindowRect() returns a RECT object, not to be confused with a Rectangle. RECT contains the top, left, right, and bottom coordinates instead of top, left, width, and height. (I know that Rectangle does contain right and bottom, but it returns width and height instead)

GetWindowPlacement() returns a WINDOWPLACEMENT object, which contains a bit more information including flags to determine if the window is maximized, minimized, or normal and what state to restore it to. It also contains a RECT that tells what the window''s normal position is, that is, the size and location of the window when it is returned to it''s normal (not maximized or minimized) state.

Finally, GetWindowInfo() returns a WINDOWINFO object. This contains the most information yet. It still has a RECT object containing the size and location of the window, but it also has another RECT with the size and location of the client area of the window. There''s also two values that hold the size of the window''s borders.

MY QUESTION

Ok, sorry for that, I wanted to make sure you were familiar with what I''m working with.

Here''s my issue, when a window is maximized it''s borders are not visible, but all of the methods that return a window rectangle still appear to take those into account. I have two monitors each with a resolution of 1920x1200, for a total of 3840x1200, set up in dual screen. When I maximize a window in the left monitor and get it''s RECT I get this (assuming no task bar):

{Left=-8,Top=-8,Right=1928,Bottom=1208}

I should get:

{Left=0,Top=0,Right=1920,Bottom=1200}

Now it''s simple enough to detect if the values of Left and Top are negative and invert and subtract them from the right and bottom. But what if I have a window maximized in the right monitor? In that case I get this RECT (again assuming no task bar):

{Left=1912,Top=-8,Right=3848,Bottom=1208}

I should get:

{Left=1920,Top=0,Right=3840,Bottom=1200}

In this case the "-8" pixels are actually part of the left monitor, so they don''t read as negative, though they are outside of that screen''s bounds.

I''m at a complete loss as to how I could detect and fix this problem, so I''m hoping that some of the smart helpful people here could...well, help me out.

Thank you in advance, and I apologize for the length of the post, I just thought thorough explanation would help me get the answer I need.

推荐答案

据我了解,这个只有在窗口最大化时才会发生......有没有办法判断是否是这种情况?


现在你正在做的问题是窗口的负左上角实际上是有效的,即使窗口没有最大化。这很容易做到,只需抓住任何窗口标题栏的右下角并将其向上拖动到右边。


现在,C#本身有标志判断一个窗口是否最大化或最小化,但我不知道它是否存在于Windows API中。一个快速的谷歌搜索出现了这个...

http://www.tek-tips.com/viewthread.c...1542276&page=2


如果没有工作,看看周围(或者这里的其他人都知道)。


如果没有办法判断窗口是否最大化,我想你可以做一些技巧。你正在做的事情可以扩展一下,比如......

As I understand it, this only happens when a window is maximized... is there a way to tell if this is the case?

The problem with what you''re doing now is a negative top-left corner of a window is actually valid, even if the window isn''t maximized. It''s easy enough to do, just grab the lower right part of the title bar for any window and drag it up and to the right.

Now, C# itself has flags to tell whether or not a window is maximized or minimized, but I don''t know if this exists in the Windows API or not. A quick google search turned up this...

http://www.tek-tips.com/viewthread.c...1542276&page=2

If that doesn''t work, take a look around (or maybe someone else here will know).

If there''s no way to tell if the window is maximized, I guess you can do a few tricks. What you''re already doing could be expanded a bit, something like...

展开 | 选择 | Wrap | 行号


I得到它了。 WINDOWPLACEMENT的showCmd保存有关窗口显示状态的信息。显示状态3最大化,所以我最终做的是将边框宽度和高度以及showCmd传递给我的ToRectangle()方法(在RECT结构内部)。如果showCmd是3,那么我将边框添加到顶部然后从右边和底部左边减去它。这给了我每次正确的窗口大小。无论如何我必须调用GetWindowPlacement,因为我需要将窗口恢复到可见的显示状态,如果它当前不在一个中,然后将它带到z顺序的前面。基本上我只是检查showCmd是否为2(最小化),然后检查标志。 2标志意味着窗口在最小化之前最大化,0标志意味着它在最小化之前是正常的。


在做这一切的过程中,我也学会了如何使用SetWindowPlacement移动和调整其他窗口的大小,这是我需要知道的另一件事。


感谢您的回复,我还是要求助于检查屏幕尺寸:/


我正在遇到的另一个问题,如果我可能(它是相关的),进程explorer.exe在技术上总是有一个可见的窗口:任务栏。如果我打开一个资源管理器窗口(例如,C驱动器)GetWindowRect没有为该窗口返回正确的矩形,它将返回任务栏。我无法弄清楚如何让探索者的窗户发出自己的坐标。也许我只能活着,但我真的希望能够看到浏览器窗口就像我可以使用其他窗口一样。
I got it. WINDOWPLACEMENT''s showCmd holds information about the window''s show state. A show state of 3 is maximized, so what I ended up doing is passing the border width and height and the showCmd to my ToRectangle() method (inside the RECT struct.) If the showCmd is 3 then I add the borders to the top and left and subtract it from the right and bottom. That gives me the correct window size every time. I have to call GetWindowPlacement anyway because I need to restore the window to a visible show state if it isn''t currently in one, then bring it to the front of the z-order. Basically I just check if the showCmd is 2 (minimized) and then I check the flags. A 2 flag means that the window was maximized before it was minimized, a 0 flag means that it was normal before it was minimized.

In doing all this, I also learned how I can move and resize other windows using SetWindowPlacement, which is another thing I needed to know.

Thanks for the reply though, I was about to resort to checking screen dimensions :/

One other problem I''m having, if I might (it''s related.) the process explorer.exe technically always has a visible window: the task bar. If I open up an explorer window though (the C drive, for example,) GetWindowRect doesn''t return the proper rectangle for that window, it returns the taskbar. I can''t figure out how to get explorer windows to give off their own coordinates. Maybe I''ll just have to live without that, but I''d really like to be able to "see" explorer windows like I can with other windows.


您的应用程序如何在同一程序中区分两个窗口?就像,如果我制作一个C#应用程序,在同一个可执行文件名下启动两个窗口,blah.exe是找到它们还是只找到其中​​一个?


这与探索者遇到的问题是一样的,还是有些不同?
How does your app differentiate between two windows in the same program? Like, if I made a C# app that launched two windows under the same executable name, "blah.exe" does it find both, or only one of them?

Is that the same problem you''re having with explorer, or somewhat different?


这篇关于帮助GetWindowRect()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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