Link to home
Start Free TrialLog in
Avatar of danny_pav
danny_pav

asked on

CWnd::GetIcon won't work

I have this chunk of code in my app
      if (hicon == NULL) {
            CWnd * p_wnd = AfxGetMainWnd();
            if (p_wnd != NULL)
                  hicon = p_wnd->GetIcon(TRUE);
      }

I traced it to the GetIcon call. It returns NULL.  However, I use the m_hWnd member of p_wnd in SPY++ and the properties show me an icon handle.  I know the icon handle is correct because I then used the debugging tools to plug in that icon handle (replacing the NULL) and it used the correct icon.

What am I doing wrong?  How do I get this call to work?
Is there an alternate way of doing this?
Danny Pav
Avatar of Bonev
Bonev

Try p_wnd->GetIcon(FALSE);

Try p_wnd->GetIcon(FALSE);

In your CMainFrame::Create you will need to set the icon for that window.

Avatar of danny_pav

ASKER

Bonev:
That can't be right.  The correct icon is already displaying and from my original problem statement:

However, I use the m_hWnd member of p_wnd in SPY++ and the    properties show me an icon handle.  I know the icon handle is correct because I then used the debugging tools to plug in that icon handle (replacing the NULL) and it used the correct icon.

SPY++ recognizes the icon.  Why does that function fail?
danny_pav,
I wrote a sample app and I've run into the same problem.
When I put a SetIcon call in CMainFrame::Create everything went fine.
Basically, there are several things to do:
1. In InitInstance:
Check if the main frame is created and assigned to m_pMainWnd
2. In CMainFrame::Create:
call SetIcon(hIcon, FALSE); // if you need a big icon, use TRUE
3. In your function:
call GetIcon(FALSE); // the size should be the same as in 2


Why do I have to do a SetIcon when the icon is already set?  I can look at the screen to verify and I can use SPY to verify.
Sure you need to set it - you cannot force the user to put the handle manually.

ASKER CERTIFIED SOLUTION
Avatar of Answers2000
Answers2000

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I didn't think that would work with MFC.  I figured that a only a few stock window classes were used and that would include stock icons as well (if any).  

I still don't understand why GetIcon or WM_GETICON doesn't work.  It seems to be not just a shortcoming of MFC.  

Thanks, Bonev for the effort.
Thanks, Answers2000 for the answer.
>> I didn't think that would work with MFC. 
I don't see why not

>> I figured that a only a few stock window classes were used and that would include stock icons as well (if any).  
I was _strongly_ under the impression that MFC 4.2 generates strings and registers _multiple_ window classes (re-using same WndProcs).  Older versions (I think) re-use the same classes.
You can read the sources and check on whichever MFC version you're using...Search for RegisterClass