Solved

CWnd::GetIcon won't work

Posted on 1998-09-21
10
396 Views
Last Modified: 2013-11-19
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
0
Comment
Question by:danny_pav
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
10 Comments
 
LVL 1

Expert Comment

by:Bonev
ID: 1322386
Try p_wnd->GetIcon(FALSE);

0
 
LVL 1

Expert Comment

by:Bonev
ID: 1322387
Try p_wnd->GetIcon(FALSE);

0
 
LVL 1

Expert Comment

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

0
Technology Partners: 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!

 
LVL 3

Author Comment

by:danny_pav
ID: 1322389
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?
0
 
LVL 1

Expert Comment

by:Bonev
ID: 1322390
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


0
 
LVL 3

Author Comment

by:danny_pav
ID: 1322391
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.
0
 
LVL 1

Expert Comment

by:Bonev
ID: 1322392
Sure you need to set it - you cannot force the user to put the handle manually.

0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1322393
The default Icon is associated with the class not the window (windows looks for a window icon first, then if it doesn't find one then looks for a class icon).

Bonev's work round works, because it sets the window icon (rather than the window class icon) so WM_GETICON will see it.

To fix generically, you don't need to set the icon, do:

if (hicon == NULL)
{
CWnd * p_wnd = AfxGetMainWnd();

// look for window icon
if (p_wnd != NULL)
  hicon = p_wnd->GetIcon(TRUE);

// look for class icon if no window icon found
if ( (hicon == NULL ) && ( p_wnd != NULL ) )
  hicon = (HICON)::GetClassLong( p_wnd->m_hWnd, GCL_HICON ) ;
}
0
 
LVL 3

Author Comment

by:danny_pav
ID: 1322394
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.
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322395
>> 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

0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ BOOL WINAPI ReadFile fails on windows 10 when reading from USB cable 9 550
ORA-01403: no data found 43 116
How to convert MFC::CString to UTF8 wchar_t* 10 461
Sed question 2 139
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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