Click here to Skip to main content
Click here to Skip to main content

General solution for transparent controls

By , 14 Oct 2008
 

Introduction

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.

Background

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.

Features

  • 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
    …….

    CSliderCtrl::PreSubclassWindow();
    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 message WM_NOTIFY_TRANSPARENT

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);
    }
    else
    {
        ::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.

License

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

About the Author

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

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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionWhy not select the background bitmap of dialog into the memory device context directlymemberhjfyyy15 Mar '12 - 18:05 
Why not select the background bitmap of dialog into the memory device context directly? In function BuildBkDC(), a temporary memory device context is created, i.e, memDC, is this necessary?
Generaltransparent the toolbarmemberguyuewuhua29 Aug '09 - 21:23 
transparent the toolbar?
QuestionHow to extend this to system menu .. Close, Maximize and Minimize buttons ?memberMember 239604213 Nov '08 - 5:45 
Thanks for the article. I would like to know How to extend this to system menu .. Close, Maximize and Minimize buttons ?
AnswerRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ?membergalexding13 Nov '08 - 13:35 
You can refer to my site: http://www.uieasy.com, there is a library DSkinLite can help you do this.
 
Software architect and developer with over 8 years of experience, specializing in window development using C++. Welcome to my home page:http://www.uieasy.com/.

GeneralRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ?memberMember 239604217 Nov '08 - 4:33 
Thanks but but i don't have any intentions to use 3rd party library where i need to maintain skin images , XML file .. etc. (Though your library is pretty pretty and simple to use).
 
I am interested to see snippet where i can extend b/g image control to system menu.
GeneralRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ?membergalexding18 Nov '08 - 4:19 
You can use a toolbar menu to replace the system menu, then you can transparent the toolbar.
You can refer to this http://www.codeproject.com/KB/menus/menubarxp.aspx.
or my Blog,http://www.uieasy.com/blog/?p=28
You can use the method without using dskinlite.
 
Software architect and developer with over 8 years of experience, specializing in window development using C++. Welcome to my home page:http://www.uieasy.com/.

GeneralRe: How to extend this to system menu .. Close, Maximize and Minimize buttons ?memberMember 239604218 Nov '08 - 4:24 
Thanks a lot and i really appreciate your generosity!!!!
 
I am looking for Dialog based application where i want to extend b/g image to system menu ( Like Maximize, Minimize & Close button menu).
GeneralOverlapping controlsmemberLee Gaiteri27 Oct '08 - 11:47 
I took a look at this and discovered that it doesn't really support transparent controls overlapping. I've been looking all over for a good solution to that but I haven't found anything useful. The simplicity of this technique is excellent, but it won't work for a case where controls could end up on top of one another.
GeneralRe: Overlapping controlsmembergalexding29 Oct '08 - 5:18 
This version doesn't support the muti-layer transparent, I will release another new version to support this feature.
 
Software architect and developer with over 8 years of experience, specializing in window development using C++. Welcome to my home page:http://www.uieasy.com/.

GeneralFew commentsmemberakirilov21 Oct '08 - 0:07 
Reading introduction of your article I thought "this is what I'm looking for", but looking in the source ... disappointment lied there. The two main things that took my attention were fake:
 
(1)"No framework dependency."
Looking in the source, it looks like it is quite MFC dependent.
 
(2)"Transparent controls"
Maybe I'm wrong but from a long time this term means not just transparent pixels, rather transluctancy (per pixel transparency).
 
P.S. At least change the picture of the thumb of the slidebar - make the blue part transparent
GeneralRe: Few commentsmembergalexding21 Oct '08 - 5:46 
(1)"No framework dependency."
For this feature, I mean the method that you can use it in other framework,but you should modify some code, And the mfc test is just a example,the button,sliderctrl, static are also example control.You can abstract the code "CTransparentHelper" and some other code to your application.
in a word, you should read the code, then use the method in your application, it is not a direct-use code.
(2) "transparent control"
The transparent control in this article is making some regions of a control to use the parent window's background at the same position.So it looks like "transparent".
 
Software architect and developer with over 8 years of experience, specializing in window development using C++. Welcome to my home page:http://www.uieasy.com/.

QuestionExcellent !! Do you plan for checkbox controls support ?memberThierry Maurel15 Oct '08 - 2:58 
Hello,
good job, do you plan to add the checkboxes in your version of the classic controls ?
 
modified on Thursday, October 16, 2008 5:29 AM

AnswerRe: Excellent !! Do you plan for checkbox controls support ?membergalexding16 Oct '08 - 14:10 
Thanks for use it. It is a general solution, and you can add control of checkbox in it easily.
I will add the checkbox demo in it in next version.
 
Software architect and developer with over 8 years of experience, specializing in window development using C++. Welcome to my home page: http://www.uieasy.com.

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 14 Oct 2008
Article Copyright 2008 by galexding
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid