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

Transparant Image Button (BMP, GIF, JPG...)

By , 31 Oct 2004
 

Sample Image - CKbcButton.jpg

Introduction

*I'm not good at English, but if you can understand my story, isn't that enough? :)

When I made this application, especially as dialog based, a lot of buttons were used. Buttons are very important things in my situation. So I decided to make an Image button source.

As you know, there are many sources for image buttons. I think it's useless work making another image button. But those image buttons have a small problem.

intro - a small problem

In this image, button's background hides the parent's background. That's not what I want. So I am going to handle this problem.

Using the code

There are two ways using this button. First of all, include the following files to your project:

  • KbcBmp.h,
  • KbcBmp.cpp,
  • KbcButton.h,
  • KbcButton.cpp,
  • Picture.h,
  • Picture.cpp

Then, let's start~!

  1. Using DDX_Control
    1. Include "KbcButton.h" in header file.
    2. Declare a CKbcButton variable in header file.
    3. Go to dialog resource and draw a button. Check 'Onwer draw' style.
    4. Use DDX_Control like this in CPP file: DDX_Control(pDX, IDC_PLAYBUTTON, m_btnPlay);
    5. Then set button's image by calling SetButtonImage() function.
      // In your header file
      
      #include "KbcButton.h"
      
      class CTestButtonDlg : public CDialog
      {
      protected:
           CKbcButton  m_btnPlay;
      
          ...
          ...
      }
      
      // in you cpp file
      
      void CTestButtonDlg::DoDataExchange(CDataExchange* pDX)
      {
           CDialog::DoDataExchange(pDX);
           //{{AFX_DATA_MAP(CTestButtonDlg)
           DDX_Control(pDX, IDC_PLAYBUTTON, m_btnPlay);
           //}}AFX_DATA_MAP
      }
      
      
      BOOL CTestButtonDlg::OnInitDialog()
      {
           ...
           ...
      
           // set button image..
           m_btnPlay.SetButtonImage("play.bmp",RGB(255,255,255));
           m_btnPlay.SetToolTipText("Play~!");
           m_btnPlay.SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
      
           ...
           ...
      }
  2. Using new operation
    1. Include "KbcButton.h" in header file.
    2. Declare a CKbcButton* variable in header file.
    3. Create CKbcButton instance by using new operator.
    4. Call Create() function.
    5. Then set button's image by calling SetButtonImage() function.
      // In your header file
      
      #include "KbcButton.h"
      
      class CTestButtonDlg : public CDialog
      {
      protected:
           CKbcButton*  m_pButton;
      
      
          ...
          ...
      
      // in you cpp file
      
      BOOL CTestButtonDlg::OnInitDialog()
      {
           ...
           ...
      
           // create button and set button image..
           CRect rtButton;
           CWnd* pWnd = GetDlgItem(IDC_STATIC_SHOW);
           pWnd->GetWindowRect(rtButton);
           ScreenToClient(rtButton);
           m_pButton = new CKbcButton;
           m_pButton->Create("CKbcButton", WS_CHILD|WS_VISIBLE|BS_OWNERDRAW, 
                                 rtButton, this, NEWBUTTON);
           m_pButton->SetButtonImage("show.gif",RGB(0,0,255));
      
           ...
           ...
      }

That's all. Isn't it simple?

Make sure that you set 'Owner draw style' to a button.

Thanks

Thanks to Dr. Yovav Gad (CPicture's author) and The Code Project. :) I learned a lot of things from this site.

History

  • Ver 1.0 (11/01/2004)
    • Used CPicture class supporting JPG, GIF...
    • Fixed cursor problem.

License

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

kbckbc
Web Developer
Korea (Republic Of) Korea (Republic Of)
Member
student of Pohang University of Science and Technology.

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   
GeneralUnicodememberSunny12702 Jan '11 - 4:43 
Won't compile under VS2008 at Unicode char set.
After I added _T to needed strings, started to crash.
Questiongif?????????????memberbatsword8 Sep '10 - 15:41 
how to use about gif?
GeneralToolTip does not appearmemberMohamadElHajj27 Mar '09 - 7:19 
I was testing this code, which is really cool and nice, but found that tooltips do not appear? AM I missing something ?
 
Moh
QuestionTransparancy doesn't work on CDHtmlDialog ?memberlambertwm10 Apr '08 - 21:10 
Hi,
 
I'm trying to use your nice button class, it works fine on standard dialogs (CDialog), but the transparancy stops working if I use the button on a dialog of type CDHtmlDialog.
 
Best way to reproduce the problem is to use the MFC wizard to create a two new MFC dialog projects, one using CDHtmlDialog and another using CDialog.
 
Any suggestions how to fix this?
 
thanks,
Lambert
Generaloh,very very good!!!memberwsb_82245 Jun '07 - 23:29 
oh,very very good!!!
haha
henhao ,xiexie!
ni de da da de hao!
GeneralRe: oh,very very good!!!memberlogi00116 Dec '09 - 0:29 
funny
GeneralRe: oh,very very good!!!memberbatsword8 Sep '10 - 15:35 
抓住了,围观。。。 Smile | :)
GeneralZqrTalentmemberZaqro20 Mar '07 - 8:19 
I think you should see WowButtons project & use GdiDrawStream function to hide other not needed parts of any bitmap images!!!
 
Zqr

Generala note when using VS2005memberrincle29 May '06 - 23:44 
nice job,Indeed I learn a lot form it. But I found that in Visual Studio 2005 the "Owner Draw" Properties of the Button should be set to "True", or else the button wouldn't display any pic. Anyway, it's a good work! 8)
 
rincle
QuestionHow to hide the white line on the button circlememberliaohaizhou11 Apr '06 - 19:00 
this sample is very good,
but when i use this class in my project i find a problem,on the button cirle have white line. i don't known how to hide those white line.
my english not vary good,do you understand my question.
please help me!
 
fdafda
 
-- modified at 1:01 Wednesday 12th April, 2006

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 31 Oct 2004
Article Copyright 2004 by kbckbc
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid