1

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.

Abdul
  • 63
  • 1
  • 10

2 Answers2

2

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.
Community
  • 1
  • 1
IInspectable
  • 46,945
  • 8
  • 85
  • 181
  • You mean i can create a normal button using CreateWindow() or CreateWindowEx() Function then i can specify BS_OWNERDRAW style to it, to make program understand there is going to be changes to this button. am i right? – Abdul Aug 07 '15 at 16:30
  • shall i do it this way? – Abdul Aug 08 '15 at 12:22
  • @Wajid: Exactly, call `CreateWindow[Ex]` with window class `WC_BUTTON`, and pass the `BS_OWNDERDRAW` style as well. Code in the linked to answer does just that, except that it doesn't call `CreateWindow[Ex]` directly, but uses a resources script that is parsed and translated into respective API calls by the dialog manager. – IInspectable Aug 09 '15 at 17:39
0

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

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252