Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 424
  • Last Modified:

Problems with own class name defined in PrecreateWindow()

In procedure PrecreateWindow() of CMainFrame I changed the class name for my main frame window of my application something like this:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
WNDCLASS myclass;
myclass.lpszClassName = "MyApplication";
if (!(AfxRegisterClass(&myclass)))
AfxThrowResourceException();

and so on ...

What now happens is that the icon in the upper left corner of my main frame window disappeared. What can cause that and how can I repair it, that my icon is appearing again ?

Thanks,
bye,
atari
0
atari
Asked:
atari
  • 5
  • 5
1 Solution
 
jkrCommented:
Well, you have to change 'cs.lpszClass' instead of using a (BTW: _uninitialized_ !) WNDCLASS structure...
0
 
atariAuthor Commented:
Sorry, I have only written a code snippet, because I am working on it in our company and do not  have the whole source code at home, where I'm sitting now, what I do is this way:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT &cs)
{
if ( !CMDIFrameWnd::PreCreateWindow(cs) )
     return 0;

   // Register my new window class

   WNDCLASS wndcls;
   HINSTANCE hInst = AfxGetInstanceHandle();
   if(!(::GetClassInfo(hInst, CUSTOM_CLASSNAME, &wndcls)))
   {
      if(::GetClassInfo(hInst, cs.lpszClass, &wndcls))
     {
        wndcls.lpszClassName = CUSTOM_CLASSNAME;
        if (!AfxRegisterClass(&wndcls))
          AfxThrowResourceException();
     }
     else
       AfxThrowResourceException();
    }
    cs.lpszClass = CUSTOMMAINFRAMECLASSNAME;

    return 1;
}

That worked, but the icon of the main frame window is the default VC-Icon for a new window !!!
0
 
jkrCommented:
Ok, sorry for being suspicious ;-)
Hmm, MFC is a bit difficult concerning the resource IDs - what happens when you set 'wndcls.hIcon' manually using 'LoadIcon()'?
0
Industry Leaders: 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!

 
atariAuthor Commented:
Because I have not the originally code in my home office I typed the above code in another project and I now will try to type the LoadIcon()-lines in, maybe it will work, wait a little bit.

Bye,
atari
0
 
jkrCommented:
Ok, i'll call in later again - but as i'm currently at work and it's 9:20pm meanwhile here, don't expect that i can respond later than in about 1h <s>
0
 
atariAuthor Commented:
Got it work, something like this:

#define CUSTOM_CLASSNAME _T("MyClassName") // somewhere at the top of Mainframe.cpp

and now:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
  if (!CFrameWnd::PreCreateWindow(cs) )
        return 0;

   WNDCLASS wndcls;
   HINSTANCE hInst = AfxGetInstanceHandle();
   if (!(::GetClassInfo(hInst, CUSTOM_CLASSNAME, &wndcls)))
   {
     if(::GetClassInfo(hInst, cs.lpszClass, &wndcls))
     {
          wndcls.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
        wndcls.lpszClassName = CUSTOM_CLASSNAME;
        if (!AfxRegisterClass(&wndcls))
          AfxThrowResourceException();
     }
     else
       AfxThrowResourceException();
    }
    cs.lpszClass = CUSTOM_CLASSNAME;

    return 1;
}

In MFC everything is hard work, isn't it ???

You earned your points.

0
 
atariAuthor Commented:
Got it work, something like this:

#define CUSTOM_CLASSNAME _T("MyClassName") // somewhere at the top of Mainframe.cpp

and now:

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
  if (!CFrameWnd::PreCreateWindow(cs) )
        return 0;

   WNDCLASS wndcls;
   HINSTANCE hInst = AfxGetInstanceHandle();
   if (!(::GetClassInfo(hInst, CUSTOM_CLASSNAME, &wndcls)))
   {
     if(::GetClassInfo(hInst, cs.lpszClass, &wndcls))
     {
          wndcls.hIcon = LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_MAINFRAME));
        wndcls.lpszClassName = CUSTOM_CLASSNAME;
        if (!AfxRegisterClass(&wndcls))
          AfxThrowResourceException();
     }
     else
       AfxThrowResourceException();
    }
    cs.lpszClass = CUSTOM_CLASSNAME;

    return 1;
}

In MFC everything is hard work, isn't it ???

You earned your points.

0
 
jkrCommented:
Yep, MFC is cute when you have to do 'standard things' ... but it lacks supporting customization (ever tried parametrized CRecordsets that don't use a view?)
0
 
atariAuthor Commented:
You are a funny guy, I'm learning every day in MFC, but there are such a lot of classes that I have never used, ODBC, SDK, DAO and so on, that I will always have questions, and therefore this discussion board is very nice for solving small problems.

Thanks,
'til the next meeting,
bye jkr,
atari
0
 
jkrCommented:
Hey, i'm doing computer programming for about 15y now, and one _never_ (definitely NEVER!) finishes learning ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now