获取确切的窗口区域大小-CreateWindow窗口大小不是正确的窗口大小 [英] Get exact window region size - CreateWindow window size isn't correct size of window

查看:600
本文介绍了获取确切的窗口区域大小-CreateWindow窗口大小不是正确的窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试用C ++创建一个窗口并绘制矩形时,发现窗口大小与我设置的大小不匹配.

I've noticed something very annoying while trying to create a window in C++ and draw Rectangles that the window size doesn't match the size I set.

例如,如果我设置480x240窗口并尝试通过获取GetWindowRect(hwnd,& rect)从上到下,从左到右绘制矩形并计算宽度和高度:

For example, If I set the 480x240 window and try to Draw rectangles from top to bottom, left to right by getting GetWindowRect(hwnd, &rect) and calculate the width and height:

rectangle_width = (rect.right - rect.left) / amountRectangleX;
rectangle_height = (rect.bottom - rect.top) / amountRectangleY;

如果amountRectangleX = 2和Y = 2,它将绘制4个矩形,但是宽度和高度为"off",因此它不会填满整个屏幕或在其上方进行渲染.发生这种情况的唯一方法(我已经用许多其他语言完成了此工作,所以我知道它可以工作)是,如果我将窗口大小"设置为480x240,则希望该区域成为绘制"的区域.因为如果在窗口的大小中包含边框,则在另一台具有不同窗口样式"等的计算机上会有所不同.我不能只是手动为我的计算机更改".

if amountRectangleX = 2 and Y = 2 it draw 4 rectangles, but the width and height is "off" so it doesn't fill up the whole screen or it renders over it. The only way this can happen (I've done this in alot of other languages so I know it works) is that if I set Window Size = 480x240 I want that to be the area to "DRAW" on. Because if borders are included in the size of the window - that would be different on another computer with different Window Style and such. ANd I can't just "alter" this manually for my computer.

如果我将窗口大小设置为480x240并截图,我看到窗口空间= 452x232令人困惑.如果我将窗口大小设置为480x240,那将是可以的,但是当我获得GetWindowRect()时,我得到的是452x232而不是480x240,这是无效的,因为我的绘画空间较小.这可以解释为什么我的矩形渲染超出窗口空间,而我不想要那样.但是我仍然希望能够将我的尺寸设置为480x240或其他任何值,但是仍然有边框.

If I set window size = 480x240 and take a screenshot I see that the Window Space = 452x232 which is confusing. It would be OK if I set window size = 480x240 but when I GetWindowRect() I get 452x232 and not 480x240 which then is invalid because I have less space to draw on. This would explain why my Rectangles render beyond the window space and I do NOT want that. But I still want to be able to set my size = 480x240 or anything else but still have borders.

为什么它会这样工作,并且有解决此问题的方法吗? 我不是唯一想要设置窗口分辨率的人,无论您使用什么计算机,设置的大小都是可以绘制的绘图区域.

Why does it work this way and is there a solution to this problem? I can't be the only one that want to be able to set the resolution of a window and no matter what computer you use, that size you set IS the DRAW AREA that you can draw upon.

推荐答案

我使用此方法,现在可以使用:

I use this method and now it works:

if ( IsWindow( hwnd ) )
{

DWORD dwStyle = GetWindowLongPtr( hwnd, GWL_STYLE ) ;
DWORD dwExStyle = GetWindowLongPtr( hwnd, GWL_EXSTYLE ) ;
HMENU menu = GetMenu( hwnd ) ;

RECT rc = { 0, 0, width, height } ;

AdjustWindowRectEx( &rc, dwStyle, menu ? TRUE : FALSE, dwExStyle );

SetWindowPos( hwnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER | SWP_NOMOVE ) ;

}

这篇关于获取确切的窗口区域大小-CreateWindow窗口大小不是正确的窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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