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

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 136
Replacement selected text 2 61
unable to delete all specified values regedit 38 410
List out all word 7 292
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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.

773 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