dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19

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

  1. #1
    Join Date
    May 2019
    Posts
    21

    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.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,379

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

    Quote Originally Posted by Arianax View Post
    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).
    Victor Nijegorodov

  3. #3
    Join Date
    May 2019
    Posts
    21

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

    Quote Originally Posted by VictorN View Post
    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. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,379

    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.
    Victor Nijegorodov

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,379

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

    Quote Originally Posted by Arianax View Post
    '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.
    Victor Nijegorodov

  6. #6
    Join Date
    May 2019
    Posts
    21

    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. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,379

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

    Quote Originally Posted by Arianax View Post
    ... it returns '0'. Is that the same as NULL?
    yes.
    Victor Nijegorodov

  8. #8
    Join Date
    May 2019
    Posts
    21

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

    Quote Originally Posted by VictorN View Post
    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. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,379

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

    Quote Originally Posted by Arianax View Post
    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 View Post
    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.
    Victor Nijegorodov

  10. #10
    Join Date
    May 2019
    Posts
    21

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

    Quote Originally Posted by VictorN View Post
    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...

  11. #11
    Join Date
    May 2019
    Posts
    21

    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. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,379

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

    Quote Originally Posted by Arianax View Post
    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
    Victor Nijegorodov

  13. #13
    Join Date
    May 2019
    Posts
    21

    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. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    19,379

    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 );
    Victor Nijegorodov

  15. #15
    Join Date
    May 2019
    Posts
    21

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

    Quote Originally Posted by VictorN View Post
    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.

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)