Custom Class Registration

Posted on 2000-02-25
Last Modified: 2013-11-20
What I want to do is register a custom window class for a window I am creating so I can give it a menu, icon, and more importantly, change its background color. It works fine until I try to change the color, since apparently that requires registering my own class.

Anyway, every time I try to register a class with ::AfxRegisterWndClass(), it fails. I narrowed it down to that function. For some reason it simply will not register.

Am I correct in putting the registration in the CMyApp:InitInstance() function? That is where I have the class registration and CMyFrame::Create () calls. I then put the code to create child controls in CMyFrame::PreCreateWindow().

Where should I put these calls, what is the correct way to get this program rolling? I have a book (Programming Windows with MFC 2nd ed by Jeff Prosise), but when it comes to registering classes and setting up menus, icons, accelerator tables, background brushes, etc. for a window it skimps on the details.
Question by:TheSnowman
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5

Expert Comment

ID: 2561049
When you say that the class "will not register", what do you mean by that?

What is returned by AfxRegisterWndClass?

Is this a debug build? If so, then AfxRegisterWndClass should give an explanation (it uses TRACE functions) in the debug output.

What version VC are you using?

Can you post your AfxRegisterWndClass call as you have it currently?


Author Comment

ID: 2561288
// Local variables
CString   strWndClass;
HICON     hIcon;
HCURSOR   hCursor;

// Get handles to icon/cursor
.. . .

// Register window class
  strWndClass = ::AfxRegisterWndClass (WS_OVERLAPPEDWINDOW, hCursor,
        0, hIcon);
catch (CResourceException *pEx)
  ::AfxMessageBox ("Could not register window class", MB_ICONERROR, NULL);
  return FALSE;

Whenever this code executes, that exception is caught. The messagebox displays, and the function returns FALSE so the program terminates.

I am using Microsoft Visual C++ 6.0.

The function does not return anything, since it throws the exception.

Tracing does not give any useful information, basically, it lets me know an exception was thrown.

Accepted Solution

SamHobbs earned 75 total points
ID: 2561341
The documentation of AfxRegisterWndClass says to refer to the WNDCLASS structure for a list of class styles. The WNDCLASS structure does not specify that windows styles are valid, so the problem is probably the WS_OVERLAPPEDWINDOW style.

There does not seem to be a class style corresponding to the WS_OVERLAPPEDWINDOW window style. Typically some or all of the following are used:


You can use the Developer's Studio "Find in Files" to search for things in the MFC samples; in this case, I searched for "AfxRegisterWndClass" and got 11 sample uses.

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 2561396
Tried it, the program still crashes. I get an error in file "wincore.cpp" at line 735 when I try to "Create" the frame window:

// Create window
m_pMainWnd->Create (strWndClass, "MyApp", WS_OVERLAPPEDWINDOW, CFrameWnd::rectDefault, NULL,
      (unsigned int) MAKEINTRESOURCE (IDI_DIR_FRAME));

Here is the code in wincore.cpp that fails:

BOOL CWnd::Create(LPCTSTR lpszClassName,
      LPCTSTR lpszWindowName, DWORD dwStyle,
      const RECT& rect,
      CWnd* pParentWnd, UINT nID,
      CCreateContext* pContext)
      // can't use for desktop or pop-up windows (use CreateEx instead)
      ASSERT(pParentWnd != NULL);
.. . . }

That ASSERT fails. I know I got it to work before, since the application has run and I never had a parent window (it is a top-level frame window).

By the way, the class now registers fine! Thanks.

Author Comment

ID: 2561403
Adjusted points to 75

Expert Comment

ID: 2561548
Well it sounds like you have a new problem. I notice that your create is for m_pMainWnd. There are at least two possibilities here. One possibility is that the main window is already created, but I assume you would know that that is a problem. Since you said that the wincore.cpp line that fails has "CWnd::Create" in it, it appears to me that you are somehow getting CWnd::Create when you probably want CFrameWnd::Create. Is that possible?

You might be able to override much less of MFC by using PreCreateWindow to specify a custom class for the frame window. PreCreateWindow is apparently where MFC supplies the window class.

Author Comment

ID: 2561628
CFrameWnd is a subclass of CWnd, so if it doesn't override the Create() method it would cascade from my class, the CFrameWnd, to CWnd. And the problem is that there is no parent window. The ASSERT fails because it is a null pointer, but I have nothing for it to point to.

What am I doing wrong here? You did answer my main question, but I did say "Where should I put these calls, what is the correct way to get this program rolling?" as well. The book I am using to learn tells how to do create windows, but doesn't give any examples for when you want to specify more resources than a menu. The MSDN can be verbose at times, but in this case I found it to be overly concise.

Expert Comment

ID: 2561733
I am not going to be picky about details. Since the points earned are worthless, it really is not important. If I answer a question, then I would prefer to get the points instead of someone else, but otherwise it is not important to me. You might get as much help or more help at, since there is no point system there so other volunteers might pick up when someone else might not be available. If this were the CodeGuru web site, then the advantage to creating a new post is that the subject could indicate something more relevant to the problem and therefore someone who is likely to know the answer is more likely to look at it.

Many times the Experts Exchange is more likely to provide answers for more advanced questions, so it is hard to say which forum is better.

I admit I ignored the question about where to put the AfxRegisterWndClass, since as far as I know, it does not matter very much. I think that where AfxRegisterWndClass is put is not the problem.

I have VC 5, so my MFC source code might be different from yours, but in my version, CFrameWnd definitely overrides Create, and it uses CreateEx. So that is a bit confusing, but in this situation it is less confusing because it explains why CWnd::Create is complaining about not using CreateEx.

You might want to find CFrameWnd::Create in your MFC source. One way to find it is to do a "Find in Files" to search multiple source files. For VC 5, it is in WINFRM.CPP, line 542.

But yes, CMyFrame::PreCreateWindow is probably a good place to put your AfxRegisterWndClass, but it will probably work just as well in your CMyApp:InitInstance() anywhere before main frame creation.


Author Comment

ID: 2561744
I had every intention of accepting your answer once the second part of my question was answered. I thought it was a simple question to anyone with more experience in MFC than myself, that is why my points are "worthless" at 75. It's not like I am having trouble programming an OLE container to display web pages or something really complicated. I am asking help with the basics.

I know I can get it to compile and run, but not with the desired effects (using ALL the resources I want.) That is why I asked about where to put stuff.

Expert Comment

ID: 2561762
I did not want to be critical of you. I am sorry to have made you feel bad. I did not intend to.

You are right that I should be more understanding of beginners.

When I said that points are worthless, it was my intent to relieve you from worrying about points. I did not want you to worry about that.

The important thing is that I was able to help. You will soon be an expert too.


Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA "SendKeys" Syntax for Multiple Keystrokes 7 125
Modbus - whats the maximum I can store in one register? 4 81
Path of Workbook 3 87
Capture logon name 13 104
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question