Instructions
- Download the source code in the Download section and extract ImageButton.h and ImageButton.c in your project's folder.
- Include files ImageButton.h and ImageButton.cpp to your project. At the beginning of the file in which you want to use these ImageButtons, you must add #include "ImageButton.h".
- Link to comctl32.lib or libcomctl32.a. If you're using an IDE with MinGW/GCC as compiler (Dev-c++, CodeBlocks, etc.), add -lcomctl32 to the project linker options. If you're using Visual C++, add "#pragma comment(lib, "comctl32.lib")" to your project's main source file. (Thanks Ovidiu)
- When you create the button (at runtime or within the .rc file) you should specify BS_OWNERDRAW style. If you don't, the button will be destroyed (just to make sure you spot it).
E.g., run-time:
aButton = CreateWindowEx(0, "BUTTON", NULL, WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, 270,75,110,23,
hwnd, NULL, GetModuleHandle(NULL), NULL);
Or, within the .rc file:
PUSHBUTTON "", IDC_BUTTON1, 23,25,60,14, BS_OWNERDRAW
- Then, before the button is displayed (ie, in your WM_INITDIALOG), use one of the function ImageButton_Create(...) that have these prototypes:
ImageButton_Create(HWND hButton);
ImageButton_Create(HWND hwndParent, UINT ctrlID);
The first function should be used when you create the button at runtime (you should have the handle to the button), while in the second one you can use the handle to the parent window of the button and the ID of the button (usually, when the button comes from .rc file).
Some examples:
ImageButton_Create(aButton);
ImageButton_Create(hwnd, IDC_BUTTON1);
- If you want to add images, use one of the function listed in the Documentation. These are ImageButton_SetBitmap for bitmaps and ImageButton_SetIcon for icons. Examples:
ImageButton_SetBitmap(aButton, IDB_BMP1, IBO_GRAY,IBO_LIGHTEN);
ImageButton_SetIcon(GetDlgItem(hwnd, IDC_BUTTON1), IDI_ICON1, 0,0,32,32);
HINSTANCE resModule = GetModuleHandle("res.dll");
ImageButton_SetBitmap(GetDlgItem(hwnd, IDC_BUTTON1), resModule, IDB_BMP1, 0,0);
If you don't want any image (that is, you don't use any of the functions named before), or if an error occur, the button will be displayed with no images.
- If you are running Windows XP and you want that pretty looking buttons typical of XP Visual Styles (above all, if you added a manifest to load the common controls library version 6), before creating any button just call ImageButton_EnableXPThemes(). If you don't call this function, the buttons will be drawn in Windows 98/ME style, even if a manifest is included. Of course, this function will be useless if you are running Windows 98 or Windows ME.
This page was last modified
40 months, 4 days and 20 hours ago