How to override OnEraseBkgnd of a transparent window?

When I make a window transparent(WS_EX_TRANSPARENT or simply return true in OnEraseBkgnd), it doesn't refresh its client area after moved (the transparent part always look the same).

Is it needed to implement something in OnEraseBkgnd() to do
so? plz give me an example. Thanks.
xtanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
wyy_cqConnect With a Mentor Commented:
void CTestDlg::OnPaint()
{
      
      if (IsIconic())
      {
            CPaintDC dc(this); // device context for painting

            SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

            // Center icon in client rectangle
            int cxIcon = GetSystemMetrics(SM_CXICON);
            int cyIcon = GetSystemMetrics(SM_CYICON);
            CRect rect;
            GetClientRect(&rect);
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

            // Draw the icon
            dc.DrawIcon(x, y, m_hIcon);
      }
      else
      {
            CPaintDC dc(this); // device context for painting
//Get and HDC pointer and pass it to SetupBk()
            HDC hdc;
            hdc = dc.GetSafeHdc();
            SetupBk(dc);
            CDialog::OnPaint();
      }

}

SetupBk(...) will do the painting work .


0
 
snoeglerCommented:
This seems a bit confusing to me ... Maybe i didn't understand
your question.
You don't erase the background
(>>WS_EX_TRANSPARENT or simply return true in OnEraseBkgnd)
so why do you wonder why it isn't erased?
Either you erase it in OnEraseBkgnd() or you erase it every
time you paint the contents (in OnPaint()) - or it isn't erased
at all :)
0
 
wyy_cqCommented:
i am sorry.
these line need to be added to my answer.
void CTestDlg::SetupBk(HDC hdc)
{
      int X,Y,H,W;
      CBitmap CB;
      CRect R;
      COLORREF C;
      GetWindowRect(&R);
      H = R.bottom - R.top;
      W = R.right - R.left;
      CB.LoadBitmap(IDB_BITMAP2);
      HBITMAP HB = (HBITMAP)CB;

      TransparentBlt( hdc , 0, 0, 535, 105, HB, 0, 0,0x00000000 , NULL );
      

//Only do this once because it takes some time.
//maybe should pre-make a seperate file to hold region data?
      if (FirstRun){
            int Set = 0;
            //begin pixel by pixel scan for transparent color
            for(X=0;X<=W;X++){
                  for(Y=0;Y<=H;Y++){
                        C = GetPixel(hdc,X,Y);
                        //Get the color
                        if(C == 0x00FFFFFF){
                              if(Set == 0){
                                    //First, init the working region
                                    //never delete Working, the OS does not store the value, only a pointer to it.
                                    Working.CreateRectRgn(X,Y,X+1,Y+1);
                                    Set = 1;
                              }
                              else{
                                    Temp.CreateRectRgn(X,Y,X+1,Y+1);
                                    //Add pixel to region
                                    Working.CombineRgn( &Working, &Temp,RGN_OR );
                                    Temp.DeleteObject();
                              }
                        }
                  }
            }
            FirstRun = 0;
            Temp.CreateRectRgn(0,0,1200,1200);
            //flip to get the non-transparent region
            Working.CombineRgn( &Working, &Temp,RGN_XOR    );
            Temp.DeleteObject();
            if (Set) SetWindowRgn((HRGN)Working,TRUE);
            //redraw just incase.. trust me.. leave this here
            RedrawWindow();
      }
      
}

static void MessageBlast(int iColumn,int iRow,LPCTSTR lpszMsg)
{
      HDC hdcScreen;

      // Get a raw screen DC
      hdcScreen = GetWindowDC( GetDesktopWindow() );

      if (hdcScreen)
      {
            TextOut( hdcScreen, iColumn, iRow, lpszMsg, lstrlen(lpszMsg));

            // Release the screen DC
            ReleaseDC( GetDesktopWindow(), hdcScreen );
      }
}

void TransparentBlt( HDC hdcDest, int nXDest, int nYDest, int nWidth,
                  int nHeight, HBITMAP hBitmap, int nXSrc, int nYSrc,
                  COLORREF colorTransparent, HPALETTE hPal )
{
      CDC dc, memDC, maskDC, tempDC;
      dc.Attach( hdcDest );
      maskDC.CreateCompatibleDC(&dc);
      CBitmap maskBitmap;
      
      //add these to store return of SelectObject() calls
      CBitmap* pOldMemBmp = NULL;
      CBitmap* pOldMaskBmp = NULL;
      HBITMAP hOldTempBmp = NULL;
      
      memDC.CreateCompatibleDC(&dc);
      tempDC.CreateCompatibleDC(&dc);
      CBitmap bmpImage;
      bmpImage.CreateCompatibleBitmap( &dc, nWidth, nHeight );
      pOldMemBmp = memDC.SelectObject( &bmpImage );
      
      // Select and realize the palette
      if( dc.GetDeviceCaps(RASTERCAPS) & RC_PALETTE && hPal )
      {
            ::SelectPalette( dc, hPal, FALSE );
            dc.RealizePalette();
            
            ::SelectPalette( memDC, hPal, FALSE );
      }
      
      hOldTempBmp = (HBITMAP) ::SelectObject( tempDC.m_hDC, hBitmap );
      
      memDC.BitBlt( 0,0,nWidth, nHeight, &tempDC, nXSrc, nYSrc, SRCCOPY );
      
      // Create monochrome bitmap for the mask
      maskBitmap.CreateBitmap( nWidth, nHeight, 1, 1, NULL );
      pOldMaskBmp = maskDC.SelectObject( &maskBitmap );
      memDC.SetBkColor( colorTransparent );
      
      // Create the mask from the memory DC
      maskDC.BitBlt( 0, 0, nWidth, nHeight, &memDC,
            0, 0, SRCCOPY );
      
      // Set the background in memDC to black. Using SRCPAINT with black
      // and any other color results in the other color, thus making
      // black the transparent color
      memDC.SetBkColor(RGB(0,0,0));
      memDC.SetTextColor(RGB(255,255,255));
      memDC.BitBlt(0, 0, nWidth, nHeight, &maskDC, 0, 0, SRCAND);
      
      // Set the foreground to black. See comment above.
      dc.SetBkColor(RGB(255,255,255));
      dc.SetTextColor(RGB(0,0,0));
      dc.BitBlt(nXDest, nYDest, nWidth, nHeight, &maskDC, 0, 0, SRCAND);
      
      // Combine the foreground with the background
      dc.BitBlt(nXDest, nYDest, nWidth, nHeight, &memDC,
            0, 0, SRCPAINT);
      
      
      if (hOldTempBmp)
            ::SelectObject( tempDC.m_hDC, hOldTempBmp);
      if (pOldMaskBmp)
            maskDC.SelectObject( pOldMaskBmp );
      if (pOldMemBmp)
            memDC.SelectObject( pOldMemBmp );
      
      dc.Detach();
}



0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.