正在看《Windows编程》,有一个叫《SCRAMBLE》的例子:用BitBlt交换显示器上两个矩形中的内容。关键代码如下:
if (LockWindowUpdate (hwnd = GetDesktopWindow ()))
{
hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdcScr) ;
cx = GetSystemMetrics (SM_CXSCREEN) / 10 ;
cy = GetSystemMetrics (SM_CYSCREEN) / 10 ;
hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ;
SelectObject (hdcMem, hBitmap) ;
....
BitBlt (hdcMem, 0, 0, cx, cy, hdcScr, x1, y1, SRCCOPY) ;
BitBlt (hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY) ;
BitBlt (hdcScr, x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY) ;
DeleteDC (hdcMem) ;
ReleaseDC (hwnd, hdcScr) ;
DeleteObject (hBitmap) ;
LockWindowUpdate (NULL) ;
}问题:为什么要在GetDCEx中使用DCX_CACHE标志,和LockWindowUpdate有关吗?(去掉这个标志,程序依旧正常工作。)查过MSDN,知道了DCX_CACHE的意思,但仍然不明白此例子这样做的目的。谢谢各位。
if (LockWindowUpdate (hwnd = GetDesktopWindow ()))
{
hdcScr = GetDCEx (hwnd, NULL, DCX_CACHE | DCX_LOCKWINDOWUPDATE) ;
hdcMem = CreateCompatibleDC (hdcScr) ;
cx = GetSystemMetrics (SM_CXSCREEN) / 10 ;
cy = GetSystemMetrics (SM_CYSCREEN) / 10 ;
hBitmap = CreateCompatibleBitmap (hdcScr, cx, cy) ;
SelectObject (hdcMem, hBitmap) ;
....
BitBlt (hdcMem, 0, 0, cx, cy, hdcScr, x1, y1, SRCCOPY) ;
BitBlt (hdcScr, x1, y1, cx, cy, hdcScr, x2, y2, SRCCOPY) ;
BitBlt (hdcScr, x2, y2, cx, cy, hdcMem, 0, 0, SRCCOPY) ;
DeleteDC (hdcMem) ;
ReleaseDC (hwnd, hdcScr) ;
DeleteObject (hBitmap) ;
LockWindowUpdate (NULL) ;
}问题:为什么要在GetDCEx中使用DCX_CACHE标志,和LockWindowUpdate有关吗?(去掉这个标志,程序依旧正常工作。)查过MSDN,知道了DCX_CACHE的意思,但仍然不明白此例子这样做的目的。谢谢各位。
In Windows NT, there is no preset number of cached DCs. If all the current DCs are in use when the application calls GetDC or BeginPaint, Windows NT will allocate another cache. Windows 95 still has a cache that contains five DCs. For portability, Win32 applications should attempt to limit their use of common DCs to five, and release a DC as soon as possible. To bypass the default DC for a window, applications can use the GetDCEx function and specify a flag of DCX_CACHE. This function overrides the CS_OWNDC and CS_CLASSDC styles and returns a common DC.