首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >似乎无法使用多个监视器进行双缓冲绘制

似乎无法使用多个监视器进行双缓冲绘制
EN

Stack Overflow用户
提问于 2012-09-20 07:58:31
回答 1查看 458关注 0票数 3

我正在尝试重新制作用C++和WinAPI编写的视窗屏幕保护程序,以便在多台显示器上工作。我找到了提供基础知识的this article。但是当我在我自己的代码中实现它时,我得到了一个奇怪的结果。请看下面的代码:

代码语言:javascript
复制
case WM_PAINT:
{
    PAINTSTRUCT ps = {0};
    HDC hdcE = BeginPaint(hWnd, &ps );

    EnumDisplayMonitors(hdcE, NULL, MyPaintEnumProc, 0);

    EndPaint(hWnd, &ps);
}
break;

BOOL CALLBACK MyPaintEnumProc(
      HMONITOR hMonitor,  // handle to display monitor
      HDC hdc1,     // handle to monitor DC
      LPRECT lprcMonitor, // monitor intersection rectangle
      LPARAM data       // data
      )
{
    MONITORINFO mi = {0};
    mi.cbSize = sizeof(mi);
    if(GetMonitorInfo(hMonitor, &mi))
    {
        //Is it a primary monitor?
        BOOL bPrimary = mi.dwFlags & MONITORINFOF_PRIMARY;

        DoDrawing(bPrimary, hdc1, &mi.rcMonitor);
    }

    return 1;
}

void DoDrawing(BOOL bPrimaryMonitor, HDC hDC, RECT* pRcMonitor)
{
//#define DIRECT_PAINT          //Comment out for double-buffering

    int nMonitorW = abs(pRcMonitor->right - pRcMonitor->left);
    int nMonitorH = abs(pRcMonitor->bottom - pRcMonitor->top);

    HDC hMemDC = ::CreateCompatibleDC(hDC);
    if(hMemDC)
    {
        HBITMAP hMemBmp = ::CreateCompatibleBitmap(hDC, nMonitorW, nMonitorH);
        if(hMemBmp)
        {
            HBITMAP hOldBmp = (HBITMAP)SelectObject(hMemDC, hMemBmp);

            COLORREF clr, clrBorder;
            if(bPrimaryMonitor)
            {
                clr = RGB(0, 128, 0);           //Green
                clrBorder = RGB(255, 0, 0);
            }
            else
            {
                clr = RGB(128, 0, 0);           //Red
                clrBorder = RGB(0, 255, 0);

            }

            RECT rcRect;
#ifndef DIRECT_PAINT
            //With double-buffering
            rcRect.left = 0;
            rcRect.top = 0;
            rcRect.right = nMonitorW;
            rcRect.bottom = nMonitorH;
#else
            rcRect = *pRcMonitor;
#endif

            HBRUSH hBrush = ::CreateSolidBrush(clr);

#ifndef DIRECT_PAINT
            //With double-buffering
            ::FillRect(hMemDC, &rcRect, hBrush);
#else
            ::FillRect(hDC, &rcRect, hBrush);
#endif


#ifndef DIRECT_PAINT
            //With double-buffering
            ::BitBlt(hDC, pRcMonitor->left, pRcMonitor->top, nMonitorW, nMonitorH, hMemDC, 0, 0, SRCCOPY);
#endif

//Debugging output
CString _s;
_s.Format(_T("%s\n")
          _T("%s\n")
          _T("hDC=0x%X\n")
          _T("hMemDC=0x%X\n")
        _T("RcMonitor: L=%d, T=%d, R=%d, B=%d")
          , 
          bPrimaryMonitor ? _T("Primary") : _T("Secondary"),
#ifndef DIRECT_PAINT
          _T("Double-buffering"),
#else
          _T("Direct paint"),
#endif
          hDC,
          hMemDC,
          pRcMonitor->left,
          pRcMonitor->top,
          pRcMonitor->right,
          pRcMonitor->bottom);
::DrawText(hDC, _s, _s.GetLength(), pRcMonitor, DT_NOCLIP | DT_NOPREFIX);


            SelectObject(hMemDC, hOldBmp);
            ::DeleteObject(hMemBmp);
        }

        ::DeleteDC(hMemDC);
    }

}

绘画总是在主监视器上工作。但是当我绘制到辅助显示器时,我只能直接绘制到它的DC。当我使用双缓冲技术(使用DIRECT_PAINT预处理器指令注释掉)时,我只在辅助显示器上看到黑屏,而实际上它应该是红色的。

我在这里附上两个截图。

第一个使用直接绘画的作品:

然后使用双缓冲的那个失败了:

你知道我在哪里做错了吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-09-21 15:36:57

将WM_PAINT的代码替换为

代码语言:javascript
复制
case WM_PAINT:
   hdc = BeginPaint(hWnd, &ps);
   EnumDisplayMonitors(hdc, NULL, MyPaintEnumProc, 0);
   EndPaint(hWnd, &ps);

使用

代码语言:javascript
复制
case WM_PAINT:
   hdc = GetDC(NULL);
   EnumDisplayMonitors(hdc, NULL, MyPaintEnumProc, 0);
   ReleaseDC(NULL, hdc);

它会起作用的。

请参阅此http://msdn.microsoft.com/en-us/library/windows/desktop/dd162610(v=vs.85).aspx

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

https://stackoverflow.com/questions/12504472

复制
相关文章

相似问题

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