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

Transparent ListBox

, 19 Aug 2005 CPOL
Rate this:
Please Sign up or sign in to vote.
How to accomplish true transparency in listboxes.


Any experienced Windows programmer can tell you that transparency is not a trivial task in Windows. A transparent ListBox control is no exception. Actually ListBoxes are a bit harder than other controls. And the reason is the way ListBoxes do their scrolling. But overall it is a pretty simple concept to implement.

For example, in order to make a static control transparent, all anyone will have to do is to handle the WM_ERASEBKGND, and to also repaint the control when the user calls SetWindowText.

In the case of a ListBox, let’s say that we take over the WM_ERASEBKGND message and return TRUE (basically the easiest way to achieve transparency). When the user presses the down button of the Listbox’s scrollbar, what Windows does is bitblt the item's top index + 1 to the last shown item one line up, and then simply draws the new item. What happens there is that the background gets copied up with the item. Clearly not the results we would be looking for.

How to do it

So how would we achieve transparency with a ListBox? Let’s start with an owner draw ListBox.

First thing we have to do is copy the parent window's image before the first time the ListBox is drawn, this will give us the background for the listbox. We can do this in the WM_ERASEBKGND message handler. The first time this message is received the ListBox has not yet been drawn, so it is a safe place to take a snapshot of the parent window.

BOOL CTransparentListBox::OnEraseBkgnd(CDC* pDC) 
   if (!m_HasBackGround)
       CWnd *pParent = GetParent();
       if (pParent)
         CRect Rect;
         CDC *pDC = pParent->GetDC();
         m_Width = Rect.Width();
         m_Height = Rect.Height();
         CDC memdc;
         CBitmap *oldbmp = memdc.SelectObject(&m_Bmp);
         m_HasBackGround = TRUE;
   return TRUE;

The second thing that we have to handle is drawing each item on the screen. Because of the scrolling we can’t trust the ListBox to do any of our drawing for us. So we override the DrawItem method and do nothing. And in turn place the code that would normally be placed there in a separate DrawItem method which we can call when we want to paint the ListBox, which we will in the OnPaint method. What OnPaint does is simply draw the background snapshot on to a memory DC, draw the visible items on to the same memory DC, and then bitblt the entire thing on the ListBox DC. Simple so far.

void CTransparentListBox::DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct )
    //do nothing when the listbox asks you to draw an item

void CTransparentListBox::DrawItem(CDC &Dc, 
            int Index,CRect &Rect,BOOL Selected)
   if (Index == LB_ERR || Index >= GetCount())

   if ( < 0 || Rect.bottom > m_Height)
   CRect TheRect = Rect;

   CDC memdc;

   CFont *pFont = GetFont();
   CFont *oldFont = Dc.SelectObject(pFont);
   CBitmap *oldbmp = memdc.SelectObject(&m_Bmp);
   CString Text;
   if (m_Shadow)
       if (IsWindowEnabled())

   if (IsWindowEnabled())
       if (Selected)

void CTransparentListBox::OnPaint() 
   CPaintDC dc(this); // device context for painting

   CRect Rect;

   int Width = Rect.Width();
   int Height = Rect.Height();

   //create memory DC's
   CDC MemDC;
   CBitmap MemBmp;

   CBitmap *pOldMemBmp = MemDC.SelectObject(&MemBmp);

   //paint the background bitmap on the memory dc
   CBitmap *pOldbmp = dc.SelectObject(&m_Bmp);
   dc.SelectObject(pOldbmp); = 0;
   Rect.left = 0;
   Rect.bottom = + GetItemHeight(0);
   Rect.right = Width;
   int size = GetCount();
   //draw each item on memory DC
   for (int i = GetTopIndex(); i < size 
            && <= Height;++i)

   //draw the results onto the listbox dc


Third and the tricky part is handling the scroll messages. To overcome the scroll problem, we intercept the WM_VSCROLL message and wrap the call to CListBox::OnVScroll with SetRedraw(FALSE), and SetRedraw(TRUE), followed by a call to RedrawWindow to refresh the content of the listbox. This will give a smooth and flicker free scrolling. Simple!

void CTransparentListBox::OnVScroll(UINT nSBCode, 
                    UINT nPos, CScrollBar* pScrollBar) 
   SetRedraw(FALSE);  //prevent any drawing
   SetRedraw(TRUE);   //restore drawing

   //draw the frame and window content in one shot

The same type of approach has to happen when an item is selected. So we intercept the LBN_SELCHANGE message and force a redraw, since our DrawItem method does nothing.

BOOL CTransparentListBox::OnLbnSelchange()
   return TRUE;

Using the code

To use this class, simply insert a ListBox into your dialog box. Make sure you have set the Owner-draw and Has Strings flags. Attach a variable of type CTransparentListBox to the control and you are ready to go. My CTransparentListBox class also gives you the ability to specify different fonts, colors, and shadows. This will come in handy on backgrounds that are too busy or too dark for the standard colors, and font size.

class CTestDialog : public CDialog
   CTransparentListBox m_ListBox;

void CTestDialog::DoDataExchange(CDataExchange* pDX)
   DDX_Control(pDX, IDC_LIST1, m_ListBox);

BOOL CTransStaticDlg::OnInitDialog()

          RGB(255,255,255),RGB(255,0,0)); //Optional   


Have fun Smile | :)


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


About the Author

Ali Rafiee
United States United States
Ali Rafiee has been developing windows applications using C++ since 1991, and he hasn't looked back since. Ali has been a software development consultant for must of his career, but he has finally settled down and has been working for an educational software company since 2000. While he is not working, he is either learning C#, flying airplanes, playing with his daughter, or answering peoples question on newsgroups, he finds that to be a great learning tool for himself.

Ali is also a Microsoft Visual C++ MVP.

Comments and Discussions

QuestionCan I use it on a combobox dropdown list as well? Pinmembercpw999cn6-Feb-11 17:36 
GeneralThe control doesn't apply to mouse wheel roll Pinmemberzhang xiaoxuan13-Sep-09 18:50 
AnswerRe: The control doesn't apply to mouse wheel roll PinmemberAli Rafiee13-Sep-09 19:15 
QuestionThis method can not work normally in dynamic created listbox? Pinmembere_ilite11-Feb-09 19:10 
GeneralC# Version PinmemberSukhjinder_K24-Jul-07 1:46 
GeneralTransparent Control Pinmemberarijit_datta268-Mar-07 21:42 
I am trying to develop a transparent control.It is having a picture box with a png image and transparent background.I have set the background color of the control to transparent.
Now, it is working if I place it on a panel or a form. But, it is not working as a transparent control if I place it on another control such as list view or month calender.
If some body can help me,I would be thankful to him.
GeneralRe: Transparent Control PinmemberAli Rafiee9-Mar-07 6:48 
GeneralRe: Transparent Control [modified] Pinmemberarijit_datta2610-Mar-07 3:07 
Questionport to C# ? PinmemberBrian Perrin16-Feb-07 10:54 
Generalperfect hscroll [modified] Pinmemberram krishna pattnayak20-Nov-06 20:49 
GeneralThanks,Till Some problem In ListBox Application Pinmemberram krishna pattnayak20-Nov-06 0:37 
GeneralRe: Thanks,Till Some problem In ListBox Application [modified] PinmemberAli Rafiee20-Nov-06 11:33 
GeneralHscrollBar in transparent ListBox Pinmemberram krishna pattnayak17-Nov-06 20:54 
AnswerRe: HscrollBar in transparent ListBox [modified] PinmemberAli Rafiee18-Nov-06 20:56 
Questionhorizontal scroll dont display total text Pinmemberram krishna pattnayak17-Nov-06 1:59 
QuestionRe: horizontal scroll dont display total text PinmemberAli Rafiee17-Nov-06 18:31 
Generalhorizontal scroll Pinmember_kane_16-Nov-06 2:16 
AnswerRe: horizontal scroll [modified] PinmemberAli Rafiee16-Nov-06 6:24 
GeneralAssept Pinmemberdenim666621-Aug-06 20:30 
GeneralRe: Assept PinmemberAli Rafiee22-Aug-06 8:42 
GeneralRe: Assept Pinmemberdenim666623-Aug-06 1:16 
AnswerRe: Assert - Fixed PinmemberAli Rafiee23-Aug-06 5:37 
GeneralRe: Assert - Fixed Pinmemberdenim666623-Aug-06 20:46 
Generalporting to ce PinmemberFernando A. Gómez F.30-Jun-06 9:39 
GeneralEnable and disable items in Listbox PinmemberRosh29-Jun-06 21:33 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.150414.1 | Last Updated 19 Aug 2005
Article Copyright 2005 by Ali Rafiee
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid