Sign in to follow this  

Unity Windows Dialog Background Color

This topic is 1104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I recently downloaded the Visual Studio 2013 Community Edition after years of using Express editions.  This means I can finally use the built-in resource editor again for things like Icons and Dialog Boxes!

 

Which is awesome, but now I'm trying to do it and I'm struggling to just set the background color of the dialog box.  I created it, I set "Use MFC" to False, and I assigned the same Class Name that I set in my WNDCLASS structure (that I then pass to RegisterClassW).  This has the desired effect of using the icon I wanted for the dialog!  But no matter what I try, I can't get the thing to draw with a default background color.  It's always white.  From everything I've read, I should just have to set "wc.hbrBackground = (HBRUSH)(COLOR_BACKGROUND + 1);"

 

But it doesn't work.  Nothing I set in wc.hbrBackground seems to have any effect.  The only thing that does anything is when I override WM_ERASEBKGND and do a FillRect with a brush from GetSysColorBrush, but I don't want to really do all that.  I want to just get the default erase mechanism to paint my window the default gray color of a regular dialog window.

 

What am I missing...?

Share this post


Link to post
Share on other sites

Huh.  Well, it is a dialog, so that explains why hbrBackground doesn't do anything.  On that page for WM_CTLCOLORDLG it says:

 

"By default, the DefWindowProc function selects the default system colors for the dialog box."

 

Which is exactly what I want!  My current DlgProc is like this:

LRESULT CALLBACK WindowClass::DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_INITDIALOG:
			break;

		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}

	return FALSE;
}

and I create the (unicode) dialog with one of these:

ZeroMemory(&wc, sizeof(wc));
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.cbWndExtra = DLGWINDOWEXTRA;
wc.hIcon = LoadIconW(hInstance, MAKEINTRESOURCE(IDI_APP));
wc.hCursor = LoadCursorW(NULL, IDC_ARROW);
wc.lpfnWndProc = DefDlgProcW;
wc.lpszClassName = L"WindowClassName";
 
if(!RegisterClassW(&wc))
return 0;
 
HWND hWndApp = CreateDialogParamW(NULL, MAKEINTRESOURCE(IDD_APP), NULL, (DLGPROC)MessageRouter, reinterpret_cast<LPARAM>(this));

Share this post


Link to post
Share on other sites

I don't know about the rest of the code, but the DlgProc in what you posted above seems wrong.

 

http://msdn.microsoft.com/en-us/library/windows/desktop/ms645469(v=vs.85).aspx

 

 

Return value

...

Typically, the dialog box procedure should return TRUE if it processed the message, and FALSE if it did not. If the dialog box procedure returns FALSE, the dialog manager performs the default dialog operation in response to the message.

 

You seem to always return FALSE, which doesn't look good to me.

Share this post


Link to post
Share on other sites

I guess I don't even know what the "default" on Windows 7 looks like... I'm doing this now to get a darker gray than I'd anticipated, but at least it's doing something I'd expect:

		case WM_ERASEBKGND:
			RECT rc;
			GetClientRect(hwnd, &rc);
			FillRect((HDC)wParam, &rc, GetSysColorBrush(COLOR_BTNSHADOW));
			return TRUE;

Share this post


Link to post
Share on other sites
To get it to look "right" on modern versions of Windows you should be looking into being theme-aware and using the theme-aware API.

For example, for drawing backgrounds you should probably use DrawThemeBackground instead of FillRect.

(Note: Error checking omitted)
 
auto theme = GetWindowTheme(hwnd);
DrawThemeBackground(theme, hdc, WP_DIALOG, 0, &rc, nullptr);
Of course, if all you want is the color...
 
auto theme = GetWindowTheme(hwnd);
COLORREF color;
GetThemeColor(theme, WP_DIALOG, 0, TMT_BACKGROUND, &color);
Course, you'll need to make sure visual styles are enabled for your program. And you may want to respond to WM_THEMECHANGED as well.

For a nice example as well as a theme exploring tool - check out this article on CodeProject Edited by SmkViper

Share this post


Link to post
Share on other sites

This topic is 1104 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this