5

I am working with the express version of Visual Studio. Therefore, using functions calls to MAKEINTRESOURCE are out of the question. I am tryting to set the application icon by overriding the getAdditionalClassInfo function.

WNDCLASSW *Robot::getAdditionalClassInfo(void) const {
    WNDCLASSW *wc = Window::getAdditionalClassInfo();
    HANDLE hIcon = LoadImage(NULL, L"imagepath/image.png", 32, 32, LR_LOADFROMFILE);

    wc->hIcon = .....;
    return wc;
}

Does anyone know how I can set this icon WITHOUT using a resource?

Alex van Rijs
  • 803
  • 5
  • 17
  • 39
  • 1
    You have to use a resource. Using the Express edition is okay when you have a lot of free time to figure out how to write a .rc file. It is meant as a learning edition. – Hans Passant Aug 19 '13 at 13:13
  • 2
    Your call to `LoadImage` is missing the type parameter (`IMAGE_ICON`), and I don't know if Windows accepts directly loading PNG files as icons; you should use a program like Gimp to make it an actual icon file (.ico) first. Also like Hans said, you ''can'' make resources with an Express version, only you have to write the .rc file manually (the Express version comes with the resource compiler even though it has no resource editor). – Medinoc Aug 19 '13 at 13:15
  • Don't get deep into this. Either make your life bearable by getting free Resource Editor to use with Express, or buy a commercial edition. – SChepurin Aug 19 '13 at 13:25
  • 2
    A .rc file containing a single icon is hardly that complicated. – Jonathan Potter Aug 19 '13 at 19:36

1 Answers1

3

My suggestion, if you'd like to use PNGs, and be able to change the icon, is to use FreeImage to load it. Then you can use FreeImage to convert it to a standard HBITMAP fairly easily.

If you're fine with using an actual icon file, you can do the following once the window has been created:

HANDLE hIcon = LoadImage(0, _T("imagepath/image.ico"), IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE);
if (hIcon) {
    //Change both icons to the same icon handle.
    SendMessage(hwnd, WM_SETICON, ICON_SMALL, hIcon);
    SendMessage(hwnd, WM_SETICON, ICON_BIG, hIcon);

    //This will ensure that the application icon gets changed too.
    SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_SMALL, hIcon);
    SendMessage(GetWindow(hwnd, GW_OWNER), WM_SETICON, ICON_BIG, hIcon);
}

You can likely call the similar function from within your getAdditionalClassInfo and setting it to the hIcon.

Mike Weir
  • 3,094
  • 1
  • 30
  • 46
  • Should `hIcon` be released somehow after this? – M.M Dec 09 '14 at 22:29
  • @MattMcNabb Don't release it until it's not needed anymore. Releasing it before the window is unloaded will mess things up. – Mike Weir Dec 10 '14 at 01:17
  • so I'll have to store this handle along with the window class and then call CloseHandle on it when the window is destroyed? – M.M Dec 10 '14 at 01:27
  • according to [MSDN doc](http://msdn.microsoft.com/en-us/library/windows/desktop/ms648063%28v=vs.85%29.aspx) another option is to set LR_SHARED – M.M Dec 11 '14 at 03:53
  • @MattMcNabb Sorry, my bad :). – Mike Weir Dec 11 '14 at 20:36