Click here to Skip to main content
15,881,812 members
Articles / Desktop Programming / MFC

CGlassDialog

Rate me:
Please Sign up or sign in to vote.
3.98/5 (24 votes)
11 Aug 2006CPOL2 min read 103.3K   1.6K   49   28
This is a simple class that makes transparent dialog boxes rise more smoothly.

Sample Image - CGlassDialog.jpg

Introduction

Before introducing this class, I want you to know my English is not very good. And, this is my first article. I've always got a lot of help from this site, so I really wanted to write an article and give back to the community.

Anyway, this is a simple class that makes transparent dialog boxes rise more smoothly. The reason why I made this is.. I've been making a program that has transparent windows. I thought if the windows could rise in more smoothly, it would be better visually. And I made up my mind to make this class.

How to make this??

It's so simple, I use an OnTimer() for the smooth rising.

First, you must declare this in your .h file:

C++
#define WS_EX_LAYERED   0x00080000
#define LWA_COLORKEY    0x00000001
#define LWA_ALPHA       0x00000002
#define ULW_COLORKEY    0x00000001
#define ULW_ALPHA       0x00000002
#define ULW_OPAQUE      0x00000004

typedef BOOL(WINAPI *SLWA)(HWND, COLORREF, BYTE, DWORD); 

and then, you can use this code:

C++
SLWA pSetLayeredWindowAttributes = NULL;  
HINSTANCE hmodUSER32 = LoadLibrary("USER32.DLL"); 
pSetLayeredWindowAttributes =(SLWA)GetProcAddress(hmodUSER32, 
           "SetLayeredWindowAttributes");
SetWindowLong(m_hWnd, GWL_EXSTYLE, 
           GetWindowLong(m_hWnd, GWL_EXSTYLE) | WS_EX_LAYERED);
pSetLayeredWindowAttributes(m_hWnd, 0, (255 * m_nGlass) / 100, LWA_ALPHA);

In this code, m_nGlass is the rating of transparency so you can set this from 0 to 100. (If you set m_nGlass to 0, the dialog box will be perfectly transparent. If you set m_nGlass to 100, the dialog box will not be transparent at all.)

I've used this point. OnTimer() is the event function of WM_TIMER, so it makes executes the function in regular intervals.

in OnTimer(), you change the value of m_nGlass:

(m_nGlass >= 70) ? ReleaseTimer() : m_nGlass += m_nStepUnit; 

How to use this??

If you want to use this class, first, add GlassiDialog.h and GlassDialog.cpp into your project and #include GlassDialog.h where needed. Then, inherit the CGlassDialog class in your dialog class. (I.e., you should change the base class in your dialog from CDialog to CGlassDialog. Just go to your .cpp, .h files and use the "Replace" feature of your code editor to replace all CDialog with CGlassDialog.)

Now, that's all! Your dialog class is ready to go. When you create your dialog object, you will be able to see a smooth rising dialog box.

Thank you for reading my article!! :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Korea (Republic of) Korea (Republic of)
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Your Project Pin
Youngman Park8-Aug-06 17:39
Youngman Park8-Aug-06 17:39 
Generalgood job Pin
MrGoodly6-Aug-06 8:22
MrGoodly6-Aug-06 8:22 
GeneralRe: good job Pin
Youngman Park7-Aug-06 3:37
Youngman Park7-Aug-06 3:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.