Solved

How to override OnEraseBkgnd of a transparent window?

Posted on 1998-12-07
3
1,503 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
  • 2
3 Comments
 
LVL 6

Expert Comment

by:snoegler
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Running shell command from Access 3 63
Adapt this command to show who installed 29 102
Hibernate methods 2 58
java ^ examples 8 57
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now