Solved

How to override OnEraseBkgnd of a transparent window?

Posted on 1998-12-07
3
1,632 Views
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.
0
Comment
Question by:xtan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 6

Expert Comment

by:snoegler
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 :)
0
 
LVL 2

Accepted Solution

by:
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;
            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
 
LVL 2

Expert Comment

by:wyy_cq
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;
      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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows 10 how make picture as desktop background 2 73
zeroMAx challenge 20 135
Annoying "thing" blocks my view 4 146
Change to event 1 138
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa‚Ķ
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.

752 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