首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HDC和Memory DC是否不同?

HDC和Memory DC是否不同?
EN

Stack Overflow用户
提问于 2020-08-16 09:41:48
回答 1查看 159关注 0票数 0

我正在学习windows winapi中的双缓冲。当我直接使用DrawText函数在HDC上绘制文本时,它就像在代码下一样工作得很好。

代码语言:javascript
复制
case WM_PAINT:
hDC = BeginPaint(hwnd,&ps);
DrawText(hDC,"test",4,&rt,DT_CENTER | DT_WORDBREAK);
DeleteDC(hMemDC);
ReleaseDC(hwnd,hDC);
EndPaint(hwnd,&ps);
break;

但是,我想使用双缓冲,所以我做了内存dc和bitblt函数。在代码下面,它不工作。我可以显示白色的空白屏幕。

代码语言:javascript
复制
case WM_PAINT:
hDC = BeginPaint(hwnd,&ps);
hMemDC = CreateCompatibleDC(hDC);
//GetClientRect(hwnd, &crt);
//hBitmap = CreateCompatibleBitmap(hDC, crt.right, crt.bottom);
//OldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
DrawText(hMemDC,"test",4,&rt,DT_CENTER | DT_WORDBREAK);
BitBlt(hDC,0,0,800,800,hMemDC,0,0,SRCCOPY);
DeleteDC(hMemDC);
ReleaseDC(hwnd,hDC);
EndPaint(hwnd,&ps);
break;

memory dc是否与原始dc不同?如果我使用CreateCompatibleBitmap函数,它工作得很好。我错过了什么概念?网站是否有组织良好的网站?

EN

回答 1

Stack Overflow用户

发布于 2020-08-16 11:44:58

典型的非双缓冲绘图例程如下所示:

代码语言:javascript
复制
case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        RECT rcWnd;
        GetClientRect(hWnd, &rcWnd);
        
        {
            DrawText(hdc, _T("Hello, world!"), -1,
                &rcWnd, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
        }
        
        EndPaint(hWnd, &ps);
    }
    break;

典型的双缓冲绘图例程如下所示:

代码语言:javascript
复制
case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);
        RECT rcWnd;
        GetClientRect(hWnd, &rcWnd);

        {
            const int width = rcWnd.right - rcWnd.left
            const int height = rcWnd.bottom - rcWnd.top;

            // create a new DC based on the target HDC
            HDC hDCMem = CreateCompatibleDC(hdc);
            // create a bitmap that is compatible with the target DC
            HBITMAP hMemBmp = CreateCompatibleBitmap(hdc, width, height);
            // select the new bitmap in to the DC, saving the old bitmap
            HBITMAP hOldBmp = (HBITMAP)SelectObject(hDCMem, hMemBmp);

            // do your drawing
            HBRUSH hBr = CreateSolidBrush(RGB(255, 255, 255));
            FillRect(hDCMem, &rcWnd, hBr);
            DeleteObject(hBr);
            DrawText(hDCMem, _T("Hello, world!"), -1,
                &rcWnd, DT_CENTER | DT_VCENTER | DT_SINGLELINE);

            // copy all the bits from our new DC over to the target DC
            BitBlt(hdc, rcWnd.left, rcWnd.top, width, height, hDCMem, 0, 0, SRCCOPY);
            // select the original bitmap the DC came with
            SelectObject(hDCMem, hOldBmp);
            // delete our bitmap
            DeleteObject(hMemBmp);
            // delete the DC
            DeleteDC(hDCMem);
        }
        
        EndPaint(hWnd, &ps);
    }
    break;
case WM_ERASEBKGND:
    // since we are drawing the entire area because we are double-buffering, there is
    // no need to erase the background. This will speed up your drawing.
    return TRUE;

HDC对象都是一样的--当您在内存中创建另一个对象时,不会发生任何特殊的事情。我是说,它们都在记忆中,真的。您只是在创建另一个画布来进行绘图,然后在一次拍摄中复制该画布。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63432370

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档