i'm reading these page: http://msdn.microsoft.com/en-us/library/windows/desktop/bb775951%28v=vs.85%29.aspx
but see the Remarks section:

Remarks

For illustrations of the principal button styles such as BS_CHECKBOX and BS_GROUPBOX, see Button Types.

The appearance of text or an icon or both on a button control depends on the BS_ICON and BS_BITMAP styles, and whether the BM_SETIMAGE message is sent. The possible results are as follows.

BS_ICON or BS_BITMAP set?   BM_SETIMAGE called?     Result
Yes                                 Yes             Show icon only.
No                                  Yes             Show icon and text.
Yes                                 No              Show text only.
No                                  No              Show text only

(the green it's the table)
for we use the BM_SETIMAGE, we must use the the SendMessage() function. now see my code:

LRESULT CALLBACK WndProc (HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    static HWND HandleButton;
    static HICON HandleIcon;

    switch (iMsg)
    {
        case WM_DESTROY:
            DestroyIcon(HandleIcon);
            PostQuitMessage (0) ;
            return 0 ;
        case WM_CREATE:

            HandleButton=CreateWindow (TEXT("button"), TEXT ("hello\t\tHI"),
            WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON | BS_LEFT,
            100, 100,500, 200,hwnd, NULL, (HINSTANCE)GetModuleHandle(NULL), NULL) ;
            HandleIcon=(HICON)LoadImage(NULL,"c:\\acrobat.ico",IMAGE_ICON,LR_DEFAULTSIZE,LR_DEFAULTSIZE,LR_LOADFROMFILE);
            SendMessage(HandleButton, BM_SETIMAGE, IMAGE_ICON,(LPARAM)HandleIcon );
            return 0;
    }
    return DefWindowProc (hwnd, iMsg, wParam, lParam) ;
}

i have sure about the path, unless the 'C' letter is case sensitive.
please tell me what i'm doing wrong with that information\code

Recommended Answers

All 7 Replies

Specify the BS_ICON style when creating the button. Also handy is to check return values from functions like LoadImage to ensure a valid icon handle is returned.

i used that style, but the text isn't showed too :(

This will work. You MUST have visual styles enabled in order to have an icon AND text at the same time..

Of course, you do not want to use the "EnableVisualStyles" function below. Do it with a manifest and a resource file! It is much better.. The below is simply to show you WHY it works.

#include <commctrl.h>

void EnableVisualStyles()
{
    char sys_dir[MAX_PATH] = {0};
    DWORD len = GetSystemDirectory(sys_dir, sizeof(sys_dir) / sizeof(sys_dir[0]));
    if (len < sizeof(sys_dir) / sizeof(sys_dir[0]))
    {
        ACTCTX actCtx =
        {
            sizeof(ACTCTX), ACTCTX_FLAG_RESOURCE_NAME_VALID | ACTCTX_FLAG_SET_PROCESS_DEFAULT |
            ACTCTX_FLAG_ASSEMBLY_DIRECTORY_VALID, "shell32.dll", 0, 0, sys_dir, reinterpret_cast<char*>(0x7C)
        };

        ULONG_PTR ulpActivationCookie = false;
        ActivateActCtx(CreateActCtx(&actCtx), &ulpActivationCookie);
    }
}

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    static HWND HandleButton;
    static HICON HandleIcon;

    switch (message)
    {
        case WM_DESTROY:
            DestroyIcon(HandleIcon);
            PostQuitMessage(0);
            break;

        case WM_CREATE:
            EnableVisualStyles();

            HandleButton = CreateWindow(
                               "BUTTON",
                               "Test",
                               WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON,
                               0, 0,
                               300, 100,
                               hwnd,
                               (HMENU)1,
                               NULL,
                               NULL
                               );
            HandleIcon=(HICON)LoadImage(NULL,"c:/acrobat.ico",IMAGE_ICON,LR_DEFAULTSIZE,LR_DEFAULTSIZE,LR_LOADFROMFILE);
            SendMessage(HandleButton,BM_SETIMAGE,(WPARAM)IMAGE_ICON,(LPARAM)HandleIcon);
            break;

        default:
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}
commented: THANK YOU VERY MUCH +2

thank you. now i need ask:
1 - can i change the icon position?
2 - can i use the windows 2000 button style and continue using the image?
3 - can i use the '\t' text format?

  1. Yes, use BS_TEXT combined with either BS_LEFT, BS_RIGHT, BS_TOP or BS_BOTTOM.
    e.g. with BS_RIGHT, the icon will be on the RHS of the button and the text will also be justified to the right.
  2. No, for that you need to use BS_OWNERDRAW.
  3. No

Edit to above post.
Re 2. It works on Windows 7 without visual styles for buttons, I've not tested on other OS.
I know for things like menus it just ends up ugly and unaligned without visual styles.

thanks for all

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.