【问题标题】:GDI - Can I save the bitmap in memory between WM_PAINT calls?GDI - 我可以在 WM_PAINT 调用之间将位图保存在内存中吗?
【发布时间】:2016-05-17 09:05:44
【问题描述】:

我想优化我的窗口绘画。 想要在窗口中拥有静态内容和动态内容。 静态内容只计算一次,并根据创建的位图进行绘制。

我正在使用下一个算法:

  /* !global object! HBITMAP hStaticBitmap */ 
  PAINTSTRUCT ps = {};

  HDC  hdc = BeginPaint (hWnd, &ps);
  //-------------------------------------------
  HDC   hStaticDC = CreateCompatibleDC (hdc);

  if ( hStaticBitmapChanged )
  {
    /* Create once */
    if (hStaticBitmap)
      DeleteObject (hStaticBitmap);

    hStaticBitmap = CreateCompatibleBitmap (hdc, myRect.right  - myRect.left,
                                                 myRect.bottom - myRect.top);
    HBITMAP hBmp_old = (HBITMAP) SelectObject (hStaticDC, wd.hStaticBitmap);
    //-------------------------------------
    OnPaintStaticFigures (hStaticDC);
    hStaticBitmapChanged = false;
    //-------------------------------------
    SetStretchBltMode (hdc, COLORONCOLOR);
    BitBlt (hdc, 0, 0,
            myRect.right - myRect.left,
            myRect.bottom - myRect.top,
            hStaticDC, 0, 0,
            SRCCOPY);
    //-------------------------------------
    SelectObject (hStaticDC, hBmp_old);
  }
  else
  {
    /* Paint every time */
    HBITMAP  hBmp_old = (HBITMAP) SelectObject (hStaticDC, wd.hStaticBitmap);
    //-------------------------------------
    SetStretchBltMode (hdc, COLORONCOLOR);
    BitBlt (hdc, 0, 0,
            myRect.right - myRect.left,
            myRect.bottom - myRect.top,
            hStaticDC, 0, 0,
            SRCCOPY);
    //-------------------------------------
    SelectObject (hStaticDC, hBmp_old);
  }

  HDC   hCmpDC = CreateCompatibleDC (hdc);
  HBITMAP  hBmp = CreateCompatibleBitmap (hdc, myRect.right  - myRect.left,
                                               myRect.bottom - myRect.top);
  SelectObject (hCmpDC, hBmp);
  //-------------------------------------
  OnPainDynamicFigures (hCmpDC, wd);
  //-------------------------------------
  SetStretchBltMode (hdc, COLORONCOLOR);
  BitBlt (hdc, 0, 0,
          myRect.right  - myRect.left,
          myRect.bottom - myRect.top,
          hCmpDC, 0, 0,
          SRCCOPY);

  DeleteDC (hStaticDC);
  DeleteDC (hCmpDC);
  DeleteObject (hBmp);
  //---------------------------------------------
  EndPaint (hWnd, &ps);

问题是:hStaticBitmap在Delete hStaticDC后被清除。 如何处理?

谢谢


编辑:

现在我不确定它是否会发生,因为 hStaticDC。我已经使 hStaticDC 全球化了。但我可以看到黑色背景,第二个位图擦除第一个。我尝试了另一种重叠模式,并使用全局 hdc 显示两个位图。

我想改变我的问题:如何使位图没有背景?

【问题讨论】:

    标签: c++ windows winforms bitmap gdi


    【解决方案1】:

    工作变体:使用下层作为上层的背景!

     /* !global object! HBITMAP hStaticBitmap */ 
     /* !global object! HBITMAP hStaticDC    */ // !!!global 
    
      PAINTSTRUCT ps = {};
      HDC  hdc = BeginPaint (hWnd, &ps);
      //-------------------------------------------
      if ( hStaticBitmapChanged )
      {
        /* Create once */
        if ( !hStaticDC )                       // !!!global 
          hStaticDC = CreateCompatibleDC (hdc);
        if (hStaticBitmap)
          DeleteObject (hStaticBitmap);
    
        hStaticBitmap = CreateCompatibleBitmap (hdc, myRect.right  - myRect.left,
                                                     myRect.bottom - myRect.top);
        SelectObject (hStaticDC, hStaticBitmap);
        //-------------------------------------
        OnPaintStaticFigures (hStaticDC);
        hStaticBitmapChanged = false;
        //-------------------------------------
      }
    
      HDC   hCmpDC = CreateCompatibleDC (hdc);
      HBITMAP  hBmp = CreateCompatibleBitmap (hdc, myRect.right  - myRect.left,
                                                   myRect.bottom - myRect.top);
      SelectObject (hCmpDC, hBmp);
      //-------------------------------------
      /* Paint every time */
      SetStretchBltMode (hCmpDC, COLORONCOLOR);   // !!! hCmpDC
      BitBlt ( hCmpDC , 0, 0,                     // !!! hCmpDC
               myRect.right - myRect.left,
               myRect.bottom - myRect.top,
               hStaticDC, 0, 0,
               SRCCOPY);
      //-------------------------------------
      OnPainDynamicFigures (hCmpDC, wd);
      //-------------------------------------
      SetStretchBltMode (hdc, COLORONCOLOR);
      BitBlt (hdc, 0, 0,
              myRect.right  - myRect.left,
              myRect.bottom - myRect.top,
              hCmpDC, 0, 0,
              SRCCOPY);
    
      // DeleteDC (hStaticDC); /* later */
      // DeleteObject (hStaticBitmap); /* later */
      DeleteDC (hCmpDC);
      DeleteObject (hBmp);
      //---------------------------------------------
      EndPaint (hWnd, &ps);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-16
      • 2017-01-25
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-07
      • 1970-01-01
      相关资源
      最近更新 更多