Click here to Skip to main content
Licence 
First Posted 31 Oct 2004
Views 153,900
Bookmarked 79 times

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

By | 31 Oct 2004 | Article
Using transparant image button~!

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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUnicode PinmemberSunny12704:43 2 Jan '11  
Questiongif????????????? Pinmemberbatsword15:41 8 Sep '10  
GeneralToolTip does not appear PinmemberMohamadElHajj7:19 27 Mar '09  
QuestionTransparancy doesn't work on CDHtmlDialog ? Pinmemberlambertwm21:10 10 Apr '08  
Generaloh,very very good!!! Pinmemberwsb_822423:29 5 Jun '07  
GeneralRe: oh,very very good!!! Pinmemberlogi0010:29 16 Dec '09  
GeneralRe: oh,very very good!!! Pinmemberbatsword15:35 8 Sep '10  
GeneralZqrTalent PinmemberZaqro8:19 20 Mar '07  
Generala note when using VS2005 Pinmemberrincle23:44 29 May '06  
QuestionHow to hide the white line on the button circle Pinmemberliaohaizhou19:00 11 Apr '06  
AnswerRe: How to hide the white line on the button circle Pinmemberywlee0528:41 12 May '06  
Generalvery good ,but how to use Resource 'bmp Pinmemberxnetec5:38 13 Dec '05  
GeneralRe: very good ,but how to use Resource 'bmp Pinmembervbachmut7:27 10 Feb '06  
GeneralVery good PinmemberEinst23:17 12 Oct '05  
GeneralRe: Very good Pinmemberkbckbc20:13 15 Oct '05  
GeneralRe: Very good PinmemberEinst22:00 30 Oct '05  
GeneralNice, but half done PinmemberAliRafiee10:07 18 Jul '05  
GeneralRe: Nice, but half done Pinmemberkbckbc20:09 15 Oct '05  
GeneralRe: Nice, but half done PinmemberXia Xiongjun13:35 4 Sep '06  
General"Unsupported operation" error PinmemberAnarchi0:52 22 Apr '05  
GeneralRe: "Unsupported operation" error PinmemberMohamed_Bakr23:21 21 May '06  
AnswerRe: "Unsupported operation" error PinmemberDennisMattingly4:51 10 May '07  
GeneralRe: "Unsupported operation" error Pinmemberzorrothefox23:15 28 Nov '07  
GeneralRe: "Unsupported operation" error PinmemberDennisMattingly3:38 29 Nov '07  
Generalit's miracle!! this is only thing to want. Pinmemberreinkorea14:08 31 Oct '04  

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.

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