1

I'm trying to make a custom exit button, and I have already created a custom graphic for the button in photoshop. I have the button as defined by this code:

button = CreateWindow(L"BUTTON", L"", WS_VISIBLE | WS_CHILDWINDOW, 400, 4, 480, 24, hWnd, button_id, hInstance, NULL);

I want to put either close.bmp or close.png or close.ico (whichever's easiest) onto the button, however I cannot find a way to do this without MFC. Please help!!!

Thanks

1

4 Answers 4

4

Use the BS_BITMAP or BS_ICON button styles. After creating the button, send it a BM_SETIMAGE message with the handle to your bitmap or icon. To get the handle to your bitmap or icon, use LoadImage.

If your image has an alpha channel (transparency), make sure it's saved as a 32-bit-per-pixel bitmap. You can get by with an icon, but the edges probably won't be as smooth.

Sign up to request clarification or add additional context in comments.

2 Comments

Using Code::Blocks 17.02 with gcc on Windows 7, I had to add a manifest file + I had to reference commctrl.h to make this work (text + image on a button). I found I had more control using BS_OWNERDRAW instead of the method described here.
@Ken_sf: The manifest file was probably to enable "visual styles." They're not enabled by default for backward compatibility. learn.microsoft.com/en-us/windows/win32/controls/…
3

There are two main possibilities:

  1. If you only want to put a bitmap on a regular button, then create the button with the WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON|BS_BITMAP style combination, load the bitmap using LoadBitmap, and put the loaded bitmap on the button by sending its handle to the button in a BM_SETIMAGE message.

  2. If you want to change the appearance of the whole button, then create it with the WS_CHILD|WS_VISIBLE|BS_OWNERDRAW style combination, and handle the WM_DRAWITEM message. Cast the lParam of this message to an LPDRAWITEMSTRUCT, and use its hDC member to draw the button. In your case, preload the bitmap using LoadBitmap, and draw it on the DC using BitBlt or TransparentBlt (source). Alternatively, you can use LoadIcon and DrawIconEx (source). Other members of the DRAWITEMSTRUCT can be used to select the bitmap, that is, the appearance of the button, depending on current action or state.

Comments

1

This is tricky...

Your best option is to use a Owner-Draw button, that is with the style bit BS_OWNERDRAW.

Then, the owner of the button (the parent window) will receive the message WM_DRAWITEM whenever the button is to be redrawn.

The drawback is that the button has to be painted fully, not only the icon, but also the frame, the background, the label if required, the focus rectangle... You have to check the associated DRAWITEMSTRUCT and decide the exact style to be drawn.

To ease the work it can be useful the Windows function DrawFrameControl() that paints several styles of frames, including the standard button frame.

1 Comment

This is a possible route, and it's useful if you have complex drawing requirements, but it's probably more work than using BS_ICON or BS_BITMAP for simply adding an image. Be aware that DrawFrameControl draws old-fashioned button frames (pre-Windows XP themes), which can look out-of-place on a modern application.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.