Solved

Displaying Bitmap on Command Button

Posted on 2000-02-22
9
348 Views
Last Modified: 2013-12-03
Hi all,

How do I display a bitmap on the command button? I would like to know the exact code. Thank you.
0
Comment
Question by:Vriz
9 Comments
 
LVL 8

Expert Comment

by:VinExpert
ID: 2545409
0
 

Author Comment

by:Vriz
ID: 2545477
Thanks VinExpert, however, I do not really understand what is stated at the links. Could anyone write down the exact code and where to place them?
0
 
LVL 3

Expert Comment

by:akalmani
ID: 2545525
Hi
   U can also try for CBitmapButton for that..
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2546105
From MS.
class CBMTest1Dlg : public CDialog
{
protected:
      CBitmapButton button1, button2;
public:
      //{{AFX_DATA(CBMTest1Dlg)
            enum { IDD = IDM_TEST_BITMAP_BUTTON1 };
      //}}AFX_DATA
      CBMTest1Dlg();

      BOOL OnInitDialog();
      //{{AFX_MSG(CBMTest1Dlg)
      //}}AFX_MSG
      DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP(CBMTest1Dlg, CDialog)
      //{{AFX_MSG_MAP(CBMTest1Dlg)
      //}}AFX_MSG_MAP
END_MESSAGE_MAP()


CBMTest1Dlg::CBMTest1Dlg()
      : CDialog(CBMTest1Dlg::IDD)
{
      // NOTE: The obsolete MFC V1 CBitmapButton constructor with 3 arguments is
      //  replaced by a call to LoadBitmaps.
      if (!button1.LoadBitmaps(_T("Image1Up"), _T("Image1Down"), _T("Image1Focus")) ||
            !button2.LoadBitmaps(_T("Image2Up"), _T("Image2Down"), _T("Image2Focus")))
      {
            TRACE0("Failed to load bitmaps for buttons\n");
            AfxThrowResourceException();
      }
}

BOOL CBMTest1Dlg::OnInitDialog()
{
      // each dialog control has special bitmaps
      VERIFY(button1.SubclassDlgItem(IDOK, this));
      button1.SizeToContent();
      VERIFY(button2.SubclassDlgItem(IDCANCEL, this));
      button2.SizeToContent();

      return TRUE;
}

/////////////////////////////////////////////////////////////////////////////

Hope helpful
Regards
Wyn
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Accepted Solution

by:
captainkirk earned 20 total points
ID: 2547109
or you can use a regular button and set it's bitmap property and do something like this:

HANDLE hButton   = NULL;
CButton* pButton = NULL;

pButton = (CButton*)GetDlgItem(IDC_YOUR_BUTTON);

hButton = LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDB_YOUR_IMAGE), IMAGE_BITMAP, nDesiredWidth, nDesiredHeight, LR_DEFAULTCOLOR);

pButton->SendMessage(BM_SETIMAGE, IMAGE_BITMAP, MAKELPARAM(hButton, 0));


0
 

Author Comment

by:Vriz
ID: 2547809
Wyn, I don't really understand how to use your code. But thanks anyway. :)

captainkirk, your code works but can you explain what each step is doing? Thanks a lot.
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2547843
1) the GetDlgItem() call returns a pointer to the CWnd object corresponding to your button - you pass in the control ID for your button and you get back the pointer, which you then cast to a CButton pointer.

2)LoadImage() just gets the bitmap or icon for you and returns a handle to it. The MAKEINTRESOURCE macro converts your integer bitmap ID to a resource type compatible with Win32 resource-management functions. You pass in the desired width and height of your image in pixels as well.

3) The SendMessage() call tells the button that it needs to draw the bitmap and supplies the handle to the bitmap you just loaded.

hope that helps...
0
 

Author Comment

by:Vriz
ID: 2547965
Thanks for the explaination.
0
 
LVL 4

Expert Comment

by:captainkirk
ID: 2547975
thanks...

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 113
Font install problem 9 42
fizzArray2 challenge 1 72
matchUp  challenge 9 94
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now