Thread: Getting resource name from int id

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Getting resource name from int id

    I am trying to create a static icon control (SS_ICON). MSDN tells me that I need to supply the resource name of the icon as the "text" argument for CreateWindow; however I only have an integer identifier of the resource. How can I obtain the "name" of a resource given that I have its identifier? I've tried MAKEINTRESOURCE, but this returns something other than "the name" (and needless to say, causes crashing).

    I could change my resource file and give the icon a "name" ID instead of a numeric ID, or I could send a STM_SETICON message after creation to make it work, but I'm thinking there must be a better way... a way to convert from integer ID to name ID and vice verca.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    In case a function expects an ID and you have a numeric ID, you should use the MAKEINTRESOURCE macro. If you get crashing, then you're probably doing something wrong. You could post the code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    That's what I thought at first too, but on taking a closer look at the MAKEINTRESOURCE macro it seems that it doesn't exactly mimic a resource name:
    Quote Originally Posted by MSDN
    The MAKEINTRESOURCE macro converts an integer value to a resource type compatible with the resource-management functions. This macro is used in place of a string containing the name of the resource...The return value should be passed only to functions which explicitly indicate that they accept MAKEINTRESOURCE as a parameter."
    From this I gather that it mimics a resource name, but is not quite the same thing, and can only be used where explicitly accepted (CreateWindow does not indicate its acceptance for static controls).
    But just in case, here is my code:
    Code:
    CreateWindowEx(0, WC_STATIC, MAKEINTRESOURCE(ID_MYICON), WS_CHILD | WS_VISIBLE | SS_ICON,
                    x, y, 0, 0, hwnd, (HMENU)IDC_STATIC, hInst, NULL);
    If it makes any difference, my only other suspicion is that, because my icon is a shared resource, the LoadIcon call being implicitly done in CreateWindow is messing up somehow, only working properly when I explicitly call LoadImage in a subsequent STM_SETIMAGE message, like so:
    Code:
    HWND h = CreateWindowEx(0, WC_STATIC, NULL,
        WS_CHILD | WS_VISIBLE | SS_ICON,
        x, y, 0, 0, hwnd, (HMENU)IDC_STATIC, hInst, NULL);
            SendMessage(h, STM_SETICON,
                (WPARAM)(HICON)LoadImage(hInst, MAKEINTRESOURCE(ID_MYCON), IMAGE_ICON, 32, 32, LR_SHARED), 0);
    Last edited by @nthony; 01-01-2008 at 09:47 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it's a macro that allows you to pass an integer resource where a string resource is accepted. CreateWindow(Ex) should accept one as a parameter. About the icon, I'm uncertain, really.
    I don't deal with native Win32 APIs a lot.

    Anyway, I don't know how CreateWindowEx works when adding icons to controls. I only know that MFC uses STM_SETICON. It might also be a good idea to load the icon first and check for a valid return and save the handle since you're supposed to close it later, I think.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Ok thanks. Since you mentioned MFC using STM_SETICON, it probably is the case that CreateWindow is a little lacking in the way it handles static icons creation, so I'll continue to use the second method with STM_SETICON. No need to worry about the handle though, as its a shared icon, so windows automatically cleans up after it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 2
    Last Post: 03-24-2006, 08:36 PM
  4. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM
  5. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM