2

I know how to draw a button in C++ but how would i make an icon on it can someone post source or give reference please? by SendMessage() or if not that way just please paste Please need easier anwsers without so many files im new a bit

3
  • please specify the GUI API you are using. Win32? MFC? Qt? I assume win32 from your SendMessage comment. But please clarify.
    – Evan Teran
    Commented Mar 11, 2009 at 3:35
  • I suppose we can assume that you're using MFC from your accepted answer :-) It'd be great to say thank you.
    – David
    Commented May 8, 2009 at 9:04
  • Lost punctuation along the way? Commented May 15, 2014 at 15:26

3 Answers 3

6

Since you're new, you may also wish to consult the MSDN Library. You can find information on Button Styles (see, specifically, the BS ICON and BS BITMAP styles) and the BM_SETIMAGE message .

2

If you use MFC then I would recommend you to use the following CButton method SetIcon:

CButton myButton;

// Create an icon button.
myButton.Create(_T("My button"), WS_CHILD|WS_VISIBLE|BS_ICON, 
   CRect(10,10,60,50), pParentWnd, 1);

// Set the icon of the button to be the system question mark icon.
myButton.SetIcon( ::LoadIcon(NULL, IDI_QUESTION) ); 

This works very well.

2

send BM_SETIMAGE message, and pass loaded image handle to lParam.

button1 = CreateWindowW(L"BUTTON", L"&Button", WS_VISIBLE | WS_CHILD | WS_TABSTOP | BS_BITMAP, 20, 50, 80, 25, hwnd, (HMENU) 600, NULL, NULL);

hImg = LoadImageW(NULL, L"test123.bmp", IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_DEFAULTSIZE | LR_LOADFROMFILE);
SendMessageW(button1, BM_SETIMAGE, IMAGE_BITMAP, (LPARAM) hImg);

P.S: you need to use BS_BITMAP flag when CreateWindow()

Not the answer you're looking for? Browse other questions tagged or ask your own question.