Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - transparentdialog.gif

Introduction

This dialog is derived from CDialog. A bitmap is loaded from a file and a region created based on a transparent colour. The bitmap is then used to paint the dialog. This demo also demonstrates dragging the dialog by clicking anywhere on a visible portion of the dialog, timer events and a button on the transparent dialog.

It is perfect to be used as a splash screen or similar application or as a skeleton application for non rectangular windows that are shaped by a supplied bitmap.

The code

You should be able to use this demo as a skeleton application for your own project.

A standard CDialog MFC EXE application was created with the wizard and apart from the standard supplied code, the following functions were overridden.

void OnClose() Used to clean up some resources on exit.
void OnPaint() Used to paint the bimap into the region.
void OnSize(UINT nType, int cx, int cy) Used to create the region and position and resize the dialog.
void OnLButtonDown(UINT nFlags, CPoint point) To allow the dialog to be dragged by clicking anywhere.

A button was created to allow the user to close the dialog. A timer was created to demonstrate auto closing after a period of time.

The image is a normal bmp file that is loaded by the app and used to paint the dialog. TRANSPARENTCOLOR is defined in the dialog class header as bright purple (RGB(255, 0, 255), but this could be changed to any colour you like.

The dialog window is auto sized to the size of the loaded bitmap then a region created based on the transparent colour. The dialog is then positioned in the centre of the current screen. This was done as an example for those who want to use this application as a splash windows.

An example of using this class is as follows:

CTransparentDialogDlg dlg;
int nResponse = dlg.DoModal();

The button serves no real purpose and was made a close button simply to demonstrate using a button.

History

V1.0 initial release 27th November 2002

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalbuttons
basculo
12:28 29 Apr '09  
how to place buttons ? when i place a new button on dilog it does not appears
GeneralQuestion on the way you exit application
andyluu
9:30 19 Jan '09  
this is a very nice way to implement transparency! Thumbs Up Thanks for the code.

I just have a question about the way you auto close the application. You used PostQuitMessage(0). AFAIK, PostQuitMessage will post WM_Quit and terminate application.However, I read somewhere that using PostQuitMessage only may leave memory leak.

Someone recommend using PostMessage(WM_Close) on main window. It will call DestroyWindow(). The DestroyWindow will destroy all windows and then call PostQuitMessage.

Do you think PostQuitMessage(0) is the better way to auto close a window? TIA.
GeneralRe: Question on the way you exit application
Nic Wilson
11:50 19 Jan '09  
hi,

i was unaware of that issue. It needs to be tested i suppose to prove the leak. but the way you suggest is fine I suppose.

nicw
Generalit's great.
Mohamad Kaifi
1:19 8 May '08  
its wonderful sir...i m new to development.and i m very much interested in MFC.can u give me any links or articles...i'll be thankful to u sir....
GeneralRe: it's great.
Nic Wilson
11:54 19 Jan '09  
There are many good books on MFC the eaisest to follow would be the "Osborne" "The Complete Reference" series they have some books on Visual C++ which have some good MFC examples. I use the MSDN library articles quite a lot, just looking at each MFC class and them implementing the ones I want or need.

nicw
MSN nicwilson@hotmail.com
GeneralGood, It's what I'm looking for ...
min_2_max
23:11 15 Aug '07  
Thanks.

I'm looking for a splash implementation that is similar to that of Photoshop.
The main function I need is to change the text on the splash is changeable at runtime.

After a while, I realized that a correct way to this is an implementation based on a dialog.
It's what I need.

One question here:

I'm using it in a Doc/View program instead of a dialog program, so I don't want the program
to exit, as the demo program, on the timer message, but just close the transparent dialog,
I change the code in CTransparentDialogDlg::OnTimer(UINT nIDEvent) from

KillTimer(1);
PostQuitMessage(0);
CDialog::OnTimer(nIDEvent);

to
KillTimer(1);
OnClose();
CDialog::OnTimer(nIDEvent);

but the splash just don't close as anticipated. Why?

Thanks again.

max
GeneralRe: Good, It's what I'm looking for ...
min_2_max
23:14 15 Aug '07  
Ok, I solved this by replacing

OnClose();

with

PostMessage(WM_CLOSE);

Max
GeneralRe: Good, It's what I'm looking for ...
min_2_max
23:40 15 Aug '07  
As a step further, how can I keep showing the splash and, meanwhile, do the
data initializing and updating a progress bar on the splash dialog?

Thanks for any hints from anybody.

Max


GeneralGetting an Error
simhadri
23:29 13 Mar '06  
Hi,
while I am running the application I am getting a message box "Error loading Bitmap".

what shd I do to solve this..???

GeneralRe: Getting an Error
Code Incomplete...
18:41 11 Apr '06  
The app looks in the same directory (folder) that the .exe file is started in. The author put the image.bmp file in the Release dir, so if you set run the project in release mode, it will find it. If you want to run in debug mode, copy image.bmp into the Debug dir.

Generalhi,greate, but can it is used with ws_child window?
wwx
18:03 13 Sep '05  
I tried to use it with ws_child, but it didn't work, is it possible? can u give me a simple sample code? thanks,

give a hand,have a heart
Generalcapture hiden window
jags_vc
20:36 16 Feb '05  




I have a problem like this.i have hooked
WM_CREATE message and whenewer application window is created
i just takes it HWND and passes user Defined message to another application that application handles userdefined message(This application is also my application).In that message handlers i have kept delay of 100 ms and capture the image and then hide it.
That is work but when i hide window in WM_CREATE hook procedure then pass Userdefined message to my application then it can`t be captured insted it capures only active window`s GUI of Size of my hiden application window.


I think that to capture any window it is requred to be visible.
but is it possible that by copying memory content of Windows Gui we can also capture the image of window ?

Confused
Generalproblem with the position of the window
Marco Randazzo
4:31 14 Sep '04  
I tried to move my transparent window modifying iX e iY values in the demo, but the window is always centered, why?

//iX = (GetSystemMetrics(SM_CXSCREEN)) / 2 - (m_Bitmap.bmWidth / 2);
//iY = (GetSystemMetrics(SM_CYSCREEN)) / 2 - (m_Bitmap.bmHeight / 2);
iX=800;
iY=400;
SetWindowPos(&wndTopMost,iX, iY, m_Bitmap.bmWidth, m_Bitmap.bmHeight, NULL);

PS: i was able to move the window using:
SetWindowPos(&wndTopMost,iX,iY,m_Bitmap.bmWidth,m_Bitmap.bmHeight, SWP_SHOWWINDOW) ;
but, in this way, i lose my bitmap, and i have only a gray mask of the window
GeneralTry this...
el_rata
9:57 15 Jan '04  
Not transparent, but i like it...

void CMyDialogClass::OnPaint()
{
CPaintDC dc(this); // device context for painting

PaintDesktop(dc); // Desktop wallpaper background
}
GeneralWonderful !
LeeZi
15:35 1 Sep '03  
Smile
GeneralGreat!! But is it possible??
AWebDude
15:24 12 Mar '03  
I'm learning VC++ so, I might sound lil stupid but is it possible to have a bmp file with single color and then make it transparent so the dialog box not at all appears on the screen and you see the desktop through dialog?
GeneralImage.bmp is not provided with the zip
Shanhaz
0:51 12 Mar '03  
Can you add the image.bmp to the zip?
GeneralRe: Image.bmp is not provided with the zip
Nic Wilson
12:18 12 Mar '03  
It is provided look again. After unzipping you will find it in the "Release" directory.

Nic

GeneralNice!
manos_crete
6:55 28 Dec '02  
It's a nice way to easily generate resource files for
transparent dialog applications!
Here is some code to perform serialization:

void Serialize(CArchive& ar, CRgn* pRgn)
{
   if( ar.IsStoring() )
   {
         // store window region
         int nCount = pRgn->GetRegionData(NULL, 1);
         if( nCount>0 )
         {
            LPBYTE pbuffer = new BYTE[nCount];
            memset(pbuffer, 0, nCount);
            int nRet = pRgn->GetRegionData((LPRGNDATA)pbuffer, nCount);
            ar << nCount;
            ar.Write(pbuffer, nCount);
            delete[] pbuffer;
         }
   }
   else
   {
         // load and create window region
         int nCount = 0;
         ar >> nCount;
         LPBYTE pbuffer = new BYTE[nCount];
         memset(pbuffer, 0, nCount);
         ar.Read(pbuffer, nCount);
         if( pRgn->m_hObject )
            pRgn->DeleteObject();
         pRgn->CreateFromData(NULL, nCount, (LPRGNDATA)pbuffer);
         delete[] pbuffer;
   }
}

Big Grin

--- Manolis from Crete ---
GeneralRe: Nice!
The Yariv
23:03 22 Jan '07  
can you please post the complete code?
How can i serialize this RGN to a file?
GeneralRe: Nice!
manos_crete
13:06 27 Jan '07  
Add the following functions in the header file of the dialog:

void SaveData(CRgn* pRgn);
BOOL LoadData(CRgn* pRgn);
void Serialize(CArchive& ar, CRgn* pRgn);

The implementation is:

void CTransparentDialogDlg::Serialize(CArchive& ar, CRgn* pRgn)
{
if( ar.IsStoring() )
{
// storing code
int nCount = pRgn->GetRegionData(NULL, 1);
if( nCount>0 )
{
LPBYTE pbuffer = new BYTE[nCount];
memset(pbuffer, 0, nCount);
int nRet = pRgn->GetRegionData((LPRGNDATA)pbuffer, nCount);
switch( nRet )
{
case COMPLEXREGION:
TRACE("New region has overlapping borders.\n");
break;

case ERROR:
TRACE("No new region created.\n");
break;

case NULLREGION:
TRACE("New region is empty.\n");
break;

case SIMPLEREGION:
TRACE("New region has no overlapping borders.\n");
break;
}
ar << nCount;
ar.Write(pbuffer, nCount);
delete[] pbuffer;
}
}
else {
// loading code
int nCount = 0;
ar >> nCount;
LPBYTE pbuffer = new BYTE[nCount];
memset(pbuffer, 0, nCount);
ar.Read(pbuffer, nCount);
if( pRgn->m_hObject )
pRgn->DeleteObject();
pRgn->CreateFromData(NULL, nCount, (LPRGNDATA)pbuffer);
delete[] pbuffer;
}
}

void CTransparentDialogDlg::SaveData(CRgn* pRgn)
{
CFile ff;
CFileException e;
if( ff.Open("MyRgn.set", CFile::modeCreate | CFile::modeWrite | CFile::shareExclusive, &e) )
{
CArchive ar(&ff, CArchive::store, 4096, NULL);

try {
Serialize( ar, pRgn );
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

ar.Close();
ff.Close();
}
}

BOOL CTransparentDialogDlg::LoadData(CRgn* pRgn)
{
CFile ff;
CFileException e;
if( ff.Open("MyRgn.set", CFile::modeRead, &e) )
{
CArchive ar(&ff, CArchive::load, 4096, NULL);

try {
Serialize( ar, pRgn );
}
catch(CFileException* e)
{
e->ReportError();
e->Delete();
}

ar.Close();
ff.Close();

return TRUE;
}

return FALSE;
}

Call LoadData inside the OnInitDialog and SaveData before you close the dialog.

good luck.

Manolis from Crete Smile

GeneralThanks, but...
zarzor
18:31 4 Dec '02  
can you do it without MFC (say, Win32 API). Anyways well done.Rose

RJS


Last Updated 4 Dec 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010