Transparent Child Windows

As the title suggests, i am trying to create a transparent background child window as a text label over a png button. So far I have this:

vesav.com.au/logo/demo2.jpg

from this code:

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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  #include <windows.h>
#include <tchar.h>
#include <objidl.h>
#include "stdafx.h"
#include "gdiplus.h"
#include <iostream>
#include <string>

Gdiplus::Bitmap* m_pBitmap;

#define ID_BTNHI 0
const char windowClassName[] = "myWindowClass";

HWND button;


class inputButton {
	HWND hWnd;
	int xpos, ypos;
public:
	void createButton(HWND,int, int, int);
};

void inputButton::createButton(HWND hWnd, int xpos, int ypos, int nCmdShow) {

	// Create button    
	//HWND hButton1 = CreateWindow(_T("BUTTON"), _T("Test button"), BS_ICON | WS_VISIBLE | WS_CHILD, xpos, ypos, 100, 100, hWnd, (HMENU)1, NULL, NULL);


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




	ShowWindow(hButton1, nCmdShow);
	UpdateWindow(hButton1);


	// Assign image to button
	Gdiplus::Bitmap* m_pBitmap;
	HICON hicon;
	m_pBitmap = Gdiplus::Bitmap::FromFile(L"greySwitch.png");
	m_pBitmap->GetHICON(&hicon);


	LRESULT lr = SendMessage(hButton1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon);


	DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
	//DWORD Flags1 =  WS_EX_TRANSPARENT;
	DWORD Flags2 = WS_POPUP;


	HWND hStatic;


	hStatic = CreateWindowEx(
		WS_EX_TRANSPARENT,					// _In_     DWORD     dwExStyle	
		TEXT("Static"),						// _In_opt_ LPCTSTR   lpClassName
		TEXT("text"),						// _In_opt_ LPCTSTR   lpWindowName
		WS_CHILD | WS_VISIBLE,				// _In_     DWORD     dwStyle
		10,									// _In_     int       x
		40,									// _In_     int       y
		80,									// _In_     int       nWidth
		20,									// _In_     int       nHeight
		hButton1,							// _In_opt_ HWND      hWndParent
		NULL,								// _In_opt_ HMENU     hMenu
		NULL,								// _In_opt_ HINSTANCE hInstance
		NULL								// _In_opt_ LPVOID    lpParam
	);

	
	/*
	HRGN GGG = CreateRectRgn(0, 0, 50, 50);
	InvertRgn(GetDC(hStatic), GGG);
	SetWindowRgn(hStatic, GGG, false);

	COLORREF RRR = RGB(255, 0, 0);
	SetLayeredWindowAttributes(hStatic, RRR, (BYTE)0, LWA_COLORKEY);


	ShowWindow(hStatic, nCmdShow);
	UpdateWindow(hStatic);

	DeleteObject(GGG);

	*/

	ShowWindow(hStatic, nCmdShow);
	UpdateWindow(hStatic);

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {

	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rect;

	switch (msg) {

		case WM_CLOSE:
			DestroyWindow(hWnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		case WM_CTLCOLORSTATIC: {
			::MessageBox(NULL, "WM_CTLCOLORSTATIC", "", MB_OK); 

			}
			break;
		case WM_CREATE: {

			}
			break;
		case WM_ERASEBKGND: {
			/*
			GetClientRect(hWnd, &rect);
			FillRect((HDC)wParam, &rect, CreateSolidBrush(RGB(255, 0, 0)));
			*/
		}
		break;
  
		default:
			return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	FreeConsole(); //Hides console


				   // Initialize GDI+
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

	

	WNDCLASSEX wc;
	HWND hWnd;
	MSG Msg;

	// Register window class for main window    
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = windowClassName;
	

	if (!RegisterClassEx(&wc)) {
		::MessageBox(NULL, "Window Registration Failed!", "", MB_OK);
		return 0;
	}

	// Create main window    
	hWnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		windowClassName,
		"C++ Win32 Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		600, 450,
		NULL, NULL, hInstance, NULL
	);

	if (hWnd == NULL) {
		::MessageBox(NULL, "Window Creation Failed!", "", MB_OK);
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);


	inputButton button1;
	button1.createButton(hWnd, 50, 50,1);
	inputButton button2;
	button2.createButton(hWnd, 150, 50, 1);
	inputButton button3;
	button3.createButton(hWnd, 250, 50, 1);
	inputButton button4;
	button3.createButton(hWnd, 350, 50, 1);





	while (GetMessage(&Msg, NULL, 0, 0) > 0) {
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}


Any ideas?
Try using COLOREF RRR = RGB(255,0,255)
Hi rjphares, I actually commented out that entire section because it didn't seem to do much except turn the main window pink...
I think magenta was/is the "standard" color for transparency. If that doesn't work I can try loading the program
It seems to make the main window pink, not the child text windows. In fact the case WM_CTLCOLORSTATIC: and case WM_ERASEBKGND: messages don't seem to be sent when creating those child windows. Do I need another Callback setion for those?

As I said, I'm very new to this, and am quite confused atm lol. Another question, how do I know when one of those buttons are pressed, and which specific button is pressed?

I think the text labels are children of the button windows, which are themselves children of the main window... Is this correct?

And do I need to register the button classes and text labels somehow, as I did the main window?

Thanks again for your help.
Last edited on
is this what you are wanting?
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "stdafx.h"

#include <windows.h>
#include <tchar.h>
#include <objidl.h>
#include "gdiplus.h"
#include <iostream>
#include <string>

Gdiplus::Bitmap* m_pBitmap;

#define ID_BTNHI 0
const char windowClassName[] = "myWindowClass";

HWND button;


class inputButton {
	HWND hWnd;
	int xpos, ypos;
public:
	void createButton(HWND, int, int, int);
};

void inputButton::createButton(HWND hWnd, int xpos, int ypos, int nCmdShow) {

	// Create button    
	//HWND hButton1 = CreateWindow(_T("BUTTON"), _T("Test button"), BS_ICON | WS_VISIBLE | WS_CHILD, xpos, ypos, 100, 100, hWnd, (HMENU)1, NULL, NULL);


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




	ShowWindow(hButton1, nCmdShow);
	UpdateWindow(hButton1);


	// Assign image to button
	Gdiplus::Bitmap* m_pBitmap;
	HICON hicon;
	m_pBitmap = Gdiplus::Bitmap::FromFile(L"greySwitch.png");
	m_pBitmap->GetHICON(&hicon);


	LRESULT lr = SendMessage(hButton1, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hicon);


	DWORD Flags1 = WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_NOACTIVATE | WS_EX_TOPMOST | WS_EX_TRANSPARENT;
	//DWORD Flags1 =  WS_EX_TRANSPARENT;
	DWORD Flags2 = WS_POPUP;


	//HWND hStatic;


	//hStatic = CreateWindowEx(
	//	WS_EX_TRANSPARENT,					// _In_     DWORD     dwExStyle	
	//	TEXT("Static"),						// _In_opt_ LPCTSTR   lpClassName
	//	TEXT("text"),						// _In_opt_ LPCTSTR   lpWindowName
	//	WS_CHILD | WS_VISIBLE,				// _In_     DWORD     dwStyle
	//	10,									// _In_     int       x
	//	40,									// _In_     int       y
	//	80,									// _In_     int       nWidth
	//	20,									// _In_     int       nHeight
	//	hButton1,							// _In_opt_ HWND      hWndParent
	//	NULL,								// _In_opt_ HMENU     hMenu
	//	NULL,								// _In_opt_ HINSTANCE hInstance
	//	NULL								// _In_opt_ LPVOID    lpParam
	//);


	/*
	HRGN GGG = CreateRectRgn(0, 0, 50, 50);
	InvertRgn(GetDC(hStatic), GGG);
	SetWindowRgn(hStatic, GGG, false);

	COLORREF RRR = RGB(255, 0, 0);
	SetLayeredWindowAttributes(hStatic, RRR, (BYTE)0, LWA_COLORKEY);


	ShowWindow(hStatic, nCmdShow);
	UpdateWindow(hStatic);

	DeleteObject(GGG);

	*/

	//ShowWindow(hStatic, nCmdShow);
	//UpdateWindow(hStatic);

}

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {

	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;
	RECT rect;

	switch (msg) {

	case WM_CLOSE:
		DestroyWindow(hWnd);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	case WM_CTLCOLORSTATIC: {
		::MessageBox(NULL, (LPCWSTR)"WM_CTLCOLORSTATIC", (LPCWSTR)"", MB_OK);

	}
							break;
	case WM_CREATE: {

	}
					break;
	case WM_ERASEBKGND: {
		/*
		GetClientRect(hWnd, &rect);
		FillRect((HDC)wParam, &rect, CreateSolidBrush(RGB(255, 0, 0)));
		*/
	}
						break;

	default:
		return DefWindowProc(hWnd, msg, wParam, lParam);
	}
	return 0;
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	FreeConsole(); //Hides console


				   // Initialize GDI+
	Gdiplus::GdiplusStartupInput gdiplusStartupInput;
	ULONG_PTR gdiplusToken;
	GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);



	WNDCLASSEX wc;
	HWND hWnd;
	MSG Msg;

	// Register window class for main window    
	wc.cbSize = sizeof(WNDCLASSEX);
	wc.style = 0;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = (LPCWSTR)windowClassName;


	if (!RegisterClassEx(&wc)) {
		::MessageBox(NULL, (LPCWSTR)"Window Registration Failed!", (LPCWSTR) "", MB_OK);
		return 0;
	}

	// Create main window    
	hWnd = CreateWindowEx(
		WS_EX_CLIENTEDGE,
		(LPCWSTR)windowClassName,
		(LPCWSTR)"C++ Win32 Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, CW_USEDEFAULT,
		600, 450,
		NULL, NULL, hInstance, NULL
	);

	if (hWnd == NULL) {
		::MessageBox(NULL, (LPCWSTR)"Window Creation Failed!", (LPCWSTR)"", MB_OK);
	}

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);


	inputButton button1;
	button1.createButton(hWnd, 50, 50, 1);
	inputButton button2;
	button2.createButton(hWnd, 150, 50, 1);
	inputButton button3;
	button3.createButton(hWnd, 250, 50, 1);
	inputButton button4;
	button3.createButton(hWnd, 350, 50, 1);





	while (GetMessage(&Msg, NULL, 0, 0) > 0) {
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}
	return Msg.wParam;
}


Also, if not - try moving the forum post to Windows Programming. And there is a freenode channel #winapi(not related to #cplusplus.com) if you have irc. I am also new so getting help from someone with advanced knowledge would be better.
Topic archived. No new replies allowed.