Solved

Custom Class Registration

Posted on 2000-02-25
10
1,135 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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
ODBC Connection Logging, ADO.NET 6 69
Hibernate methods 2 77
fix34  challenge 9 141
sumHeights2  challenge 7 114
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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