Click here to Skip to main content
15,396,239 members
Articles / Desktop Programming / WTL
Posted 14 Oct 2008


102 bookmarked

General solution for transparent controls

Rate me:
Please Sign up or sign in to vote.
4.77/5 (21 votes)
14 Oct 2008CPOL2 min read
A very simple, integrated method to make controls such as buttons, slider controls, and progress controls to be transparent in a dialog.


In UI development, we have to implement some nice effects usually, and making some controls to be transparent is a problem that we often meet. In this article, I present an approach to implement the transparency of controls. The source code includes the class CTransparentHelper based on the Win32 API, and it can be used in MFC, ATL, WTL, Win32 applications, or may be some other framework. I developed a new version because I used it at my work, so if you have any suggestions, bug reports, or problems, please send it to me. And, you can also visit my technical blog to get more information.


I wrote this code because I needed some transparent controls such as buttons, slider controls, and progress controls. I found that some methods available online are not effective when the parent window moves, or when the control moves; this is a problem when we want to use a transparent control in a resizable dialog. So, I wrote the CTransparentHelper; you can have a smooth transparency effect when MoveWindow is called, and the class is suitable for all controls.


  • No framework dependency.
  • Can be used for controls of different types.
  • Can be used for multi-layer transparency.
  • Easy to use in the current code.

Using the code

Before introducing how to use the source code, I suggest using a memory DC to store the background DC of the parent dialog. This is convenient for the transparency of child controls, and also boosts the efficiency of drawing. For detailed information, please refer to the source code.

Include “TransparentHelper.h”

Include “TransparentHelper.h” in the file of the control which needs to be transparent. And, add an object of type CTransparentHelper.

#pragma once
#include "TransparentHelper.h"
// CSliderCtrlEx
class CSliderCtrlEx : public CSliderCtrl
    CTransparentHelper m_objTrans;

Initialize the object of CTransparentHelper

void CSliderCtrlEx::PreSubclassWindow()

    // TODO: Add your specialized code here and/or call the base class

    m_objTrans.Install( GetSafeHwnd());

Call the function TransparentBk of CTransparentHelper when you need

BOOL CSliderCtrlEx::OnSliderDrawChannel( CDC* pDC, CRect& rect, UINT nState)
    if ( m_objTrans.IsValid() )
        m_objTrans.TransparentBk( pDC->GetSafeHdc(), GetSafeHwnd());
    return TRUE;

Add code to the control’s parent window

Sometimes, the parent window is a dialog. I need to deal with the message WM_TRANSPARENT_BK, which is sent from the transparent control in order to get back the DC.

LRESULT CTransparentControlDlg::OnTransaprentBk( WPARAM wParam, LPARAM lParam)
    HDC hdc = ( HDC)wParam;
    HWND hwnd = ( HWND)lParam;

    CTransparentHelper::OnTranparentControl( m_pMemDC->GetSafeHdc(), 
                                           (WPARAM)hdc, (LPARAM)hwnd);
    return TRUE;

Remark: The m_pMemDC is the memory DC of the dialog, which will be changed when the dialog’s size changes.

Notfiy children when background changes

When the dialog’s memory DC changes, it must notify the child which has a transparent tag.

void CTransparentControlDlg::BuildBkDC()
    //rebuild the background dc
    //when the parent dialog's background is rebuild, 
    //notify the child which has an transparent tag.
    CTransparentHelper::NotifyTransparentChild( GetSafeHwnd());


The transparent control needs to deal with the message WM_NOTIFY_TRANSPARENT sent by the parent, when the background changes.

LRESULT CSliderCtrlEx::OnNotifyTransparent( WPARAM wParam, LPARAM lParam) 
    if ( ::IsWindowEnabled( GetSafeHwnd()))
        ::EnableWindow( GetSafeHwnd(),FALSE); 
        ::EnableWindow( GetSafeHwnd(),TRUE);
        ::EnableWindow( GetSafeHwnd(),TRUE); 
        ::EnableWindow( GetSafeHwnd(),FALSE);
    //This operation is for the repaint of slider control, 
    //because Invalidate cann't bring the NM_CUSTOMDRAW message.
    //M..., this may not the best method to solve the problem. 
    //If you have other method, please tell me.

    return TRUE;

Remark: For some controls under some drawing methods (e.g., NM_CUSTOMDRAW), Invalidate will not cause a real repaint. So, I adde the WM_NOTIFY_TRANSAPRENT message to make it compatible. If the transparent control will repaint itself after calling Invalidate, it needn’t deal with the message.


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


About the Author

Software Developer (Senior)
United States United States
Software architect and developer with over 8 years of experience, specializing in window development using C++. Welcome to my home page:

Comments and Discussions

QuestionWhy not select the background bitmap of dialog into the memory device context directly Pin
hjfyyy15-Mar-12 18:05
Memberhjfyyy15-Mar-12 18:05 
Generaltransparent the toolbar Pin
guyuewuhua29-Aug-09 21:23
Memberguyuewuhua29-Aug-09 21:23 
QuestionHow to extend this to system menu .. Close, Maximize and Minimize buttons ? Pin
Member 239604213-Nov-08 5:45
MemberMember 239604213-Nov-08 5:45 
AnswerRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ? Pin
galexding13-Nov-08 13:35
Membergalexding13-Nov-08 13:35 
GeneralRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ? Pin
Member 239604217-Nov-08 4:33
MemberMember 239604217-Nov-08 4:33 
GeneralRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ? Pin
galexding18-Nov-08 4:19
Membergalexding18-Nov-08 4:19 
GeneralRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ? Pin
Member 239604218-Nov-08 4:24
MemberMember 239604218-Nov-08 4:24 
GeneralOverlapping controls Pin
Lee Gaiteri27-Oct-08 11:47
MemberLee Gaiteri27-Oct-08 11:47 
GeneralRe: Overlapping controls Pin
galexding29-Oct-08 5:18
Membergalexding29-Oct-08 5:18 
GeneralRe: Overlapping controls Pin
asrx12-Jul-14 22:12
Memberasrx12-Jul-14 22:12 
GeneralFew comments Pin
akirilov21-Oct-08 0:07
Memberakirilov21-Oct-08 0:07 
GeneralRe: Few comments Pin
galexding21-Oct-08 5:46
Membergalexding21-Oct-08 5:46 
QuestionExcellent !! Do you plan for checkbox controls support ? Pin
Thierry Maurel15-Oct-08 2:58
MemberThierry Maurel15-Oct-08 2:58 
AnswerRe: Excellent !! Do you plan for checkbox controls support ? Pin
galexding16-Oct-08 14:10
Membergalexding16-Oct-08 14:10 

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.