/// <summary> /// Get the standard Windows icon. /// </summary> /// <param name="iconId">Type of icon to get</param> /// <returns>BitmapSource</returns> public static BitmapSource GetWindowsIcon(StockIconId iconId) { // Get handle of big icon. const StockIconFlags Flags = StockIconFlags.Large | StockIconFlags.Handle; var info = new StockIconInfo { cbSize = (uint)Marshal.SizeOf(typeof(StockIconInfo)) }; // Save bitmap source of icon. BitmapSource source = null; SHGetStockIconInfo(iconId, Flags, ref info); try { if (info.hIcon != IntPtr.Zero) { source = Imaging.CreateBitmapSourceFromHIcon(info.hIcon, Int32Rect.Empty, null); } } catch (Exception ex) { App.LogException(ex); } finally { DestroyIcon(info.hIcon); } return(source); }
/// <summary> /// Gets the stock icon with the given id and flags. /// </summary> /// <param name="stockIconId">The id of the icon to retrieve.</param> /// <param name="flags">Flag values indicating how to create the icon.</param> /// <returns>Returns an instance of <see cref="BitmapSource"/> containing the stock icon.</returns> private static BitmapSource GetBitmapSource(StockIconId stockIconId, StockIconFlags flags) { int hResult; BitmapSource bitmapSource; var stockIconInfo = StockIconInfo.Empty(); //Get the stock icon's handle. hResult = SHGetStockIconInfo((uint)stockIconId, 0x100 | (uint)flags, ref stockIconInfo); //If the icon retrieval failed, throw an error. if (hResult < 0) { throw new COMException("An error occurred when calling SHGetStockIconInfo.", hResult); } try { //Convert the handle into an instance of BitmapSource. bitmapSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(stockIconInfo.IconHandle, Int32Rect.Empty, null); } finally { //Don't forget to release the resources. DestroyIcon(stockIconInfo.IconHandle); } //Freeze the bitmap. bitmapSource.Freeze(); return(bitmapSource); }
//--------------------------------------------------------------------------------------------- // 指定のストックアイコンを取得する // id : アイコンの種類を指定する StockIconId enum 型 // flag : アイコンのサイズを指定する StockIconFlags enum 型 private BitmapSource GetStockIconById(StockIconId id, StockIconFlags flag) { BitmapSource bitmapSource = null; StockIconFlags flags = StockIconFlags.Handle | flag; var info = new StockIconInfo(); info.cbSize = (uint)Marshal.SizeOf(typeof(StockIconInfo)); IntPtr result = SHGetStockIconInfo(id, flags, ref info); if (info.hIcon != IntPtr.Zero) { bitmapSource = Imaging.CreateBitmapSourceFromHIcon(info.hIcon, Int32Rect.Empty, null); DestroyIcon(info.hIcon); } return(bitmapSource); }
//--------------------------------------------------------------------------------------------- // Image プロパティに基づいてアイコンを設定する private void SetupButtonImage() { StockIconId id = StockIconId.SIID_WARNING; switch (FImage) { case MessageBoxImage.Stop: // case MessageBoxImage.Hand: // case MessageBoxImage.Error: id = StockIconId.SIID_ERROR; break; case MessageBoxImage.Question: id = StockIconId.SIID_HELP; break; case MessageBoxImage.Exclamation: // case MessageBoxImage.Warning: id = StockIconId.SIID_WARNING; break; } PART_Image.Source = this.GetStockIconById(id, StockIconFlags.Large); }
private static extern IntPtr SHGetStockIconInfo( StockIconId siid, // 取得するアイコンの IDを指定する StockIconId enum 型 StockIconFlags uFlags, // 取得するアイコンの種類を指定する StockIconFlags enum 型 ref StockIconInfo psii //(戻り値)StockIconInfo 型 );
private static extern IntPtr SHGetStockIconInfo( StockIconId siid, // StockIconId enum type that specifies the ID of the icon to retrieve StockIconFlags uFlags, // StockIconFlags enum type that specifies the type of icon to retrieve ref StockIconInfo psii // (Return value) StockIconInfo type );
/// <summary> /// Gets the stock icon with the given id. /// </summary> /// <param name="stockIconId">The id of the icon to retrieve.</param> /// <returns>Returns an instance of <see cref="BitmapSource"/> containing the stock icon.</returns> private static BitmapSource GetBitmapSource(StockIconId stockIconId) { return(GetBitmapSource(stockIconId, 0)); }
public static int GetStockIconInfo(StockIconId stockIconId, GetStockIconInfoFlags flags, ref StockIconInfo stockIconInfo) { return(SHGetStockIconInfo(stockIconId, flags, stockIconInfo)); }
private static extern int SHGetStockIconInfo(StockIconId siid, GetStockIconInfoFlags uFlags, [In, Out] StockIconInfo psii);