在子窗口内,IDB_MYBMP位图如果不使用DC内存可以正常显示,但使用DC内存显示BMP位图显示不出来。代码片段:static HDC         hdcMem, hdcMemImage ;
static HBITMAP     hBitmap             ;
static BITMAP      bitmap              ;
static int         cxClient            ;//窗口客户区的宽度 
static int         cyClient            ;//窗口客户区的高度case WM_SIZE :
     cxClient = LOWORD (lParam) ;
     cyClient = HIWORD (lParam) ;
     return 0 ;case WM_PAINT:
     hdc    = GetDC (hwnd);
     hdcMem      = CreateCompatibleDC(hdc); 
     hdcMemImage = CreateCompatibleDC(NULL); 
           
     hBitmap = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_MYBMP));
     SelectObject(hdcMemImage,hBitmap);
 
     GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
         
     BitBlt(hdcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMemImage,0,0,SRCCOPY);
     BitBlt (hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY) ;     DeleteDC(hdcMem);
     DeleteDC(hdcMemImage);
          
     ReleaseDC (hwnd, hdc) ;
     return 0 ;   

解决方案 »

  1.   

    在WM_PAINT消息中不要用 hdc = GetDC (hwnd); 用 BeginPaint(hDlg,&pst); 
    代码修改如下
     
      hdc = BeginPaint(hDlg,&pst);
      if ( hdc == NULL ) break;    hdcMem = CreateCompatibleDC(hdc);  
       hdcMemImage = CreateCompatibleDC(hdc);  
        
      hBitmap = LoadBitmap(g_hInstance, MAKEINTRESOURCE(IDB_MYBMP));
      SelectObject(hdcMemImage,hBitmap);
      GetObject (hBitmap, sizeof (BITMAP), &bitmap) ;
    //建立画布 增加的是以下两句
      hBitmap1 = CreateCompatibleBitmap(hdcMemImage,bitmap.bmWidth,bitmap.bmHeight);
      SelectObject(hdcMem,hBitmap1);
     
         BitBlt(hdcMem,0,0,bitmap.bmWidth,bitmap.bmHeight,hdcMemImage,0,0,SRCCOPY);
      BitBlt (hdc, 0, 0, cxClient, cyClient, hdcMem, 0, 0, SRCCOPY) ;   DeleteObject(hBitmap);
      DeleteObject(hBitmap1);
      DeleteDC(hdcMem);
      DeleteDC(hdcMemImage);
      EndPaint(hDlg,&pst);