gpt4 book ai didi

c++ - 似乎无法使用多个显示器进行双缓冲绘画

转载 作者:太空狗 更新时间:2023-10-29 21:47:46 54 4
gpt4 key购买 nike

我正在尝试重新制作使用 C++ 和 WinAPI 编写的 Windows 屏幕保护程序以在多台显示器上运行。我找到了 this article这给出了基础知识。但是当我在自己的代码中实现它时,我得到了一个奇怪的结果。看看这段代码:

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

知道我在这里做错了什么吗?

最佳答案

替换 WM_PAINT 的代码

case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EnumDisplayMonitors(hdc, NULL, MyPaintEnumProc, 0);
EndPaint(hWnd, &ps);

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 .

关于c++ - 似乎无法使用多个显示器进行双缓冲绘画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12504472/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com