4

アプリケーションのすべてのフォーム (ツールバーのアイコン) で共有したい画像リストの 1 つのインスタンスが必要です。以前に尋ねられた質問を見たことがありますが、人々はユーザー コントロールを考え出しました (これは、イメージリストの複数のインスタンスを作成し、不要なオブジェクトとオーバーヘッドを作成するため、良くありません)。

設計時のサポートは優れていますが、それほど重要ではありません。

Delphi では、これは非常に簡単でした。DataForm を作成し、画像を共有すれば完了です。

その上にC#/.Net/Winformsのバリエーションはありますか?

4

2 に答える 2

6

静的クラスに ImageList インスタンスを保持させるだけで、それをアプリケーションで使用できると思います。

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; }
}

次に、ホストされている ImageList から画像をロードできます。

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

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

于 2009-07-17T09:18:43.033 に答える
3

そのようにシングルトンクラスを使用できます(以下を参照)。デザイナーを使用して画像リストにデータを入力し、手動で使用している画像リストにバインドできます。


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;
    }
}
于 2009-07-17T09:36:29.800 に答える