正在看《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的意思,但仍然不明白此例子这样做的目的。谢谢各位。

解决方案 »

  1.   

    不知道是不是下面的原因:If you do not use CS_OWNDC, CS_CLASSDC, or CS_PARENTDC for a class, the windows in that class use one of the common DCs Windows keeps cached for general use. Common DCs are retrieved and released with each use. Each context is initialized with the default selections each time a DC is retrieved, unless the DC for the window is still in the Windows DC cache. In this case, the clipping region and device origin need not be reinitialized, thus saving time. 
    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.
      

  2.   

    出处: http://msdn2.microsoft.com/en-us/library/ms997511.aspx