2009-07-17 21 views
3

私は、アプリケーションのすべてのフォーム(ツールバーのアイコン)上で共有したいイメージリストのインスタンスを1つ持っています。私は前に質問された質問を見て、人々はユーザコントロールを思いつきました(それは、それはイメージリストの複数のインスタンスを作成し、不必要なオブジェクトとオーバーヘッドを作成するため、良いことではありません)。.Net Winformsアプリケーションで共有イメージリスト

デザインタイムのサポートは良いでしょうが、それほど必須ではありません。

これは非常に簡単でした:データフォームを作成し、画像を共有してください。

C#/ Net/Winformsの亜種がありますか?

答えて

5

あなたは、単に静的クラスは、イメージリストのインスタンスを保持します、そして、あなたのアプリケーションでは、私が推測することを使用することができます

public static class ImageListWrapper 
{ 
    static ImageListWrapper() 
    { 
     ImageList = new ImageList(); 
     LoadImages(ImageList); 
    } 

    private static void LoadImages(ImageList imageList) 
    { 
     // load images into the list 
    } 

    public static ImageList ImageList { get; private set; } 
} 

次にあなたがホストされているイメージリストからイメージを読み込むことができます。

someControl.Image = ImageListWrapper.ImageList.Images["some_image"]; 

しかし、そのソリューションでは設計時間のサポートはありません。

3

soのようなシングルトンクラスを使用できます(下記参照)。デザイナを使用してイメージリストを作成し、手動で使用するイメージリストにバインドすることができます。


using System.Windows.Forms; 
using System.ComponentModel; 

//use like this.ImageList = StaticImageList.Instance.GlobalImageList 
//can use designer on this class but wouldn't want to drop it onto a design surface 
[ToolboxItem(false)] 
public class StaticImageList : Component 
{ 
    private ImageList globalImageList; 
    public ImageList GlobalImageList 
    { 
     get 
     { 
      return globalImageList; 
     } 
     set 
     { 
      globalImageList = value; 
     } 
    } 

    private IContainer components; 

    private static StaticImageList _instance; 
    public static StaticImageList Instance 
    { 
     get 
     { 
      if (_instance == null) _instance = new StaticImageList(); 
      return _instance; 
     } 
    } 

    private StaticImageList() 
     { 
     InitializeComponent(); 
     } 

    private void InitializeComponent() 
    { 
     this.components = new System.ComponentModel.Container(); 
     this.globalImageList = new System.Windows.Forms.ImageList(this.components); 
     // 
     // GlobalImageList 
     // 
     this.globalImageList.ColorDepth = System.Windows.Forms.ColorDepth.Depth32Bit; 
     this.globalImageList.ImageSize = new System.Drawing.Size(16, 16); 
     this.globalImageList.TransparentColor = System.Drawing.Color.Transparent; 
    } 
} 
関連する問題