Custom Class Registration

Posted on 2000-02-25
Medium Priority
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
  • 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 150 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.

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!


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 www.codeguru.com, 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

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
How to fix display issue, screen flickering issue when I plug in power cord to the machine. Before I start explaining the solution lets check out once the issue how it looks like after I connect the power cord. most of you also have faced this…
Suggested Courses

601 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