Click here to Skip to main content
15,881,559 members
Articles / Desktop Programming / MFC
Tip/Trick

Simple Way to Make Transparent Dialog

Rate me:
Please Sign up or sign in to vote.
4.44/5 (6 votes)
5 Aug 2013CPOL 26.5K   1.7K   18   2
Enable user to make a dialog transparent without making its child control transparent
Image 1

Introduction

This is my first tip. I am writing a simple block of code to make a dialog transparent. I have seen many articles which make a dialog completely transparent with its child control too.

This tip uses a simple trick to make a transparent dialog without making its child controls transparent.

Using the Code

The idea behind making a transparent dialog is:

We can set the opacity and transparency color key of a layered window.

I used SetLayeredWindowAttributes with LWA_COLORKEY to replace a color with transparency, in order to achieve a transparent background.

The code that makes dialog is:

  1. Set the background color of the dialog which is not used in the application. I selected a color (e.g., RGB(1,11,21)) that is not present anywhere except in the dialog background.
  2. Set that color transparent using SetLayeredWindowAttributes with the LWA_COLORKEY flag.
C++
SetBackgroundColor(RGB(1,11,21));
LONG ExtendedStyle = GetWindowLong(GetSafeHwnd(),GWL_EXSTYLE );
SetWindowLong(GetSafeHwnd(),GWL_EXSTYLE,ExtendedStyle | WS_EX_LAYERED );
::SetLayeredWindowAttributes(GetSafeHwnd(),RGB(1,11,21),0,LWA_COLORKEY);

Paste the above code in the OnInitDialog() function of your dialog class. Use Visual Studio 2010 and derive your dialog class from the CDialogEx class.

License

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


Written By
Software Developer Retina Software Pvt Ltd
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionQuestion Pin
Raymart Medel4-Mar-18 2:14
Raymart Medel4-Mar-18 2:14 
GeneralMy vote of 2 Pin
GOLDENPrinciple11-Sep-13 5:39
GOLDENPrinciple11-Sep-13 5:39 
it doesn't explain the details. maybe more source code will be better.

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.