似乎无法在多个显示器上实现双缓冲绘图

3

我试图将使用C++和WinAPI编写的Windows屏幕保护程序重新制作,以适用于多个监视器。我找到了这篇文章,它提供了基础知识。但是当我在自己的代码中实现时,我得到了奇怪的结果。看一下这段代码:

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预处理器指令)时,在辅助监视器上只会得到黑屏,而本应该是红色的屏幕。
我在这里附上两张截图。
第一张是直接绘制的工作截图:enter image description here 然后是双缓冲失败的截图:enter image description here 有任何想法我在这里做错了什么?

你有检查 FillRect() 和/或 BitBlt() 是否返回错误吗? - Remy Lebeau
刚刚检查了一下,它们在屏幕保护程序的整个生命周期中都没有返回0。 - c00000fd
大多数GDI函数在返回错误方面非常不可靠。甚至检查都几乎没有意义。 - Carey Gregory
想法是,在MyPaintEnumProc中,您的hdc1始终为NULL。因此,您会进一步失败。 - Maximus
还检查了一下,hdc1不是NULL。但我完全同意,那些GDI函数是完全不可靠的! - c00000fd
1个回答

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接