Solved

Custom Class Registration

Posted on 2000-02-25
10
1,126 Views
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.
0
Comment
Question by:TheSnowman
  • 5
  • 5
10 Comments
 
LVL 3

Expert Comment

by:SamHobbs
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?

0
 

Author Comment

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

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

// Register window class
try
{
  strWndClass = ::AfxRegisterWndClass (WS_OVERLAPPEDWINDOW, hCursor,
        0, hIcon);
}
catch (CResourceException *pEx)
{
  ::AfxMessageBox ("Could not register window class", MB_ICONERROR, NULL);
  pEx->Delete();
  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.
0
 
LVL 3

Accepted Solution

by:
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:

CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW

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.

0
 

Author Comment

by:TheSnowman
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.
0
 

Author Comment

by:TheSnowman
ID: 2561403
Adjusted points to 75
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 3

Expert Comment

by:SamHobbs
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.
0
 

Author Comment

by:TheSnowman
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.
0
 
LVL 3

Expert Comment

by:SamHobbs
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.

0
 

Author Comment

by:TheSnowman
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.
0
 
LVL 3

Expert Comment

by:SamHobbs
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.

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

705 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now