Graphical Group Buttons

I have a black main form and I want to add a group button with a gray background. I do so using the BS_GROUPBOX flag. However, the background color turns out to be transparent and not gray as I want it. I tried handling the WM_CTLCOLORSTATIC message, but couldn't make it to work. I tried adding a background from a *.bmp file using the BS_BITMAP flag and BM_SETIMAGE message. The bmp image loads, but not directly on top of the butoon or centered around it. It loads above the button. All I want is to add a gray (or any color for that matter other than black) group buton to my form.

Here is the 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
/*
***************************************************************
*           Media Player via mciSendString function           *
*                 (c) Ed Encarnacion, 2013                    *
***************************************************************
*/

#include <windows.h>

//Globals
HINSTANCE hInst;
/*
 Define the list of all the objects used in this app.
 100 series reserved for static; 200 series reserved for color edit
*/
#define ID_GROUP_BTN   100

//This sub creates all the objects of the app: buttons, labels, sliders etc.
void create_objects(HWND hWnd)
{
	//Group button
	CreateWindowEx(WS_EX_TRANSPARENT , L"button", L"", WS_CHILD | WS_VISIBLE | BS_GROUPBOX | BS_BITMAP, 16, 40, 689, 113, hWnd, (HMENU) ID_GROUP_BTN, NULL, NULL);
	static TCHAR Bitmap_File[] = TEXT ("D:\\VC++Tests\\WindowsAPI\\Player\\Player\\BCKGND.bmp"); //Path to the bitmap with the desired background
	HANDLE hBitmap = LoadImage(0, Bitmap_File, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
	SendMessage(GetDlgItem(hWnd, ID_GROUP_BTN), BM_SETIMAGE, (WPARAM) IMAGE_BITMAP, (LPARAM) hBitmap);


	CloseHandle(hBitmap);
}

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

//Main procedure
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("Ed's Media Player") ;
     HWND         hWnd ;
     MSG          msg ;
     WNDCLASS     wndclass ;

	//The following two lines define the color of the main window and a brush to go with it
	COLORREF windowBackgroundColor = RGB(0x00, 0x00, 0x00); //All 0's equates to black
	HBRUSH windowBackgroundBrush = CreateSolidBrush(windowBackgroundColor); 

	wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
	wndclass.lpfnWndProc   = WndProc ;
    wndclass.cbClsExtra    = 0 ;
    wndclass.cbWndExtra    = 0 ;
    wndclass.hInstance     = hInstance ;
    wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
    wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
    wndclass.hbrBackground = windowBackgroundBrush;
    wndclass.lpszMenuName  = NULL ;
    wndclass.lpszClassName = szAppName ;

	if(!RegisterClass(&wndclass))
	{
		MessageBox (NULL, TEXT ("This program requires Windows NT!"),  szAppName, MB_ICONERROR);
		return 0;
	}
	hInst = hInstance;

	//Main form creation
	DWORD dwStyle=(WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX);
    hWnd = CreateWindow (szAppName,                    // window class name
                          szAppName,                   // window caption
                          dwStyle,                     // window style (originally created with WS_OVERLAPPEDWINDOW, but changed to to dWstyle to prevent re-sizing)
                          CW_USEDEFAULT,               // initial x position
                          CW_USEDEFAULT,               // initial y position
                          728,                         // initial Width size
                          503,                         // initial Height size
                          NULL,                        // parent window handle
                          NULL,                        // window menu handle
                          hInstance,                   // program instance handle
                          NULL) ;                      // creation parameters
     
     ShowWindow (hWnd, iCmdShow) ;
     UpdateWindow (hWnd) ;

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

}


LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	HDC         hdc ;
    PAINTSTRUCT ps ;
    int wmId, wmEvent;
	static HBRUSH hBrushColor;

    switch (message)
	{
		case WM_CREATE:
			create_objects(hWnd);
			break;
		case WM_COMMAND:
			wmId    = LOWORD(wParam);
			wmEvent = HIWORD(wParam);

			break;

		case WM_PAINT:
			hdc = BeginPaint (hWnd, &ps) ;
			//Any paint related code goes here
			EndPaint (hWnd, &ps) ;
			break;
		case WM_CTLCOLORSTATIC:
			if(GetDlgItem(hWnd, ID_GROUP_BTN) == (HWND)lParam)
			{
				if (!hBrushColor)
				{
					hBrushColor = CreateSolidBrush(RGB(0xFF, 0xFF, 0xFF)); // White background is returned
					SetBkMode((HDC)wParam, TRANSPARENT); 
					SetBkColor((HDC)wParam, RGB(0xFF, 0x00, 0x00));
				}
				//return (BOOL) GetStockObject(HOLLOW_BRUSH);
				return (LRESULT)hBrushColor;
			}
			
			break;
		case WM_CTLCOLOREDIT:
			break;
		case WM_DESTROY:
			 PostQuitMessage(0);
			 break;
		default:
			return DefWindowProc (hWnd, message, wParam, lParam);
			break;
	}

	return 0;
}


Thanks,

DominicanJB
Last edited on
Topic archived. No new replies allowed.