![]() ![]() Private const uint SHGSI_SMALLICON = 0x1 Private const uint SHGSI_LARGEICON = 0x0 Public static extern bool DestroyIcon(IntPtr handle) Public static extern int SHGetStockIconInfo(uint siid, uint uFlags, ref SHSTOCKICONINFO psii) Var icon = (Icon)Icon.FromHandle(info.hIcon).Clone() // Get a copy that doesn't use the original handleÄestroyIcon(info.hIcon) // Clean up native icon to prevent resource leak SHGetStockIconInfo(type, SHGSI_ICON | size, ref info) Info.cbSize = (uint)Marshal.SizeOf(info) Private static Icon GetStockIcon(uint type, uint size) Public static Icon FolderLarge => folderIcon ? (folderIcon = GetStockIcon(SHSIID_FOLDER, SHGSI_LARGEICON)) You can avoid this by using Clone() to get an Icon that doesn't rely on your original native handle. ![]() ![]() This means if you immediately call DestroyIcon() and then try to do something with the icon you just created, it will cause exceptions. When you create an icon using Icon.FromHandle(), the object stores the handle you gave it and will use it for later operations.When you get an icon handle from SHGetStockIconInfo (or even SHGetFileInfo, for that matter), the native icon must be cleaned up by calling DestroyIcon(), otherwise you'll create a resource leak.Here is an example class structured in a similar way to Evk's class. ![]() It's not any more complicated than SHGetFileInfo. SHGetStockIconInfo is the correct way to do it, and doesn't require the addition of unnecessary file IO. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |