Home
About
Blog
Media Gallery

Creating a STATIC bitmap button


In winmain:
ReportButtonHwnd = CreateWindowExW(
	0,
	L"STATIC",
	L"",
	SS_BITMAP | WS_VISIBLE | WS_CHILD | SS_NOTIFY,
	620, 390, 118, 48,
	ParentHwnd,
	(HMENU)ID_SOME_BUTTON,
	hInstance,
	0
);
SomeButtonImg = LoadBitmapW(hInstance, MAKEINTRESOURCE(ID_SOMEBUTTON_IMG));

// Set global hand cursor (optional). May be GCLP_ if 64-bit.
SetClassLongPtrW(ReportButtonHwnd, GCL_HCURSOR, (long long)LoadCursorW(0, IDC_HAND));

Here and/or right after any realtime background UI drawing you may do, or it may be overpainted:
SendMessage(ReportButtonHwnd, STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)ReportButtonImg);


SS_NOTIFY above makes sure clicks work via WNDPROC > WM_COMMAND and its HMENU ID:
case WM_COMMAND:
	switch (LOWORD(wParam)) {
		case ID_SOME_BUTTON:
			// button was clicked.
			break;
	}
	break;

STN_CLICKED, STN_DBLCLK, STN_DISABLE and STN_ENABLE all become available as well, under WM_COMMAND.


resource.rc
ID_SOMEBUTTON_IMG BITMAP "somebutton.bmp"


resource.h
// Use any unique numbers you want.
#define ID_SOME_BUTTON 20001
#define ID_SOMEBUTTON_IMG 20002


I've used this to avoid the black border normal buttons get when using the BUTTON class.

The programming differences in creating a STATIC button is using SS_BITMAP instead of BS_BITMAP, STM_SETIMAGE instead of BM_SETIMAGE, and of course the STATIC lpClassName instead of BUTTON.

Original Post: Sep 22nd, '22 16:18 CEST.
Updated: Sep 22nd, '22 17:48 CEST.

Tags: C/C++