possible to get the exact handle of Icon which is inserted into ImageList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prpradip
    New Member
    • Nov 2008
    • 28

    possible to get the exact handle of Icon which is inserted into ImageList

    In C#, Is it possible to get the exact handle of Icon which is inserted into ImageList from ImageList?

    Note: ImageList_GetIc on() returns not exact handle

    Eg.
    ------------------------------------
    Icon x=new Icon(....);
    _imageList.Imag es.Add(x);
    IntPtr iHnd=x.Handle;
    ------------------------------------

    And I want from _imageList so that ......
    _imageList.some thing.Handle = x.Handle is satisfied
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    I wouldn't have thought it was possible to do exactly what you want, in the way you want. Here's why:

    When you add an Icon to the ImageList, it is first converted to a bitmap, which is a whole new object with a whole new handle. Because of this, the handle of the Icon you added to the ImageList initally will never equal the handle of the corresponding image in the ImageList. They are different objects.
    If you want to be able to marry up the original icon with an item in the list at a later date I would suggest using the overload of ImageList.Add that allows you to add a key. You could even use the original handle as a key. See this following example.

    Code:
       Icon icon = new Icon(); // Construct your icon here
                IntPtr p1 = icon.Handle;
                string key = p1.ToString();
                imageList1.Images.Add(key, icon);
    You can then find this image later using:

    Code:
      IntPtr p1 = icon.Handle;
                string key = p1.ToString();
                Image i = imageList1.Images[key];

    Comment

    • prpradip
      New Member
      • Nov 2008
      • 28

      #3
      Hey nukefusion,
      Its working thnx.

      Comment

      Working...