2010-12-06 124 views
2

我目前正在開發c#應用程序。我正在製作自己的自定義消息框,所以我想知道是否可以爲標籤指定一個默認系統圖標(即您將在標準MessageBox上看到的圖標之一)。將系統圖標分配給標籤

任何有關此事的幫助將不勝感激。

感謝

回答

4

您可以System.Drawing中和System.Windows.Forms的互操作:

System.Drawing.Icon icon = System.Drawing.SystemIcons.Warning; 

BitmapSource bs = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 

image1.Source = bs; 
+0

很好的建議。 「image1.Source should = bs」可能是「this.Icon = bs」 – 2010-12-06 01:14:27

1

SLaks的解決方案可能是要走的最簡單的方法。如果由於某種原因,你不想使用Windows窗體的功能,這是很容易實現自己:

public enum SystemIcons 
{ 
    Application = 32512, 
    Error = 32513, 
    Hand = Error, 
    Question = 32514, 
    Warning = 32515, 
    Exclamation = Warning, 
    Information = 32516, 
    Asterisk = Information, 
    WinLogo = 32517, 
    Shield = 32518, 
} 

public static ImageSource LoadSystemIcon(SystemIcons iconId) 
{ 
    string iconName = "#" + ((int)iconId); 
    IntPtr hIcon = LoadIcon(IntPtr.Zero, iconName); 
    if (hIcon == IntPtr.Zero) 
     return null; 

    return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(
     hIcon, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); 
} 

[DllImport("user32.dll")] 
static extern IntPtr LoadIcon(IntPtr hInstance, string lpIconName); 
6
System.Drawing.Icon myIcon = new System.Drawing.Icon(System.Drawing.SystemIcons.Question,32,32); 
label.Image = myIcon.ToBitmap();