Custom Class Callback Function

Hello,

I am creating a child button, and as a child to that button, an SS_OWNERDRAW text label window.

If I create the label as a child of the main window, WM_DRAWITEM is sent and I can paint the label. But if I make the label a child of the button, it is not sent, or at least not sent to "LRESULT CALLBACK WndProc" handler.

Is the message sent to the button window as opposed to the main window, and can I create a second CALLBACK to listen for messages to all the buttons of a specific class? Below is the class and functions that create the buttons and labels.

Thanks,
Nathan.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94

class inputButton {
	HWND hWnd;
	int xpos, ypos, nCmdShow, buttonState, buttonId, labelId;
	LPCSTR buttonText;
public:
	HWND createButton(HWND, int, int, int, int, int, int, LPCSTR);
	void createLabel(HWND, int, int, int, int, LPCSTR);
	void toggleButton(HWND);
};


HWND inputButton::createButton(HWND hWnd, int xpos, int ypos, int nCmdShow, int buttonState, int buttonId,  int labelId, LPCSTR buttonText ) {

	// Create button    
	HWND hButton1;
	hButton1 = CreateWindowEx(
		0,														// _In_     DWORD     dwExStyle											
		_T("BUTTON"),											// _In_opt_ LPCTSTR   lpClassName
		_T("TEXT"),											// _In_opt_ LPCTSTR   lpWindowName
		BS_ICON | WS_VISIBLE | WS_CHILD, // | SS_NOTIFY,		// _In_     DWORD     dwStyle
		xpos,													// _In_     int       x
		ypos,													// _In_     int       y
		100,													// _In_     int       nWidth
		100,													// _In_     int       nHeight
		hWnd,													// _In_opt_ HWND      hWndParent
		(HMENU)buttonId,										// _In_opt_ HMENU     hMenu
		NULL,													// _In_opt_ HINSTANCE hInstance
		NULL													// _In_opt_ LPVOID    lpParam
	);

	// Assign image to button
	Gdiplus::Bitmap* m_pBitmap;
	HICON hicon;
	if (buttonState == 0) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"greySwitch.png"); }
	if (buttonState == 1) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"greenSwitch.png"); }
	if (buttonState == 2) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"redSwitch.png"); }
	if (buttonState == 3) { m_pBitmap = Gdiplus::Bitmap::FromFile(L"yellowSwitch.png"); }
	m_pBitmap->GetHICON(&hicon);


	LRESULT lr = SendMessage(hButton1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon);
	ShowWindow(hButton1, nCmdShow);
	UpdateWindow(hButton1);
	return (hButton1);
}


void inputButton::createLabel(HWND hWnd, int xpos, int ypos, int nCmdShow, int buttonId, LPCSTR buttonText) {
	HWND hStatic;
	int labelId = buttonId + 1000;
	hStatic = CreateWindowEx(
		WS_EX_TRANSPARENT,										// _In_     DWORD     dwExStyle	
		TEXT("STATIC"),											// _In_opt_ LPCTSTR   lpClassName
		TEXT(buttonText),										// _In_opt_ LPCTSTR   lpWindowName
		WS_CHILD | WS_VISIBLE | SS_OWNERDRAW | SS_CENTER,		// _In_     DWORD     dwStyle
		xpos+10,												// _In_     int       x
		ypos+40,												// _In_     int       y
		80,														// _In_     int       nWidth
		20,														// _In_     int       nHeight
		hWnd,													// _In_opt_ HWND      hWndParent
		(HMENU)labelId,											// _In_opt_ HMENU     hMenu
		NULL,													// _In_opt_ HINSTANCE hInstance
		NULL													// _In_opt_ LPVOID    lpParam
	);


	if (hStatic == NULL) {
		logText = "Failed to create label: "  + std::to_string(labelId);
		logTextLPSTR = const_cast<char *>(logText.c_str());
		writeLog(logTextLPSTR);
	}
	else {
		logText = "Created label: " + std::to_string(labelId);
		logTextLPSTR = const_cast<char *>(logText.c_str());
		writeLog(logTextLPSTR);
	}
	ShowWindow(hStatic, nCmdShow);
	UpdateWindow(hStatic);
};
void inputButton::toggleButton(HWND hWnd) {
	// need code here to know which button has been pressed.
};


.
.
.
.
.
	inputButton button01;
	HWND buttonHandle = button01.createButton(hWnd, 50, 50, 1, 0, AUX1_I01_BUTTON_ID, AUX1_I01_LABEL_ID, AUX1_I01_LABEL_TEXT );
	button01.createLabel(buttonHandle, 50, 50, 1, AUX1_I01_BUTTON_ID, AUX1_I01_LABEL_TEXT);
Last edited on
I think the parent has to send the WM_DRAWITEM message to its children.
Try to send the WM_DRAWITEM message form the button to its child(ren).
Is it really necessary to have the button text as a separate window?
Why don't you draw the text when you draw the button?

BTW. To see what messages get send you can use this tool
https://www.codeproject.com/Tips/815076/Yet-Another-Spy
If have not yet tried it, but it looks useful.

VS has tools called Spy++ and Spy++64 which might help.
Hi Thomas, Can I draw a transparent text label over a png button in the same window? I couldn't find a way to do that, but it would sure make it easier!





I haven't tried it, but in your paint function try to use DrawText or TextOut to draw the text.
Topic archived. No new replies allowed.