Thread: LoadIcon in win API window class

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    LoadIcon in win API window class

    Hey, I'm trying to learn WIN API and I'm using these tutorials:
    http://www.winprog.org/tutorial/

    Ok, I made a basic window, and I have been experimenting with the different styles and stuff, but I haven't been able to load an icon. I'm using this:

    Code:
    mw.hIcon         = LoadIcon(hInstance, "large_icon.bmp");
    mw.hIconSm       = LoadIcon(hInstance, "small_icon.bmp");
    The bmp's are in the same directory as the exe. They don't have to be included by the complier as a resource or something do they?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    LoadIcon, LoadCursor and LoadBitmap have all been superseded by LoadImage for a few years now so you should prefer the use of that api function.

    An icon isn't a bitmap (well, it's actually at least one bitmap and corresponding mask(s) and some information that describes them; a cursor is very similar) so trying to load a bitmap where an icon is expected is unlikely to behave as you would seem to hope.

    In order to use LoadImage to load a non-resource image file, you need to specify the path to that image file. If it's part of an executable's (including dll) resource section then you use the instance handle for that executable/dll as the first parameter and converted resource id instead of the file name.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    310
    agree, but LoadIcon has less params
    * PC: Intel Core 2 DUO E6550 @ 2.33 GHz with 2 GB RAM: Archlinux-i686 with xfce4.
    * Laptop: Intel Core 2 DUO T6600 @ 2.20 GHz with 4 GB RAM: Archlinux-x86-64 with xfce4.

  4. #4
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks for the help. I actually ended up using an icon, but I ran into some trouble, LoadIcon() expects an integer, so I had to create a resource file (which I had never done before). It was good practice though.


    Also, are you saying if I were to use the LoadImage() function, I wouldn't need a resource number? Just a filename and path?

    Anyway, thanks for your help,
    David

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Juneda
    Posts
    291
    Don't need to use integers: you'll probably have a *.rc like

    Code:
    theicon ICON "theicon.ico"
    now you can use it with

    Code:
    wc.hIcon=LoadIcon(hInstance,"theicon");
    //or
    wc.hIcon=(HICON)LoadImage(hInstance,"theicon",IMAGE_ICON,32,32,LR_DEFAULTCOLOR);
    Hope that will help
    Niara

  6. #6
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Well, when I changed it to:
    Code:
    wc.hIcon = LoadIcon(hinstance, "IDI_MYICON")
    and it didn't give me an error, but it also didn't work.

    I looked it up, LoadIcon() does except a handle and an integer. So it will only work if I do this:
    Code:
    wc.hIcon = LoadIcon(hinstance, 101)
    101 is my resource id/code majigger.

    or:
    Code:
    wc.hIcon = LoadIcon(hinstance, MAKEINTRESOURCE(IDI_MYICON))
    But according to Ken If I use LoadImage() instead I can just use the filename and path.

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You can use whichever you choose - as described by msdn for LoadImage; it's considerably more versatile than the functions it supersedes.

    If you have a numeric resource identifier then use the MAKEINTRESOURCE macro to convert it to the correct type.

    Just remember to free non-shared image resources as described in msdn's LoadImage page.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  2. Pong is completed!!!
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 05-26-2005, 10:50 AM
  3. help with this
    By tyrantil in forum C Programming
    Replies: 18
    Last Post: 01-30-2005, 04:53 PM
  4. my wndProc is out of scope
    By Raison in forum Windows Programming
    Replies: 35
    Last Post: 06-25-2004, 07:23 AM
  5. opengl help
    By heat511 in forum Game Programming
    Replies: 4
    Last Post: 04-05-2004, 01:08 AM