Freigeben über


StockIcons for Windows Forms?

This is what I had written to better understand the API, in C++/CLI and for Windows Forms. One could use it like this:

System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {

   Drawing::Icon ^ ic = StockIcons::GetIcon(StockIconIdentifier::Shield, StockIconFlag::ShellSize);

   button->ImageList = gcnew ImageList();

   button->ImageList->Images->Add(ic);

   button->ImageIndex = 0;

}

 

If you would rather have this in C#, translating it should not be that hard. (Hint: have a look at StockIcons for Windows Presentation Framework?)

#pragma once

using namespace System;

using namespace Drawing;

namespace VistaBridge { namespace UI {

public enum class StockIconFlag {

   Handle = SHGSI_ICON,

   SystemIndex = SHGSI_SYSICONINDEX,

   LinkOverlay = SHGSI_LINKOVERLAY,

   Selected = SHGSI_SELECTED,

   Large = SHGSI_LARGEICON,

   ShellSize = SHGSI_SHELLICONSIZE

};

// Once the final Windows SDK is released, delete this type:

typedef enum {

    SIID_DOCNOASSOC = 0, // document (blank page), no associated program

    SIID_DOCASSOC = 1, // document with an associated program

    SIID_APPLICATION = 2, // generic application with no custom icon

    SIID_FOLDER = 3, // folder (closed)

    SIID_FOLDEROPEN = 4, // folder (open)

    SIID_DRIVE525 = 5, // 5.25" floppy disk drive

    SIID_DRIVE35 = 6, // 3.5" floppy disk drive

    SIID_DRIVEREMOVE = 7, // removable drive

    SIID_DRIVEFIXED = 8, // fixed (hard disk) drive

    SIID_DRIVENET = 9, // network drive

    SIID_DRIVENETDISABLED = 10, // disconnected network drive

    SIID_DRIVECD = 11, // CD drive

    SIID_DRIVERAM = 12, // RAM disk drive

    SIID_WORLD = 13, // entire network

    SIID_SERVER = 15, // a computer on the network

    SIID_PRINTER = 16, // printer

    SIID_MYNETWORK = 17, // My network places

    SIID_FIND = 22, // Find

    SIID_HELP = 23, // Help

    SIID_SHARE = 28, // overlay for shared items

    SIID_LINK = 29, // overlay for shortcuts to items

    SIID_SLOWFILE = 30, // overlay for slow items

    SIID_RECYCLER = 31, // empty recycle bin

    SIID_RECYCLERFULL = 32, // full recycle bin

    SIID_MEDIACDAUDIO = 40, // Audio CD Media

    SIID_LOCK = 47, // Security lock

    SIID_AUTOLIST = 49, // AutoList

    SIID_PRINTERNET = 50, // Network printer

    SIID_SERVERSHARE = 51, // Server share

    SIID_PRINTERFAX = 52, // Fax printer

    SIID_PRINTERFAXNET = 53, // Networked Fax Printer

    SIID_PRINTERFILE = 54, // Print to File

    SIID_STACK = 55, // Stack

    SIID_MEDIASVCD = 56, // SVCD Media

    SIID_STUFFEDFOLDER = 57, // Folder containing other items

    SIID_DRIVEUNKNOWN = 58, // Unknown drive

    SIID_DRIVEDVD = 59, // DVD Drive

    SIID_MEDIADVD = 60, // DVD Media

    SIID_MEDIADVDRAM = 61, // DVD-RAM Media

    SIID_MEDIADVDRW = 62, // DVD-RW Media

    SIID_MEDIADVDR = 63, // DVD-R Media

    SIID_MEDIADVDROM = 64, // DVD-ROM Media

    SIID_MEDIACDAUDIOPLUS = 65, // CD+ (Enhanced CD) Media

    SIID_MEDIACDRW = 66, // CD-RW Media

    SIID_MEDIACDR = 67, // CD-R Media

    SIID_MEDIACDBURN = 68, // Burning CD

    SIID_MEDIABLANKCD = 69, // Blank CD Media

    SIID_MEDIACDROM = 70, // CD-ROM Media

    SIID_AUDIOFILES = 71, // Audio files

    SIID_IMAGEFILES = 72, // Image files

    SIID_VIDEOFILES = 73, // Video files

    SIID_MIXEDFILES = 74, // Mixed files

    SIID_FOLDERBACK = 75, // Folder back

    SIID_FOLDERFRONT = 76, // Folder front

    SIID_SHIELD = 77, // Security shield. Use for UAC prompts only.

    SIID_WARNING = 78, // Warning

    SIID_INFO = 79, // Informational

    SIID_ERROR = 80, // Error

    SIID_KEY = 81, // Key / Secure

    SIID_SOFTWARE = 82, // Software

    SIID_RENAME = 83, // Rename

    SIID_DELETE = 84, // Delete

    SIID_MEDIAAUDIODVD = 85, // Audio DVD Media

    SIID_MEDIAMOVIEDVD = 86, // Movie DVD Media

    SIID_MEDIAENHANCEDCD = 87, // Enhanced CD Media

    SIID_MEDIAENHANCEDDVD = 88, // Enhanced DVD Media

    SIID_MEDIAHDDVD = 89, // HD-DVD Media

    SIID_MEDIABLURAY = 90, // BluRay Media

    SIID_MEDIAVCD = 91, // VCD Media

    SIID_MEDIADVDPLUSR = 92, // DVD+R Media

    SIID_MEDIADVDPLUSRW = 93, // DVD+RW Media

    SIID_DESKTOPPC = 94, // desktop computer

    SIID_MOBILEPC = 95, // mobile computer (laptop/notebook)

    SIID_USERS = 96, // users

    SIID_MEDIASMARTMEDIA = 97, // Smart Media

    SIID_MEDIACOMPACTFLASH = 98, // Compact Flash

    SIID_DEVICECELLPHONE = 99, // Cell phone

    SIID_DEVICECAMERA = 100, // Camera

    SIID_DEVICEVIDEOCAMERA = 101, // Video camera

    SIID_DEVICEAUDIOPLAYER = 102, // Audio player

    SIID_NETWORKCONNECT = 103, // Connect to network

    SIID_INTERNET = 104, // Internet

    SIID_ZIPFILE = 105, // ZIP file

    SIID_SETTINGS = 106, // Settings

    SIID_MAX_ICONS = 107,

} FINAL_SHSTOCKICONID;

public enum class StockIconIdentifier {

   DocumentNotAssociated = SIID_DOCNOASSOC,

   DocumentAssociated = SIID_DOCASSOC,

  Application = SIID_APPLICATION,

   Folder = SIID_FOLDER,

   FolderOpen = SIID_FOLDEROPEN,

   Drive525 = SIID_DRIVE525,

   Drive35 = SIID_DRIVE35,

   DriveRemove = SIID_DRIVEREMOVE,

   DriveFixed = SIID_DRIVEFIXED,

   DriveNet = SIID_DRIVENET,

   DriveNetDisabled = SIID_DRIVENETDISABLED,

   DriveCD = SIID_DRIVECD,

   DriveRAM = SIID_DRIVERAM,

   World = SIID_WORLD,

   Server = SIID_SERVER,

   Printer = SIID_PRINTER,

   MyNetwork = SIID_MYNETWORK,

   Find = SIID_FIND,

   Help = SIID_HELP,

   Share = SIID_SHARE,

   Link = SIID_LINK,

   SlowFile = SIID_SLOWFILE,

   Recycler = SIID_RECYCLER,

   RecyclerFull = SIID_RECYCLERFULL,

   MediaCDAudio = SIID_MEDIACDAUDIO,

   Lock = SIID_LOCK,

   AutoList = SIID_AUTOLIST,

   PrinterNet = SIID_PRINTERNET,

   ServerShare = SIID_SERVERSHARE,

   PrinterFax = SIID_PRINTERFAX,

   PrinterFaxNet = SIID_PRINTERFAXNET,

   PrinterFile = SIID_PRINTERFILE,

   Stack = SIID_STACK,

   MediaSvcd = SIID_MEDIASVCD,

   StuffedFolder = SIID_STUFFEDFOLDER,

   DriveUnknown = SIID_DRIVEUNKNOWN,

   DriveDvd = SIID_DRIVEDVD,

   MediaDvd = SIID_MEDIADVD,

   MediaDvdRam = SIID_MEDIADVDRAM,

   MediaDvdRW = SIID_MEDIADVDRW,

   MediaDvdR = SIID_MEDIADVDR,

   MediaDvdRom = SIID_MEDIADVDROM,

   MediaCDAudioPlus = SIID_MEDIACDAUDIOPLUS,

   MediaCDRW = SIID_MEDIACDRW,

   MediaCDR = SIID_MEDIACDR,

   MediaCDBurn = SIID_MEDIACDBURN,

   MediaBlankCD = SIID_MEDIABLANKCD,

   MediaCDRom = SIID_MEDIACDROM,

   AudioFiles = SIID_AUDIOFILES,

   ImageFiles = SIID_IMAGEFILES,

   VideoFiles = SIID_VIDEOFILES,

   MixedFiles = SIID_MIXEDFILES,

   FolderBack = SIID_FOLDERBACK,

   FolderFront = SIID_FOLDERFRONT,

   Shield = SIID_SHIELD,

   Warning = SIID_WARNING,

   Info = SIID_INFO,

   Error = SIID_ERROR,

   Key = SIID_KEY,

   Software = SIID_SOFTWARE,

   Rename = SIID_RENAME,

   Delete = SIID_DELETE,

   MediaAudioDvd= SIID_MEDIAAUDIODVD,

   MediaMovieDvd = SIID_MEDIAMOVIEDVD,

   MediaEnhancedCD = SIID_MEDIAENHANCEDCD,

   MediaEnhancedDvd = SIID_MEDIAENHANCEDDVD,

   MediaHDDvd = SIID_MEDIAHDDVD,

   MediaBluRay = SIID_MEDIABLURAY,

   MediaVcd = SIID_MEDIAVCD,

   MediaDvdPlusR = SIID_MEDIADVDPLUSR,

   MediaDvdPlusRW = SIID_MEDIADVDPLUSRW,

   DesktopPC = SIID_DESKTOPPC,

   MobilePC = SIID_MOBILEPC,

   USERS = SIID_USERS,

   MediaSmartMedia = SIID_MEDIASMARTMEDIA,

   MediaCompactFlash = SIID_MEDIACOMPACTFLASH,

   DeviceCellPhone = SIID_DEVICECELLPHONE,

   DeviceCamera = SIID_DEVICECAMERA,

   DeviceVideoCamera = SIID_DEVICEVIDEOCAMERA,

   DeviceAudioPlayer = SIID_DEVICEAUDIOPLAYER,

   NetworkConnect = SIID_NETWORKCONNECT,

   Internet = SIID_INTERNET,

   ZipFile = SIID_ZIPFILE,

   Settings = SIID_SETTINGS,

};

public ref class StockIcons {

   public:

      static Icon ^ GetIcon(StockIconIdentifier identifier) { return GetIcon(identifier, static_cast<StockIconFlag>(0)); }

      static Icon ^ GetIcon(StockIconIdentifier identifier, StockIconFlag flags) {

         flags = flags | StockIconFlag::Handle;

         SHSTOCKICONINFO stockIconInfo = {0};

         stockIconInfo.cbSize = sizeof(stockIconInfo);

         HRESULT hResult = SHGetStockIconInfo(static_cast<SHSTOCKICONID>(identifier), static_cast<UINT>(flags), & stockIconInfo);

         if FAILED(hResult)

            throw gcnew Runtime::InteropServices::COMException("SHGetStockIconInfo execution failure", hResult);

         return Icon::FromHandle(static_cast<IntPtr>(stockIconInfo.hIcon));

      }

};

} }