[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()
{
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", IMAGE_ICON, 16, 16, LR_LOADFROMFILE);
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.
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
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).
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
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);
}
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Please, read the documentation!
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.
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
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.
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?
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
Originally Posted by
Arianax
... it returns '0'. Is that the same as NULL?
yes.
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
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.
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
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.
I already wrote about the possible reason:
Quote:
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.
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
Originally Posted by
VictorN
I already wrote about the possible reason:
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...
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?
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
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
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()....?
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 );
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
Originally Posted by
VictorN
Try:
Code:
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", ICON_IMAGE, 0, 0, LR_LOADFROMFILE );
No improvement.
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
Originally Posted by
Arianax
No improvement.
Could you post (attach to your post) your icon file?
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
Originally Posted by
VictorN
Could you post (attach to your post) your icon file?
I was thinking the same thing... it must be the .ico file...
I have made this file simply by saving a 16x16 .bmp with MSPAINT as a 256color bitmap with the file extension .ico:
I get the error message 'invalid file' when I try to upload a .ico file to this site... Maybe I have created it incorrectly..?
Visual Studio 2017 interprets the file as a BMP if I attach it to the Resource Editor (something I don't want to do since I prefer LoadImage() to LoadIcon()).
Re: Changing the Small and Big icons of a window using LoadImage... How to?
IT WORKS!!!
It turned out that I needed to convert the standard 24-Bit BMP file into a .ico file using a specialized Icon Converter piece of software.
The only strange thing is:
I still receive the error messages when I do error checking, but the icon appears correctly when the Window is created during Debug mode.
So the following code works effectively:
Code:
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", ICON_IMAGE, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
Many thanks for all your assistance Victor...
Re: Changing the Small and Big icons of a window using LoadImage... How to?
Quote:
Originally Posted by
Arianax
I still receive the error messages when I do error checking, but the icon appears correctly when the Window is created during Debug mode.
So the following code works effectively:
Code:
HICON hIconSm = (HICON)LoadImage(NULL, L"Icon.ico", ICON_IMAGE, 0, 0, LR_LOADFROMFILE | LR_DEFAULTSIZE);
Again: what error message and how did you check it?