如何找出DC的尺寸?
假设我有一个设备上下文句柄(当然,在 Windows 环境中):
HDC hdc;
如何获取它的宽度和高度?
Let's say I have a handle to device context (naturally, in Windows environment):
HDC hdc;
How can I get the width and height of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
设备上下文 (DC) 是定义一组图形对象及其关联属性以及影响输出的图形模式的结构。
我猜你指的是绘制的位图的宽度和高度?
如果是这样,那么我想你可以尝试以下操作:
A device context (DC) is a structure that defines a set of graphic objects and their associated attributes, and the graphic modes that affect output.
By width and height I'm guessing you are referring to the bitmap painted ?
If so then i guess you can try the following :
我对 GDI 也知之甚少,但似乎 GetDeviceCaps< /a> 可能会成功。
I also know little about GDI, but it seems GetDeviceCaps might do the trick.
当我只有 HDC 时,我总是使用这段简单的代码来获取渲染区域的尺寸。
首先,你必须从HDC获取一个HWND - 很简单,然后你可以获取这个HWND的客户端矩形:
This simple piece of code I use always to get the dimensions of the rendering area, when I have only the HDC.
First, you must get a HWND from the HDC - is simple, then you can get the client rect of this HWND:
作为免责声明,我对 GDI 或您在应用程序中必须使用的内容一无所知。我只是想尽可能提供帮助。
也就是说,我发现一个链接似乎表明使用
GetClientRect
来获取绘图区域的大小是合适的:http://www.toymaker.info/Games/html/gdi.html#winsize
As a disclaimer, I know nothing about GDI or what you have to work with in your application. I'm just trying to be helpful if possible.
That said, I found a link which seems to suggest that it's appropriate to use
GetClientRect
to get the size of the drawing area:http://www.toymaker.info/Games/html/gdi.html#winsize
但是如果获取计算器的window_dc尺寸,它会在“GetCurrentObject”或“GetObject”处失败,我认为窗口属性可能包含“ws_ex_noredirectionbitmap”,在这种情况下如何获取尺寸?
but if get Calculator' window_dc dimension, it will failed at “GetCurrentObject” or "GetObject", i think maybe the window attribute include "ws_ex_noredirectionbitmap", how to get dismension in this case?
您可以
WindowFromDC(...)
获取 DC 的窗口(如果它与窗口关联)。然后,您可以使用@KevinK的答案来获取客户端的正确信息。You could
WindowFromDC(...)
to get the DC's window if it's associated with a window. You could then use @KevinK's answer to get the client rect from this.我正在两种可能的上下文上绘图:位图(2480x3508)和打印机页面(4961x7016)。
位图尺寸返回打印机页面的错误尺寸 (1x1)。
GetDeviceCaps
返回打印机页面的正确尺寸,但返回位图的错误尺寸(屏幕尺寸)。此函数返回位图和打印机页面的正确尺寸。可能不适用于其他环境。
I am drawing on two possible contexts: Bitmap (2480x3508) and printer page (4961x7016).
Bitmap dimensions return wrong dimensions (1x1) for the printer page.
GetDeviceCaps
returns correct dimensions for the printer page, but returns wrong dimensions (screen dimensions) for the bitmap.This function returns correct dimensions for both a bitmap and a printer page. Probably doesn't work for other contexts.