Reputation: 1
I'm writing a program and I have a problem I hope you guys could help me with.
Like you can see in my code downbelow, I have the WinMain WINAPI which creates the mainframe (parent window). When I try to change the background color in the wc.hbrBackground variable nothing is happening. I really don't understand why. I have tried almost everything. No matter which color I pick it stays as a dark gray background - and no errors occurs. I suspects the "marking" of the clientarea as the problem but I can't figure it out.
Can you guys help me how to change it?
An excerpt of my code in main.cpp:
HWND CreateNewMDIChild(HWND hMDIClient)
{
MDICREATESTRUCT mcs;
HWND hChild;
mcs.szTitle = "[Untitled]";
mcs.szClass = g_szChildClassName;
mcs.hOwner = GetModuleHandle(NULL);
mcs.x = mcs.cx = CW_USEDEFAULT;
mcs.y = mcs.cy = CW_USEDEFAULT;
mcs.style = MDIS_ALLCHILDSTYLES;
hChild = (HWND)SendMessage(hMDIClient, WM_MDICREATE, 0, (LONG)&mcs);
if(!hChild)
{
MessageBox(hMDIClient, "MDI Child creation failed.", "Oh Oh...",
MB_ICONEXCLAMATION | MB_OK);
}
return hChild;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
HWND hStatus;
int statwidths[] = {100, -1};
CLIENTCREATESTRUCT ccs;
// Create MDI Client
// Find window menu where children will be listed
ccs.hWindowMenu = GetSubMenu(GetMenu(hwnd), 5);
ccs.idFirstChild = ID_MDI_FIRSTCHILD;
g_hMDIClient = CreateWindowEx(WS_EX_CLIENTEDGE, "mdiclient", NULL,
WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL | WS_VISIBLE,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
hwnd, (HMENU)IDC_MAIN_MDI, GetModuleHandle(NULL), (LPVOID)&ccs);
if(g_hMDIClient == NULL)
MessageBox(hwnd, "Could not create MDI client.", "Error", MB_OK | MB_ICONERROR);
// Create Toolbar
HWND hTool;
TBBUTTON tbb[5];
TBADDBITMAP tbab;
hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE, 0, 0, 0, 0,
hwnd, (HMENU)IDC_MAIN_TOOL, GetModuleHandle(NULL), NULL);
if(hTool == NULL)
MessageBox(hwnd, "Could not create tool bar.", "Error", MB_OK | MB_ICONERROR);
// Send the TB_BUTTONSTRUCTSIZE message, which is required for
// backward compatibility.
SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
tbab.hInst = HINST_COMMCTRL;
tbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);
ZeroMemory(tbb, sizeof(tbb));
tbb[0].iBitmap = STD_FILENEW;
tbb[0].fsState = TBSTATE_ENABLED;
tbb[0].fsStyle = TBSTYLE_BUTTON;
tbb[0].idCommand = ID_FILE_NEW;
SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);
// Create Status bar
hStatus = CreateWindowEx(0, STATUSCLASSNAME, NULL,
WS_CHILD | WS_VISIBLE | SBARS_SIZEGRIP, 0, 0, 0, 0,
hwnd, (HMENU)IDC_MAIN_STATUS, GetModuleHandle(NULL), NULL);
SendMessage(hStatus, SB_SETPARTS, sizeof(statwidths)/sizeof(int), (LPARAM)statwidths);
SendMessage(hStatus, SB_SETTEXT, 0, (LPARAM)"Version 1.0.0 beta");
}
break;
case WM_SIZE:
{
HWND hTool;
RECT rcTool;
int iToolHeight;
HWND hStatus;
RECT rcStatus;
int iStatusHeight;
HWND hMDI;
int iMDIHeight;
RECT rcClient;
// Size toolbar and get height
hTool = GetDlgItem(hwnd, IDC_MAIN_TOOL);
SendMessage(hTool, TB_AUTOSIZE, 0, 0);
GetWindowRect(hTool, &rcTool);
iToolHeight = rcTool.bottom - rcTool.top;
// Size status bar and get height
hStatus = GetDlgItem(hwnd, IDC_MAIN_STATUS);
SendMessage(hStatus, WM_SIZE, 0, 0);
GetWindowRect(hStatus, &rcStatus);
iStatusHeight = rcStatus.bottom - rcStatus.top;
// Calculate remaining height and size edit
GetClientRect(hwnd, &rcClient);
iMDIHeight = rcClient.bottom - iToolHeight - iStatusHeight;
hMDI = GetDlgItem(hwnd, IDC_MAIN_MDI);
SetWindowPos(hMDI, NULL, 0, iToolHeight, rcClient.right, iMDIHeight, SWP_NOZORDER);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_COMMAND:
// Some code
default:
return DefFrameProc(hwnd, g_hMDIClient, msg, wParam, lParam);
}
return 0;
}
LRESULT CALLBACK MDIChildWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP | WS_GROUP, 270, 40, 80, 20, hwnd, (HMENU) ID_EB_1, NULL, NULL);
CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, 270, 70, 80, 20, hwnd, (HMENU) ID_EB_2, NULL, NULL);
CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, 430, 70, 80, 20, hwnd, (HMENU) ID_EB_3, NULL, NULL);
CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, 270, 135, 80, 20, hwnd, (HMENU) ID_EB_4, NULL, NULL);
CreateWindow("EDIT", "", WS_VISIBLE | WS_CHILD | WS_BORDER | WS_TABSTOP, 430, 135, 80, 20, hwnd, (HMENU) ID_EB_5, NULL, NULL);
}
break;
case WM_COMMAND:
// Some code
break;
return DefMDIChildProc(hwnd, msg, wParam, lParam);
default:
return DefMDIChildProc(hwnd, msg, wParam, lParam);
}
return 0;
}
BOOL SetUpMDIChildWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = MDIChildWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szChildClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(0, "Could Not Register Child Window", "Oh Oh...",
MB_ICONEXCLAMATION | MB_OK);
return FALSE;
}
else
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
InitCommonControls();
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.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MAINMENU);
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
if(!SetUpMDIChildWindowClass(hInstance))
return 0;
hwnd = CreateWindowEx(
0,
g_szClassName,
"Conosoft Enomi - Version 1.0.0 (beta)",
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, 1100, 700,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
g_hMainWindow = hwnd;
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
if (!TranslateMDISysAccel(g_hMDIClient, &Msg))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
return Msg.wParam;
}
I really hope you guys can help me, thank you! :)
Upvotes: 0
Views: 673
Reputation: 3214
Here is what worked for me. There are a couple of places to look for when changing the background color of an MDI app.
+----------------------------+
| |
+----------------------------+
| |
| +-------------+ |
| | | |
| | +---------------+ |
| +-----| | |
| | (c) | |
| +---------------+ |
| (f) |
+----------------------------+
(c) is the child window's background, which is defined as wc.hbrBackground
in SetUpMDIChildWindowClass()
in your example.
BOOL SetUpMDIChildWindowClass(HINSTANCE hInstance)
{
...
wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
You can use
COLOR_... + 1
as you did. There are a few predefined values in WinUser.h
.GetStockObject(BLACK_BRUSH)
if the stock brush suits your needs.NULL
if you want to handle WM_ERASEBKGND
yourself, and perhaps draw a fancy background image.CreateSolidBrush(...)
if you want to create a brush with your choice of color. Make sure to delete it before closing the app.Here is an example for the last case.
HBRUSH BackgroundBrush;
int WINAPI WinMain(...)
{
...
hBackgroundBrush = CreateSolidBrush(BACKGROUND_COLOR);
LRESULT CALLBACK WndProc(...)
{
...
case WM_DESTROY:
DeleteObject(hBackgroundBrush);
(f) is the background of the frame window. You won't be able to see it if the child window is maximized. In your case, it's defined as wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
in WinMain()
.
Note that if the child window has a grand-child window that covers the entire client area of the child window, then you need to let the grand-child window handle the background. For example, my child window has an Edit control that covers the whole child window.
LRESULT CALLBACK ChildWndProc(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam) {
...
case WM_CTLCOLOREDIT:
SetTextColor((HDC)wParam, TEXT_COLOR);
SetBkColor((HDC)wParam, BACKGROUND_COLOR);
return (LRESULT)hBackgroundBrush;
Upvotes: 0
Reputation: 613013
You cannot effect this change with hbrBackground
since the client window class is pre-defined by the system. When you created the client window you passed "mdiclient"
as the window class name. You did not register that class, it is pre-defined. Incidentally, you are expected to use the macro MDICLIENT
to specify that class name.
Instead you need to subclass the client window and handle WM_ERASEBKGND
to paint the background as you desire.
Upvotes: 2