Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MFC MainFrame background color

Status
Not open for further replies.

Vachaun22

Programmer
Oct 7, 2003
171
0
0
US
I saw there was a post about this already, but it didn't help me. I'm trying to figure out how to change the WNDCLASS structure of an MFC application so that CMainFrame's background color can be altered. I'm not able to locate how to do this. Any help would be appreciated.

Also, I'm a newb to MFC programming and windows interface. Thanks in advance.
 


One way to change the background color:

Create a member variable in your .h file:
Code:
   CBrush m_windowBackground;

In the ::OnInitDialog() function add the lines:

Code:
   m_windowBackground.CreateSolidBrush(RGB(0x00, 0x00, 0xff));  // Blue brush

Then in the ::OnCtlColor() function add the lines:

Code:
   if (pWnd->GetDlgCtrlID() == NULL)
      hbr = m_windowBackground;

I hope this helps.

You probably could also change the HBRUSH parameter directly in the WNDCLASS structure in the start up function of your program.

HyperEngineer
If it ain't broke, it probably needs improvement.
 
HyperEngineer,

I partly understand what you are saying, and I'll definitely attempt using an HBRUSH. I've tried changing the hbrBackground member of WNDCLASS in the startup function, but I either get and Assertion, or it does nothing at all. So what I'm figuring is I'm either too dumb to register the new class and use it, or I'm just doing it completely wrong.

When I try to alter the WNDCLASS structure, I then tried the AfxRegisterClass() function and I believe it's the AfxRegisterWndClass() function. The latter of the two allows you to supply directly as a parameter the hbrBackground, but alas, it does nothing for me.

If you could give me an example of altering the main applications WNDCLASS that would be the preferrable way, since this should cut down on the amount of executable code. Thanks again for your help.
 
I would have to see your code where you are trying to change this parameter. All this is done behind the scenes for me. That's why I have to use the Dialog::OnCtlColor() function.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Well, the first place I tried to do this was in

BOOL CMainFrame::preCreateWindow(CREATESTRUCT& cs)

I simply got the WNDCLASS structure from the application, tried to alter it, then attempted to reregister the class using the AfxRegisterClass functions. This I believe simply gave me an assertion.

Then I decided to try to put it in

BOOL cMyApp::InitInstance()

In this case, I created a WNDCLASS variable, and set all the members as you would in a straight non MFC application. Then continued on to register the class with the AfxRegisterClass( &wndclass ) function. This didn't give me any problems, as well as it didn't do anything either.

I would assume altering the class hbrBackground property would be the better way to change the background color. And in a tutorial on MFC that I followed, it showed creating a WNDCLASS and using it in CMainFrame::CMainFrame constructor. And this works perfectly, but the application MFC code was handwritting, not generated by the compiler.

But I think I tried to create this code in MFC generated files, and I get an assertion. Very confusing. The code used is fairly straitforward as follows:

CMainFrame::CMainFrame()
{
// Declare a window class variable
WNDCLASS WndCls;
const char *StrWndName = "MFC Learning Experience";

InitCommonControls();
CoInitialize(NULL);

WndCls.style = CS_VREDRAW | CS_HREDRAW;
WndCls.lpfnWndProc = AfxWndProc;
WndCls.cbClsExtra = 0;
WndCls.cbWndExtra = 0;
WndCls.hInstance = AfxGetInstanceHandle();
WndCls.hIcon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
WndCls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
WndCls.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
WndCls.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);

const char *StrClass = AfxRegisterWndClass(WndCls.style, WndCls.hCursor,
WndCls.hbrBackground, WndCls.hIcon);

if (!Create(StrClass, StrWndName, WS_OVERLAPPEDWINDOW | WS_SYSMENU | WS_THICKFRAME, rectDefault, NULL, WndCls.lpszMenuName))
return;

ShowWindow(1);
}
 
OK....this should be appended to the last post...

If I place the code shown into CMainFrame::CMainFrame, it does change the background color, and also throws and Assertion and kills the application.
 
Alright, well I finally figured it out. Hopefully this will help someone down the road, or even right now. If you are using the MFC Application Wizard to start your code, and you hate that stupid white background, here is how you can change the background color to whatever you want.

1. In CMainFrame::preCreateWindow, you need to create a WNDCLASS Structure. Fill this structure in as described in the MSDN, remembering to set wndclass.hbrBackground to the color you want the window to be.

2. Register the new class you have just created: AfxRegisterClass(&wndclass);

3. Set cs.lpszClass to the name of your new class:
cs.lpszClass = wndclass.lpszClassName;

And there you have it. The mainframe's background color is now changed to what you want it to be, instead of the default white background (which in all actuallity, I believe the default WNDCLASS used actually sets it's hbrBackground to NULL).

Thanks for your help HyperEngineer!!!!
 
In MSDN for the WNDCLASS structure I did not see where the COLOR_3DFACE was a valid value. Did you try any other values? This may or may not be the problem. I'm still checking it. But this might be worth a try. Some values are:

COLOR_BTNFACE
COLOR_HIGHLIGHT
COLOR_MENU
COLOR_WINDOW

There are more, just check MSDN.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
Yeah, actually I'm not sure where 3DFACE came from, but it normally just comes up with some strange color. It must be declared somewhere because it never through a compiler error. But anyway, the problem was actually, creating, registering and then using a WNDCLASS structure, which is what I did and described. Hope someone else finds this usefull too. Thanks.
 
Glad you figured it out. Someday I may need it, because I'm always changing colors. But, for right now, I use the OnCtlColor() function.

Thanks for the info.



HyperEngineer
If it ain't broke, it probably needs improvement.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top