Click here to Skip to main content
Licence CPOL
First Posted 15 Mar 2009
Views 252,956
Downloads 11,055
Bookmarked 248 times

Cool, Semi-transparent and Shaped Dialogs with Standard Controls for Windows 2000 and Above

By | 7 May 2009 | Article
This article tries to find a way to show Windows standard controls on layered windows. Provides both Native MFC and WinForms source code.
Prize winner in Competition "Best C++/MFC article of March 2009"

Introduction

First, let me show you some screenshots captured from the demo program.

8.jpg

3.jpg

The program demonstrates semi-transparent dialogs which are compatible with Windows 2000 and higher.

Background

Layered windows, supported from Windows NT 5.0, provide a way to create windows that have a complex shape with alpha blending effects. The major challenge is how to show standard controls on layered windows.

The following shows the mechanism:

4.jpg

When the dialog is being created, a fake window is created by CreateWindowEx with the styles WS_EX_LAYERED, WS_EX_TRANSPARENT, and WS_EX_NOACTIVATE. The alpha value of the real window will be modified to 5 by SetLayeredWindowAttributes so that the real window is almost transparent.

The real window is in charge of processing user input events and Windows messages; the fake one is in charge of the presentation. The fake window is always kept the same size / position as the real one.

How do we show standard controls on the fake window? When the presentation needs to be refreshed, the background image is painted first. Then, all the child controls will be captured by sending the WM_PRINT message, and painted at the same position on the fake window. Especially, for EDIT controls, EditBox / Editable ComboBox / etc., we need to draw the caret by ourselves.

When should we refresh the presentation? We need to refresh the fake window when there is an update on the UI. In the demo, it hooks into all the child controls recursively, and changes the WNDPROC address by SetWindowLongPtr with the parameter GWLP_WNDPROC.

You can get more details by looking into the source code.

Using the Code in Native C++ / MFC

First Step

Copy all the files in /Src/*.* to your project.

Second Step

You need an image file to be the dialog background. You'd better choose PNG or TIFF which support the alpha channel. The image file can be embedded into a resource or placed on disk, judged by yourself.

Final Step

Replace the dialog base class from CDialog to CImgDialogBase.

// Load from disk file
CDemo2Dlg::CDemo2Dlg(CWnd* pParent /*=NULL*/)
    : CImgDialogBase( CDemo2Dlg::IDD
    , CUtility::GetModulePath() + _T("background.png")
    , pParent
    )
{

}

// Or load from resource
CDemo3Dlg::CDemo3Dlg(CWnd* pParent /*=NULL*/)
    : CImgDialogBase(CDemo3Dlg::IDD
    , IDB_PNG_DLG2
    , _T("PNG")
    , AfxGetResourceHandle()
    , pParent
    )
{

}

Using the Code in WinForms/.NET

First Step

Copy files in the /Src/*.* directory to your project.

Second Step

You need an image file to be the dialog background. You'd better choose PNG or TIFF which support alpha channel.

Final Step

Replace the dialog base class from Form to ImageDlgBase.

public partial class Form2 : CoolImageDlg.ImageDlgBase
{
    public Form2()
    {
        base.DlgBgImg = ImgDlgSample.Properties.Resources.DemoDlgBg2;
        //......
    }
}

Something Important

The dialog works in a way that if there is one pixel that needs to be updated, the whole window will be refreshed. Therefore, if the dialog is very large and complex, or has a lot of child controls, it may cause performance issues.

Some of the controls cannot work with WM_PRINT; in that case, the control won't display correctly. In other words, not all controls are supported.

The sample code uses the GDI+ helper class from Zoltan Csizmadia. For those who do not want to use GDIPlus.dll, CxImage is another choice.

Most of the machines use 96 DPI as their monitor setting. For those machines which use an unusual DPI setting, please note the demo is not meant for such settings and the child controls will be misplaced. You need to add your own code to re-layout the child controls if you do need to support unusual DPIs.

License

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

About the Author

Jerry.Wang

Architect
Best Brain
China China

Member

Jerry is working for the Best Brain ChangSha Office, China. He has been being interested in computer programing from his childhood. Skilled in Windows/Linux C++, Mac OS Objective-C, .Net & C#, ASP.Net, AS3/JS/PHP etc.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHello! I have a question to ask 【你好 我有个问题想问 】 [modified] PinmemberMember 848367320:17 3 Apr '12  
QuestionHow to show Transparent Static Control Pinmemberreplyam0:19 30 Aug '11  
GeneralMy vote of 5 Pinmemberlimitswen20:14 23 Aug '11  
GeneralMy vote of 5 PinmemberNowBie6:05 27 Jul '11  
GeneralMy vote of 5 PinmemberJamming111:40 17 Jun '11  
GeneralMy vote of 5 Pinmemberrahulrcn1:19 30 May '11  
GeneralMy vote of 5 Pinmemberwild221:34 3 Mar '11  
GeneralWhen I added the slider control, it would not be print on fake windows Pinmemberxujingyu18:19 7 Oct '10  
Generalgood. win7's glass to xp Pinmemberwave8263:32 12 Aug '10  
GeneralGreatJob Pinmembergyrtenudre2:30 9 Aug '10  
GeneralMy vote of 5 PinmemberAzadar.H7:00 17 Jul '10  
QuestionAdd Panel control PinmemberYoni924:13 3 Jun '10  
Generalfake windows and real windows PinmemberGlen Hadi16:27 30 Mar '10  
GeneralA Question Pinmemberzhaoxy285019:58 6 Mar '10  
GeneralTransparent controls PinmemberMickael121:10 24 Feb '10  
GeneralRe: Transparent controls PinmemberJerry.Wang16:37 8 Mar '10  
GeneralWM_PAINT Pinmemberdima_kdl5:22 7 Nov '09  
GeneralRe: WM_PAINT PinmemberJerry.Wang13:28 7 Nov '09  
Generalgood job,good boy! Pinmemberhuangzongwu1:45 16 Oct '09  
GeneralCode is slow to execute PinmemberDiamonddrake5:44 3 Sep '09  
GeneralRe: Code is slow to execute PinmemberD.K.Wang23:20 4 Sep '09  
GeneralAnother Good Article, goto http://www.codeproject.com/KB/dialog/PerfectTranlucentDlg.aspx PinmemberD.K.Wang4:11 2 Sep '09  
GeneralRe: Another Good Article, goto http://www.codeproject.com/KB/dialog/PerfectTranlucentDlg.aspx PinmemberJerry.Wang8:30 3 Nov '09  
QuestionSDI/MDI PinmemberGwanho16:11 31 Aug '09  
QuestionI got a problem of string conversion Pinmemberbluespeed00116:46 29 Jul '09  

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 7 May 2009
Article Copyright 2009 by Jerry.Wang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid