如何找出DC的尺寸?

发布于 2024-09-07 08:56:25 字数 95 浏览 5 评论 0原文

假设我有一个设备上下文句柄(当然,在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

内心激荡 2024-09-14 08:56:25

设备上下文 (DC) 是定义一组图形对象及其关联属性以及影响输出的图形模式的结构。

我猜你指的是绘制的位图的宽度和高度?
如果是这样,那么我想你可以尝试以下操作:

BITMAP structBitmapHeader;
memset( &structBitmapHeader, 0, sizeof(BITMAP) );

HGDIOBJ hBitmap = GetCurrentObject(hDC, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);

//structBitmapHeader.bmWidth
//structBitmapHeader.bmHeight

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 :

BITMAP structBitmapHeader;
memset( &structBitmapHeader, 0, sizeof(BITMAP) );

HGDIOBJ hBitmap = GetCurrentObject(hDC, OBJ_BITMAP);
GetObject(hBitmap, sizeof(BITMAP), &structBitmapHeader);

//structBitmapHeader.bmWidth
//structBitmapHeader.bmHeight
-黛色若梦 2024-09-14 08:56:25

我对 GDI 也知之甚少,但似乎 GetDeviceCaps< /a> 可能会成功。

I also know little about GDI, but it seems GetDeviceCaps might do the trick.

┼── 2024-09-14 08:56:25

当我只有 HDC 时,我总是使用这段简单的代码来获取渲染区域的尺寸。
首先,你必须从HDC获取一个HWND - 很简单,然后你可以获取这个HWND的客户端矩形:

RECT    rcCli;          
GetClientRect(WindowFromDC(hdc), &rcCli);
// then you might have: 
nWidth = rcCli.right-rcCli.left; 
nHeight  = rcCli.bottom-rcCli.top;

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:

RECT    rcCli;          
GetClientRect(WindowFromDC(hdc), &rcCli);
// then you might have: 
nWidth = rcCli.right-rcCli.left; 
nHeight  = rcCli.bottom-rcCli.top;
你的笑 2024-09-14 08:56:25

作为免责声明,我对 GDI 或您在应用程序中必须使用的内容一无所知。我只是想尽可能提供帮助。

也就是说,我发现一个链接似乎表明使用 GetClientRect 来获取绘图区域的大小是合适的:

RECT clientRect;

GetClientRect(hWnd,&clientRect);

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:

RECT clientRect;

GetClientRect(hWnd,&clientRect);

http://www.toymaker.info/Games/html/gdi.html#winsize

櫻之舞 2024-09-14 08:56:25

但是如果获取计算器的window_dc尺寸,它会在“GetCurrentObject”或“GetObject”处失败,我认为窗口属性可能包含“ws_ex_noredirectionbitmap”,在这种情况下如何获取尺寸?

HDC win_dc = ::GetWindowDC(hwnd);
BITMAP bm = { 0 };
HGDIOBJ hBitmap = GetCurrentObject(win_dc, OBJ_BITMAP);
if (hBitmap)
{
    if (GetObject(hBitmap, sizeof(BITMAP), &bm))
    {
        windc_dimension.cx = bm.bmWidth;
        windc_dimension.cy = bm.bmHeight;
    }
}

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?

HDC win_dc = ::GetWindowDC(hwnd);
BITMAP bm = { 0 };
HGDIOBJ hBitmap = GetCurrentObject(win_dc, OBJ_BITMAP);
if (hBitmap)
{
    if (GetObject(hBitmap, sizeof(BITMAP), &bm))
    {
        windc_dimension.cx = bm.bmWidth;
        windc_dimension.cy = bm.bmHeight;
    }
}
苏佲洛 2024-09-14 08:56:25

您可以 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.

淡写薰衣草的香 2024-09-14 08:56:25

我正在两种可能的上下文上绘图:位图(2480x3508)和打印机页面(4961x7016)。

位图尺寸返回打印机页面的错误尺寸 (1x1)。 GetDeviceCaps 返回打印机页面的正确尺寸,但返回位图的错误尺寸(屏幕尺寸)。

此函数返回位图和打印机页面的正确尺寸。可能不适用于其他环境。

std::pair<int, int> getDrawingAreaDimensions(HDC context)
{
    // Drawing area is a bitmap
    {
        BITMAP header;
        ZeroMemory(&header, sizeof(BITMAP));

        HGDIOBJ bmp = GetCurrentObject(context, OBJ_BITMAP);
        GetObject(bmp, sizeof(BITMAP), &header);
        const int width = header.bmWidth;
        const int height = header.bmHeight;
        if (width > 1 && height > 1) {
            return {width, height};
        }
    }

    // Drawing area is a printer page
    const int width = GetDeviceCaps(context, HORZRES);
    const int height = GetDeviceCaps(context, VERTRES);
    return {width, height};
}

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.

std::pair<int, int> getDrawingAreaDimensions(HDC context)
{
    // Drawing area is a bitmap
    {
        BITMAP header;
        ZeroMemory(&header, sizeof(BITMAP));

        HGDIOBJ bmp = GetCurrentObject(context, OBJ_BITMAP);
        GetObject(bmp, sizeof(BITMAP), &header);
        const int width = header.bmWidth;
        const int height = header.bmHeight;
        if (width > 1 && height > 1) {
            return {width, height};
        }
    }

    // Drawing area is a printer page
    const int width = GetDeviceCaps(context, HORZRES);
    const int height = GetDeviceCaps(context, VERTRES);
    return {width, height};
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文