2011-07-12 104 views
2

我在GUI應用程序中的ImageList中有幾個圖標。我想從這個列表中設置通知圖標,但問題是它只接受圖標實例而不是圖像。如何將ImageList中的圖像轉換爲圖標類型?

System.Windows.Forms.NotifyIcon trayIcon = ...; 
System.Windows.Forms.ImageList notifierImageList = ...; 

trayIcon.Icon = notifierImageList.Images[0]; //This fails since no such cast exist 

謝謝。

回答

6

這裏有幾個選項。

  1. 不是將圖標存儲在ImageList中,而是可以將其存儲爲資源。然後從資源中構建一個Icon對象。

  2. 通過創建手柄將圖像轉換爲圖標。這個我在網上找到的。

    notifyIcon1.Icon = Icon.FromHandle(((Bitmap)imageList1.Images[0]).GetHicon());

+0

非常感謝! – Mehran

相關問題