检索没有Windows阴影的窗口大小 [英] Retrieve Window Size without Windows Shadows

查看:320
本文介绍了检索没有Windows阴影的窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于Window句柄捕获C#中的桌面窗口.我正在使用.NET,并使用PInvoke到GetWindowRect()来捕获窗口矩形.我的窗口选择和矩形捕获工作正常.

I'm trying to capture desktop windows in C# based on Window handles. I'm using .NET and using PInvoke to GetWindowRect() to capture the window rectangle. I've got the Window selection and rectangle capture working fine.

但是,捕获的窗口矩形不仅包括实际的窗口大小,还包括窗口的装饰物,例如其周围的阴影.当我尝试将窗口裁剪为位图时,位图包含区域和阴影.在Windows 10上,我得到了透明阴影区域,其中包括在活动窗口下方可能可见的所有内容:

However, the window rectangles captured include not just the actual window size but also the Window's adornments like the shadow around it. When I try to clip the window to a bitmap the bitmap contains the area and shadow. On Windows 10 I get the transparent shadow area including any content that might be visible below the active window:

我使用的代码非常简单,即通过PInvoke调用使用Win32 GetWindowRect()捕获窗口:

The code I use is simple enough capturing the Window using Win32 GetWindowRect() via PInvoke call:

var rect = new Rect();
GetWindowRect(handle, ref rect);
var bounds = new Rectangle(rect.Left, rect.Top, rect.Right - rect.Left, rect.Bottom - rect.Top);
var result = new Bitmap(bounds.Width, bounds.Height);

using (var graphics = Graphics.FromImage(result))
{
    graphics.CopyFromScreen(new Point(bounds.Left, bounds.Top), Point.Empty, bounds.Size);
}

return result;

然后我捕获图像并将其分配到图片框中.

I then capture the image and assign it into a picture box.

此外,各个窗口之间似乎存在一些差异-一些窗口具有阴影,而另一些则没有.大多数都可以,但是像Visual Studio和Chrome这样的工具却不能,因此剥离多余像素甚至不是一件简单的事情.

In addition it looks like there's some variation between windows - some windows have shadows others do not. Most do, but some like Visual Studio and Chrome do not, so it's not even a simple matter of stripping out the extraneous pixels.

我已经尝试过使用GetClientRect(),但这只是我所需要的客户区域而已.我想要得到的是带有边框但没有阴影的实际Window矩形.

I've tried using GetClientRect() but that gets me just the client area which is not what I've after. What I'd like to get is the actual Window rectangle with borders but without the shadows.

反正有这样做吗?

推荐答案

我正在运行Windows 10,并且在编写用于将窗口捕捉到屏幕顶部或底部的应用程序时遇到了相同的问题.我发现DwmGetWindowAttribute()可以工作.它返回的RECT值与GetWindowRect()略有不同.

I'm running Windows 10 and came across the same issue when writing an app to snap windows to the top or bottom of the screen. I've found DwmGetWindowAttribute() to work. It returns a RECT with slightly different values than GetWindowRect()...

来自示例窗口的结果:

GetWindowRect():{X = 88,Y = 26,Width = 871,Height = 363}

GetWindowRect(): {X=88,Y=26,Width=871,Height=363}

DwmGetWindowAttribute():{X = 95,Y = 26,Width = 857,Height = 356}

DwmGetWindowAttribute(): {X=95,Y=26,Width=857,Height=356}

我的测试表明,GetWindowRect()包含装饰物,而DwmGetWindowAttribute()不包含装饰物.

My testing showed that GetWindowRect() included the adornments, while DwmGetWindowAttribute() did not.

如果在带有装饰的窗口上从这两种方法获得相同的结果,则可能是该特定窗口正在绘制其自己的装饰,或者该窗口设置了一些其他属性需要考虑在内.

If you're getting identical results from both methods on a window with adornments, it might be that that particular window is drawing its own adornments, or that there is some other attribute or property set to the window that needs to be taken into account.

这篇关于检索没有Windows阴影的窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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