none
How to link icons with enum? RRS feed

  • Question

  • I have an image strip containing a list of 16x16 icons, e.g. 150 icons, I also have an enum having 150 values, how can map each icon in the image strip to each value in the enum?

    Thanks in advance.

    Friday, September 20, 2019 9:47 AM

Answers

  • Every image added to an image list is assigned an index.  Image list indexes are 0 based, so the first image gets assigned index 0.

    So if your enum starts with 0 for the first entry then it will correspond directly with the image list indexes.

    • Marked as answer by Yan Yang Monday, September 23, 2019 10:11 AM
    Friday, September 20, 2019 10:40 AM
  • CImageList::Create

    You use CImageList. Then you must use an index to get the corresponding icon. You can use the enum as the index.

    enum eIcon { ICON1 = 1, ICON2, ICON3,... }; for (int i = 0; i < myImageList.GetImageCount(); i++) { hIcon = myImageList.ExtractIcon(i); // i is from 0..149 } // int GetIndexInImageList(enum eIcon i) { return i - ICON1; // The enum value is used like an integer placeholder }

    //
    int i = GetIndexInImageList(ICON3);
    hIcon = myImageList.ExtractIcon(i);


    Regards, Guido

    • Marked as answer by Yan Yang Monday, September 23, 2019 10:11 AM
    Friday, September 20, 2019 10:59 AM

All replies

  • Hello,

    how are the Icons defined in the "image strip"? An enum is a integer value. So if the Icons are defined as string with the corresponding enum number "icon1" for enum1, "icon2" for enum2,... then you can set an enum variable by extracting the value, e.g.

    enum { ICON1 = 1, ICON2, ICON3,...
    
    CString szIcon = "icon1"; // "icon2"...
    int i = atoi(szIcon.Mid(4));
    switch (i)
    {
     case ICON1: cout << "Icon 1"; break;
     case ICON2: cout << "Icon 2"; break;
     ...
    }
    

    Regards, Guido

    Friday, September 20, 2019 9:53 AM
  • Thanks very much for your reply.

    The size of an icon is 16x16, the resource file is a 2400x16 bitmap image which contains 150 16x16 icons. I want to load the image and map each icon in the image to an enum value.

    Thanks again.

    Friday, September 20, 2019 10:01 AM
  • The size of an icon is 16x16, the resource file is a 2400x16 bitmap image which contains 150 16x16 icons. I want to load the image and map each icon in the image to an enum value.

    How do you split the big image into 150 small icons?
    Friday, September 20, 2019 10:16 AM
  • CImageList::Create
    Friday, September 20, 2019 10:28 AM
  • Every image added to an image list is assigned an index.  Image list indexes are 0 based, so the first image gets assigned index 0.

    So if your enum starts with 0 for the first entry then it will correspond directly with the image list indexes.

    • Marked as answer by Yan Yang Monday, September 23, 2019 10:11 AM
    Friday, September 20, 2019 10:40 AM
  • CImageList::Create

    You use CImageList. Then you must use an index to get the corresponding icon. You can use the enum as the index.

    enum eIcon { ICON1 = 1, ICON2, ICON3,... }; for (int i = 0; i < myImageList.GetImageCount(); i++) { hIcon = myImageList.ExtractIcon(i); // i is from 0..149 } // int GetIndexInImageList(enum eIcon i) { return i - ICON1; // The enum value is used like an integer placeholder }

    //
    int i = GetIndexInImageList(ICON3);
    hIcon = myImageList.ExtractIcon(i);


    Regards, Guido

    • Marked as answer by Yan Yang Monday, September 23, 2019 10:11 AM
    Friday, September 20, 2019 10:59 AM