Click here to Skip to main content
15,795,887 members
Articles / Desktop Programming / Windows Forms

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

Rate me:
Please Sign up or sign in to vote.
4.85/5 (95 votes)
28 Sep 2012CPOL3 min read 2.9M   32.7K   350   94
This article tries to find a way to show Windows standard controls on layered windows. Provides both Native MFC and WinForms source code.

Introduction

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

Image 1

SemiTranDlgWithCtrls/8.jpg

SemiTranDlgWithCtrls/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:

SemiTranDlgWithCtrls/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.

C++
// 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.

C#
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.

Image 5

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)


Written By
Team Leader
China China
Jerry is from China. He was captivated by computer programming since 13 years old when first time played with Q-Basic.



  • Windows / Linux & C++
  • iOS & Obj-C
  • .Net & C#
  • Flex/Flash & ActionScript
  • HTML / CSS / Javascript
  • Gaming Server programming / video, audio processing / image & graphics


Contact: vcer(at)qq.com
Chinese Blog: http://blog.csdn.net/wangjia184

Comments and Discussions

 
AnswerRe: Can you convert it to VC 6 or VC.Net? Pin
akuma966011021-Apr-09 21:48
akuma966011021-Apr-09 21:48 
GeneralRe: Can you convert it to VC 6 or VC.Net? Pin
Jerry.Wang23-Apr-09 16:58
Jerry.Wang23-Apr-09 16:58 
GeneralRe: Can you convert it to VC 6 or VC.Net? Pin
akuma966011024-Apr-09 0:34
akuma966011024-Apr-09 0:34 
GeneralRe: Can you convert it to VC 6 or VC.Net? Pin
Jerry.Wang7-May-09 4:22
Jerry.Wang7-May-09 4:22 
GeneralRe: Can you convert it to VC 6 or VC.Net? Pin
akuma96601107-May-09 18:43
akuma96601107-May-09 18:43 
GeneralRe: Can you convert it to VC 6 or VC.Net? Pin
dinggen30-May-09 1:51
dinggen30-May-09 1:51 
GeneralRe: Can you convert it to VC 6 or VC.Net? Pin
Michel Gerritse7-May-09 4:41
Michel Gerritse7-May-09 4:41 
GeneralRe: Can you convert it to VC 6 or VC.Net? Pin
akuma96601107-May-09 18:53
akuma96601107-May-09 18:53 
Thanks for your kindly advise, but our f**king IT policy says, "When there is a commercial version of one software, you can't use its free version... ". Really bulls**t, isn't it? Poke tongue | ;-P

Be it so

GeneralVista Aero Effect Pin
zengkun10023-Mar-09 19:16
zengkun10023-Mar-09 19:16 
GeneralRe: Vista Aero Effect Pin
Jerry.Wang23-Mar-09 21:30
Jerry.Wang23-Mar-09 21:30 
GeneralGreat! You've got 5 of me! Pin
Tiep Le23-Mar-09 18:16
Tiep Le23-Mar-09 18:16 
GeneralRe: Great! You've got 5 of me! Pin
Jerry.Wang23-Mar-09 21:35
Jerry.Wang23-Mar-09 21:35 
QuestionHow can i build a resizable dialog using the effect? Pin
zwicker19-Mar-09 23:38
zwicker19-Mar-09 23:38 
AnswerRe: How can i build a resizable dialog using the effect? Pin
Jerry.Wang20-Mar-09 5:04
Jerry.Wang20-Mar-09 5:04 
Generalerror run under vs2008 Pin
zwicker19-Mar-09 18:48
zwicker19-Mar-09 18:48 
GeneralRe: error run under vs2008 Pin
Jerry.Wang19-Mar-09 19:31
Jerry.Wang19-Mar-09 19:31 
GeneralRe: error run under vs2008 Pin
ERICKYCheng3-Aug-10 23:34
ERICKYCheng3-Aug-10 23:34 
Generalcannot download files... Pin
Thierry Maurel15-Mar-09 12:55
Thierry Maurel15-Mar-09 12:55 
GeneralRe: cannot download files... Pin
Jerry.Wang15-Mar-09 14:53
Jerry.Wang15-Mar-09 14:53 
GeneralRe: cannot download files... Pin
Thierry Maurel16-Mar-09 0:12
Thierry Maurel16-Mar-09 0:12 
GeneralAWESOME Pin
tamash_ionut15-Mar-09 9:58
tamash_ionut15-Mar-09 9:58 
GeneralRe: AWESOME Pin
Jerry.Wang15-Mar-09 14:42
Jerry.Wang15-Mar-09 14:42 
GeneralRe: AWESOME Pin
tamash_ionut15-Mar-09 22:42
tamash_ionut15-Mar-09 22:42 
GeneralRe: AWESOME Pin
Jerry.Wang16-Mar-09 5:15
Jerry.Wang16-Mar-09 5:15 
General两个问题 Pin
Satervalley15-Mar-09 5:35
Satervalley15-Mar-09 5:35 

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.