How to override OnEraseBkgnd of a transparent window?

Posted on 1998-12-07
Last Modified: 2013-11-20
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.
Question by:xtan
  • 2

Expert Comment

ID: 1325853
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 :)

Accepted Solution

wyy_cq earned 100 total points
ID: 1325854
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;
            int x = (rect.Width() - cxIcon + 1) / 2;
            int y = (rect.Height() - cyIcon + 1) / 2;

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


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


Expert Comment

ID: 1325855
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;
      H = R.bottom -;
      W = R.right - R.left;

      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
                        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.
                                    Set = 1;
                                    //Add pixel to region
                                    Working.CombineRgn( &Working, &Temp,RGN_OR );
            FirstRun = 0;
            //flip to get the non-transparent region
            Working.CombineRgn( &Working, &Temp,RGN_XOR    );
            if (Set) SetWindowRgn((HRGN)Working,TRUE);
            //redraw just incase.. trust me.. leave this here

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 );
      CBitmap maskBitmap;
      //add these to store return of SelectObject() calls
      CBitmap* pOldMemBmp = NULL;
      CBitmap* pOldMaskBmp = NULL;
      HBITMAP hOldTempBmp = NULL;
      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 );
            ::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.BitBlt(0, 0, nWidth, nHeight, &maskDC, 0, 0, SRCAND);
      // Set the foreground to black. See comment above.
      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 );


Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 129
matchUp  challenge 6 70
post4 challenge 28 105
Updating statistics with error notification email in SQL server 4 110
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question