How to make an icon button in C++

11,477

Solution 1

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.

Solution 2

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 .

Solution 3

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()

Share:
11,477
jean9
Author by

jean9

My name is Steven and i am trying to learn all i can about programming exspecially in C++

Updated on June 04, 2022

Comments

  • jean9
    jean9 almost 2 years

    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