Solved

Making a transparent dialog

Posted on 2003-11-21
9
499 Views
Last Modified: 2013-11-20
I am trying to make a transparent dialog. I have succeeded using a bitmap mask to set the windows region to render, however I need to animate the control within the dialog, and this does not work so well with the masking method.

I have managed to make the background appear transparent by intercepting WM_ERASBKGD, however the background moves with the dialog when it is dragged.

I am assuming that what I need to do is repaint all the windows behind my dialog when it is moved. Does this sound right? And if this is the right way to go about it, how do I repaint other windows?

200 points is for if someone can tell me the method and point me in the right direction. If there is a nice full solution or example someone can give me there are more points up for grabs.
0
Comment
Question by:completecontrol
  • 5
  • 2
  • 2
9 Comments
 
LVL 19

Accepted Solution

by:
Dexstar earned 200 total points
ID: 9798412
@completecontrol:

If you want to make a window transparent, then it is easiest to use the Windows API function to do it.  It only works on Windows 2000 or higher, but the code looks like this:

      #ifndef WS_EX_LAYERED
      #define WS_EX_LAYERED           0x00080000
      #define LWA_COLORKEY            0x00000001
      #define LWA_ALPHA               0x00000002
      #endif // ifndef WS_EX_LAYERED

      // Preparation for the function we want to import from USER32.DLL
      typedef BOOL (WINAPI *lpfnSetLayeredWindowAttributes)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);

      BOOL MakeWindowTransparent( CWnd* pWnd, BYTE nTransLevel )
      {
            BOOL bSuccess = FALSE;
            OSVERSIONINFO os = { sizeof(os) };
            ::GetVersionEx( &os );

            if ( VER_PLATFORM_WIN32_NT == os.dwPlatformId && os.dwMajorVersion >= 5 )
            {
                  lpfnSetLayeredWindowAttributes pSetLayeredWindowAttributes;

                  // Here we import the function from USER32.DLL
                  HMODULE hUser32 = ::GetModuleHandle(_T("USER32.DLL"));
                  pSetLayeredWindowAttributes =
                        (lpfnSetLayeredWindowAttributes)::GetProcAddress( hUser32, "SetLayeredWindowAttributes" );

                  if ( pSetLayeredWindowAttributes )
                  {
                        pWnd->ModifyStyleEx( 0, WS_EX_LAYERED );
                        bSuccess = pSetLayeredWindowAttributes( pWnd->m_hWnd, 0, nTransLevel, LWA_ALPHA );
                  }

                  ::FreeLibrary( hUser32 );
            }
            return bSuccess;
      }

The "nTransLevel" value is how transparent you want the window to be.  255 = Invisible, 0 = Totally Visible.  So, to set it to be 50/50, you would do this:
     MakeWindowTransparent( wnd, 128 );


Hope That Helps,
Dex*
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9798418
@completecontrol:

That function will work on any version of Windows... It just won't set the transparency on version of Windows before 2000.

Dex*
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9798513
@completecontrol:

Finally, here is a link that explains everything in more detail:  http://www.codeproject.com/win32/quaker1.asp

Dex*
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9802154
Your dialog will receive a WM_MOVE message when it is being moved.  Here you need to repaint the background of your dialog.
Maybe have a flag (bool member var) that switches on/off the normal erase background handler you have customised.
Other windows should repaint themselves normally.
0
 

Author Comment

by:completecontrol
ID: 9811718
Thanks for your help. Something urgent has come up at work, so I can't work on this till later in the week. I haven't abandoned the thread though. I'll try your suggestions and see how they go. One thing I should have mentioned though - it has to work on Win98 which is why I was using the SetWindowRgn method. What I really need help on is the repainting stuff.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9812733
The MW_MOVE and WM_ERASEBACKGROUND should apply to Win98 without any problems.
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9819199
Well, that won't work under Win98, but I'm not sure of anything that will.  When you say you need to make it "transparent", do you mean you need to make it so that you can half see your window and half see what's under it, or you do just want to set a region of the dialog to be completely clear (essentially, make a window that isn't a rectangle)?

Dex*
0
 

Author Comment

by:completecontrol
ID: 9884546
Sorry about not getting back here. I haven't had a chance to work on the project since I first posted. I don't know whne I will either so I am going to award the points to Dexstar for replies above and beyond the call of duty
0
 
LVL 19

Expert Comment

by:Dexstar
ID: 9884589
Thank you!  Good luck with that in the future!

D*
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.

860 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