Click here to Skip to main content
13,292,997 members (100,879 online)
Click here to Skip to main content
Add your own
alternative version


63 bookmarked
Posted 19 Nov 2003

An easy way to create transparent button

, 19 Nov 2003
Rate this:
Please Sign up or sign in to vote.
An article on owner draw button


I wanted to draw a button which is transparent to its back ground image and hence had gone through several articles about button styles - transparent buttons. They were all very tedious tasks of making a button transparent - erase button back ground, draw picture etc. I found an easy method. The idea behind this is, draw a bitmap picture on the dialog box. Draw the same bitmap on the button from the location LeftTop of the button. Button is not visible like a button unless mouse cursor is moved on it. Therefore I'll add two icons on the button.

//I have implemented two important functions - 
//SetTransparent() and PaintBG(CDC *pDC, CRect rc)
//SetTranparent function loads bitmap from the resource and 
//creates an handle to CDC - m_hHdc. 

void CBut::SetTransparent()
    HBITMAP m_hBmp = ::LoadBitmap(::AfxGetInstanceHandle(), 
    m_hHdc = ::CreateCompatibleDC(NULL);
    ::SelectObject(m_hHdc, m_hBmp);

//PaintBG fnction is called in DrawItem function which draws 
//bitmap on the button from the location left-top of the button.

void CBut::PaintBG(CDC *pDC, CRect rc)
    CRect rect;

        ::BitBlt(pDC->m_hDC, rc.left,, rect.Width(), 
            rect.Height(), m_hHdc, rect.left,, SRCCOPY);

How to use the source files

Draw bitmap on the dialog box in CPaint function of your CDialog derived class. Check the 'OwnerDraw' property of the button. Create a variable of type CButtonStyle in dialog class. Don't forget to include ButtonStyle.h file name in the same class.

void CButtonStyleDlg::OnPaint() 
    if (IsIconic())
        CPaintDC dc(this); // device context for painting
        CPaintDC dc(this);
        CRect rect;
        BITMAP bmp;
        HBITMAP hBmp = ::LoadBitmap(::AfxGetInstanceHandle(), 
        ::GetObject(hBmp, sizeof(bmp), &bmp);
        HDC hDC = ::CreateCompatibleDC(NULL);
        SelectObject(hDC, hBmp);

        //Note: Do not use StretchBlt function.
        ::BitBlt(dc.m_hDC, 0, 0, rect.Width(), rect.Height(), hDC, 0, 0, 

//Set button properties in OnInitDialog() function of CDialog derived class

BOOL CButtonStyleDlg::OnInitDialog()

    // Set the icon for this dialog.  The framework does this automatically
    //  when the application's main window is not a dialog
    SetIcon(m_hIcon, TRUE);            // Set big icon
    SetIcon(m_hIcon, FALSE);        // Set small icon
    // TODO: Add extra initialization here
    m_btnButton1.SetTextColor(RGB(0,220,0), RGB(0,255,0));
    m_btnButton1.SetIcons(IDI_ICON4, IDI_ICON4);
    m_btnButton1.FontStyle("MS Sans Serif");

    m_btnOK.SetTextColor(RGB(0,220, 0), RGB(0,255,0));
    m_btnOK.SetIcons(IDI_ICON2, IDI_ICON2);
    m_btnOK.FontStyle("MS Sans Serif");

    m_btnExit.SetTextColor(RGB(0,220,0), RGB(0,255,0));
    m_btnExit.SetIcons(IDI_ICON1, IDI_ICON1);
    m_btnExit.FontStyle("MS Sans Serif");

    m_btnClick.SetTextColor(RGB(0,220,0), RGB(0,255,0));
    m_btnClick.SetIcons(IDI_ICON3, IDI_ICON5);
    m_btnClick.FontStyle("MS Sans Serif");
    return TRUE;  // return TRUE  unless you set the focus to a control


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
India India
No Biography provided

You may also be interested in...


Comments and Discussions

QuestionThanks Pin
luizalves197214-Mar-12 9:27
memberluizalves197214-Mar-12 9:27 
Questionhow to use this ButtonStyle when i need to SetTransparent for more than one kind of background Pin
chaiein6-Mar-12 20:49
memberchaiein6-Mar-12 20:49 
GeneralResource leak Pin
advatronix21-Dec-05 11:58
memberadvatronix21-Dec-05 11:58 
GeneralRe: Resource leak Pin
hugheslin29-Mar-07 22:23
memberhugheslin29-Mar-07 22:23 
GeneralRe: Resource leak Pin
hugheslin29-Mar-07 23:44
memberhugheslin29-Mar-07 23:44 
GeneralDidn't handle Button Move Pin
liuhoihing27-Nov-03 20:22
memberliuhoihing27-Nov-03 20:22 
GeneralWhy not... Pin
funny thing21-Nov-03 17:13
memberfunny thing21-Nov-03 17:13 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171207.1 | Last Updated 20 Nov 2003
Article Copyright 2003 by bhushan_at
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid