CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: [RESOLVED] Changing the Small and Big icons of a window using LoadImage... How to?

1. Member
Join Date
May 2019
Posts
41

## [RESOLVED] Changing the Small and Big icons of a window using LoadImage... How to?

So another question since I've hit another stumbling block...

Tried about 7 different guides and many different forum posts, none of which are working, I must be doing something obvious wrong...

Trying to change the 'small' icon (upper-left icon in a window) and the 'Big' icon (icon that appears in the taskbar) of a Window I've created with Win32.

I'm using LoadImage() and placing the result into an 'HICON' Handle. I'm not using LoadIcon().

Here's the relevant code:

Under WNDCLASSEX wc in WinMain:

Code:
WNDCLASSEX wc;

wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
and then, later, under a custom function I made that appears under WM_CREATE in WndProc function:

Code:
void LoadVisuals()
{

SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);

}
The icon doesn't change, it remains the same.

I run some error checking. The LoadImage() part reports Error Code 0 (function successful) but the SendMessage() part reports 'Error 1813: The specified resource type cannot be found in the image file.'

I have confirmed the .ico icon's file path and have tried specifying the FULL PATH ("C:\\files\\to\\icon.ico").

Can anyone explain what I'm doing wrong? Am I placing LoadVisuals() in the wrong part of WndProc? Am I casting LoadImage() to the wrong handle-type? Is wc.hIconSm overriding the SendMessage()?

Appreciate your help - I've checked lots of other forum posts but nothing has worked so far.

2. ## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by Arianax
Code:
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
I run some error checking. The LoadImage() part reports Error Code 0 (function successful) but the SendMessage() part reports 'Error 1813: The specified resource type cannot be found in the image file.'
1. There is no error checking in your code snippet.
2. If LoadImage returns NULL then you have to call GetLastError.
3. WM_SETICON message cannot report any error. It just returns the HICON of the previous icon (or NULL if there was not the icon of the given type).

3. Member
Join Date
May 2019
Posts
41

## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by VictorN
1. There is no error checking in your code snippet.
2. If LoadImage returns NULL then you have to call GetLastError.
3. WM_SETICON message cannot report any error. It just returns the HICON of the previous icon (or NULL if there was not the icon of the given type).

Error checking:

Code:
if(!hIcon)
{

DWORD dLastError = GetLastError();

LPCTSTR strErrorMessage = NULL;

FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
dLastError,
0,
(LPWSTR) &strErrorMessage,
0,
NULL);

MessageBox(hWnd, strErrorMessage, L"Error", MB_OK);

}
and:

Code:
if(!SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm))
{

DWORD dLastError = GetLastError();

LPCTSTR strErrorMessage = NULL;

FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ARGUMENT_ARRAY | FORMAT_MESSAGE_ALLOCATE_BUFFER,
NULL,
dLastError,
0,
(LPWSTR) &strErrorMessage,
0,
NULL);

MessageBox(hWnd, strErrorMessage, L"Error", MB_OK);

}

4. ## Re: Changing the Small and Big icons of a window using LoadImage... How to?

WM_SETICON message may return NULL if no icon was previously set. And it does not mean any error.
Moreover, SendMessage does NOT SetLastError. So it does not make any sense to call GetLastError independing on the SendMessage return value.

5. ## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by Arianax
'Error 1813: The specified resource type cannot be found in the image file.'
If it was the error after LoadImage returned NULL, then it means that your icon file does not contain the icon 16x16.

6. Member
Join Date
May 2019
Posts
41

## Re: Changing the Small and Big icons of a window using LoadImage... How to?

I see... but I'm still not sure where to find a clue as to why the Icon is not appearing...

I have read the documentation for LoadImage() and it says it sets GetLastError() but it returns '0'. Is that the same as NULL?

7. ## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by Arianax
... it returns '0'. Is that the same as NULL?
yes.

8. Member
Join Date
May 2019
Posts
41

## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by VictorN
yes.
So it's an error, but I don't know what type of error or what is causing it... so it's not much help.

Any further ideas as to what I should try to fix this? I have done lots of research but answers have not yielded results.

9. ## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by Arianax
So it's an error, but I don't know what type of error or what is causing it... so it's not much help.

Any further ideas as to what I should try to fix this? I have done lots of research but answers have not yielded results.
Originally Posted by VictorN
If it was the error after LoadImage returned NULL, then it means that your icon file does not contain the icon 16x16.
You could try to open your icon file with the VS and see which icon types this file contains.

10. Member
Join Date
May 2019
Posts
41

## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by VictorN

You could try to open your icon file with the VS and see which icon types this file contains.
hIcon = 16x16, 24-Bit, BMP with file extension '.ico'

Do I need to convert it to .ico? It already has that file extension...

11. Member
Join Date
May 2019
Posts
41

## Re: Changing the Small and Big icons of a window using LoadImage... How to?

I have specified:

Code:
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
Under WNDCLASSEX;

Does LoadImage() and SendMessage() override this?

I have placed LoadImage() and SendMessage() under the case WM_CREATE: in WndProc.
Is this the right place?

12. ## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by Arianax
I have specified:

Code:
wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
Under WNDCLASSEX;

Does LoadImage() and SendMessage() override this?

I have placed LoadImage() and SendMessage() under the case WM_CREATE: in WndProc.
Is this the right place?
1. Did you mean "override" or "overwrite"?
2. You may want to show the code snippets including WM_CREATE and WNDCLASSEX

13. Member
Join Date
May 2019
Posts
41

## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Code:
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", ICON_IMAGE, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);

if(hIconSm == NULL)
{
MessageBox(hWnd, L"Icon returned NULL!!", L"Error", MB_OK);
Above code is still returning NULL, so I guess hIconSm isn't accepting the call to LoadImage()....?
Last edited by Arianax; June 4th, 2019 at 10:56 AM.

14. ## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Try:
Code:
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", ICON_IMAGE, 0, 0, LR_LOADFROMFILE );

15. Member
Join Date
May 2019
Posts
41

## Re: Changing the Small and Big icons of a window using LoadImage... How to?

Originally Posted by VictorN
Try:
Code:
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", ICON_IMAGE, 0, 0, LR_LOADFROMFILE );
No improvement.
Last edited by Arianax; June 4th, 2019 at 10:57 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•