Abdul
Abdul

Reputation: 101

how to make a clickable button in c++ win32

anyone can tell me how to use a bitmap as a button, actually i can create a static control and could set a picture to it but the thing is that i don't know how to use it as a button, i am using c++ win32. This is how i create the bitmap

Code:

HWND Profile_Stuff(HWND hWnd, HINSTANCE hInst)
{
    HWND Profile_Pic;

    Profile_Pic = CreateWindow("STATIC", NULL, SS_BITMAP|WS_CHILD|WS_VISIBLE|WS_TABSTOP|WS_BORDER, 5,5,33,33, hWnd, NULL, hInst, NULL);
    HBITMAP hBmp = (HBITMAP)LoadImage(NULL, "camera1.jpg", IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
    if(hBmp == NULL){
        MessageBox(NULL, "Error while loading image", "Error", MB_OK|MB_ICONERROR);
    }
    SendMessage(Profile_Pic, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)hBmp);
    return 0;
}

then i call the function in main window wm_create message handler which creates it successfully, now i don't know to use it as a button, like we have a picture of an advertisement at the bottom of bit torrent application. i am using visual studio c++ with win32 api.

Upvotes: 1

Views: 3621

Answers (2)

Serge Ballesta
Serge Ballesta

Reputation: 149065

In Windows, the windows belong to a class, a the class defines the windows procedure for all windows of that class, meaning how they react to events.

If you create a STATIC window, it will not react to any click and will not be useable as a button.

You could create a custom class, register it along with a custom windows procedure able to mimic a BUTTON. But unless you have very special requirements just create an owner drawn button as shown in @IInspectable's answer

Upvotes: 0

IInspectable
IInspectable

Reputation: 51464

If you want a button control, you should create a button control. The visual representation can be controlled by the application. To do so, specify the BS_OWNERDRAW Button Style. A button control with this style sends a WM_DRAWITEM message to the control parent whenever a visual aspect has changed. The control parent can then render the control as it sees fit.

An introduction to owner-drawn controls is available at Custom Controls. If you wish to retain some parts of the button control (e.g. its border), see Using Visual Styles with Custom and Owner-Drawn Controls for details (or DrawFrameControl if you aren't using Visual Styles).


Fully working sample code for an owner-drawn button control can be found in this answer.

Upvotes: 2

Related Questions