質問

ユーザーがチャートの色を変更できるようにするために単純なプロパティグリッドを作成しようとしています。デフォルトでは、チャートには「ChartColorPalette」の「Palette」プロパティがあります。Property Gridの基礎となるオブジェクトには、同じタイプの「パレット」プロパティもあります。可能な値のドロップダウンリストを取得します。ただし、価値の名前の左側にある小さな縞模様のイメージがあります。

今、私はUityPeeDitor派生クラスを書くことができ、 "PaintValue"を描くことができます。

は、私が少しのビットマップを得るために使用できる「ChartColorpalette」列挙のタイプエディタがすでにあるかどうか知っていますか?

役に立ちましたか?

解決

起こったように、Uitypeeditorを書くことはそれほど難しいではなく、その多くのコードではありません。 まず、このように見たタイプエディタを作成しました。

private class ChartColorPaletteEditor : UITypeEditor
{
    public override bool GetPaintValueSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override void PaintValue(PaintValueEventArgs e)
    {
        String paletteName = e.Value.ToString();
        String baseName = this.GetType().Namespace + ".MyChart";
        ResourceManager mgr = new ResourceManager(baseName, this.GetType().Assembly);
        Bitmap bmp = mgr.GetObject(paletteName) as Bitmap;
        if (bmp != null)
        {
            e.Graphics.DrawImage(bmp, e.Bounds);
            bmp.Dispose();
        }
    }
}
.

私は通常の方法でこれを私の制御プロパティに添付しました:

[DefaultValue(typeof(ChartColorPalette), "BrightPastel")]
[Editor(typeof(ChartColorPaletteEditor), typeof(System.Drawing.Design.UITypeEditor))]
[Category("Appearance")]
[Description("The named palette to use when choosing the colour scheme for the chart series lines.")]
public ChartColorPalette Palette { get; set; }
.

その後、各小型パレット画像に対して小さなPNGリソースを追加しました。私は "Chart"から継承した "MyChart"を派生させ、そのイメージをリソースとして追加しました(必ず "persistance"プロパティを "persistance"プロパティを "persistance"プロパティを "埋め込み"に設定して "PNGファイルを保存します) 。 PNGファイルの名前は、ChartColorPalette列挙体の名前と一致しました。

唯一の問題は、から小さな20 x 14画像を取得する場所でした。私はもともとpaint.exeを使ってそれらを淘汰しましたが、それを好きではなかったので、私は私のためにそれらを生成するためのいくつかのコードを書いた。それはかなり単純でしたが、チャート作成コントロールによって使用されるカラー値を見つけたら。 1つの微妙なことは、パレット内に12色以上があるところで、少しビットマップは他のすべての色を使用します。そのコードは次のように見えました:

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Reflection;
using System.Windows.Forms.DataVisualization.Charting;

namespace ConsoleApplication10
{
class Program
{
  static void Main(string[] args)
  {
     Enum.GetValues(typeof(ChartColorPalette)).OfType<ChartColorPalette>().ToList().ForEach(GeneratePNG);
  }

  static void GeneratePNG(ChartColorPalette palette)
  {
     if (palette == ChartColorPalette.None) return;

     Color[] colours = palette.GetColors();

     if (colours.Length >= 12)
     {
        colours = new Color[] { colours[0], colours[2], colours[4], colours[6], colours[8], colours[10] };
     }
     else
     {
        colours = new Color[] { colours[0], colours[1], colours[2], colours[3], colours[4], colours[5] };
     }

     using (Bitmap bmp = new Bitmap(20, 14))
     {
        using (Graphics gr = Graphics.FromImage(bmp))
        {
           using (SolidBrush b1 = new SolidBrush(colours[0]),
                             b2 = new SolidBrush(colours[1]),
                             b3 = new SolidBrush(colours[2]),
                             b4 = new SolidBrush(colours[3]),
                             b5 = new SolidBrush(colours[4]),
                             b6 = new SolidBrush(colours[5]))
           {
              int height = bmp.Height - 2;
              gr.DrawRectangle(Pens.Black, 0, 0, bmp.Width - 1, bmp.Height - 1);
              gr.FillRectangle(b1, new Rectangle(1, 1, 3, height));
              gr.FillRectangle(b2, new Rectangle(4, 1, 3, height));
              gr.FillRectangle(b3, new Rectangle(7, 1, 3, height));
              gr.FillRectangle(b4, new Rectangle(10, 1, 3, height));
              gr.FillRectangle(b5, new Rectangle(13, 1, 3, height));
              gr.FillRectangle(b6, new Rectangle(16, 1, 3, height));
           }
        }

        String path = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        path = Path.Combine(path, @"Visual Studio 2010\Projects\DataVisualization.Charting\Palette Bitmaps");

        String filename = palette.ToString() + ".png";
        bmp.Save(Path.Combine(path, filename), ImageFormat.Png);
     }
  }
}

public static class Extensions
{
  public static Color[] GetColors(this ChartColorPalette value)
  {
     switch (value)
     {
        case ChartColorPalette.Berry:
           return GetColors(0x8a2be2, 0xba55d3, 0x4169e1, 0xc71585, 0x0000ff, 0x8a2be2, 0xda70d6, 0x7b68ee, 0xc000c0, 0x0000cd, 0x800080);
        case ChartColorPalette.Bright:
           return GetColors(0x008000, 0x0000ff, 0x800080, 0x00ff00, 0xff00ff, 0x008080, 0xffff00, 0x808080, 0x00ffff, 0x000080, 0x800000, 0xff0000, 0x808000, 0xc0c0c0, 0xff6347, 0xffe4b5);
        case ChartColorPalette.BrightPastel:
           return GetColors(0x418cf0, 0xfcb441, 0xe0400a, 0x056492, 0xbfbfbf, 0x1a3b69, 0xffe382, 0x129cdd, 0xca6b4b, 0x005cdb, 0xf3d288, 0x506381, 0xf1b9a8, 0xe0830a, 0x7893be);
        case ChartColorPalette.Chocolate:
           return GetColors(0xa0522d, 0xd2691e, 0x8b0000, 0xcd853f, 0xa52a2a, 0xf4a460, 0x8b4513, 0xc04000, 0xb22222, 0xb65c3a);
        case ChartColorPalette.EarthTones:
           return GetColors(0xff8000, 0xb8860b, 0xc04000, 0x6b8e23, 0xcd853f, 0xc0c000, 0x228b22, 0xd2691e, 0x808000, 0x20b2aa, 0xf4a460, 0x00c000, 0x8fbc8b, 0xb22222, 0x8b4513, 0xc00000);
        case ChartColorPalette.Excel:
           return GetColors(0x9999ff, 0x993366, 0xffffcc, 0xccffff, 0x660066, 0xff8080, 0x0066cc, 0xccccff, 0x000080, 0xff00ff, 0xffff00, 0x00ffff, 0x800080, 0x800000, 0x008080, 0x0000ff);
        case ChartColorPalette.Fire:
           return GetColors(0xffd700, 0xff0000, 0xff1493, 0xdc143c, 0xff8c00, 0xff00ff, 0xffff00, 0xff4500, 0xc71585, 0xdde221);
        case ChartColorPalette.Grayscale:
           return GetColors(0xc8c8c8, 0xbdbdbd, 0xb2b2b2, 0xa7a7a7, 0x9c9c9c, 0x919191, 0x868686, 0x7b7b7b, 0x707070, 0x656565, 0x5a5a5a, 0x4f4f4f, 0x444444, 0x393939, 0x2e2e2e, 0x232323);
        case ChartColorPalette.Light:
           return GetColors(0xe6e6fa, 0xfff0f5, 0xffdab9, 0xfffacd, 0xffe4e1, 0xf0fff0, 0xf0f8ff, 0xf5f5f5, 0xfaebd7, 0xe0ffff);
        case ChartColorPalette.Pastel:
           return GetColors(0x87ceeb, 0x32cd32, 0xba55d3, 0xf08080, 0x4682b4, 0x9acd32, 0x40e0d0, 0xff69b4, 0xf0e68c, 0xd2b48c, 0x8fbc8b, 0x6495ed, 0xdda0dd, 0x5f9ea0, 0xffdab9, 0xffa07a);
        case ChartColorPalette.SeaGreen:
           return GetColors(0x2e8b57, 0x66cdaa, 0x4682b4, 0x008b8b, 0x5f9ea0, 0x3cb371, 0x48d1cc, 0xb0c4de, 0xffffff, 0x87ceeb);
        case ChartColorPalette.SemiTransparent:
           return GetColors(0xff6969, 0x69ff69, 0x6969ff, 0xffff69, 0x69ffff, 0xff69ff, 0xcdb075, 0xffafaf, 0xafffaf, 0xafafff, 0xffffaf, 0xafffff, 0xffafff, 0xe4d5b5, 0xa4b086, 0x819ec1);
        case ChartColorPalette.None:
        default:
           return GetColors(0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000);
     }
  }

  private static Color[] GetColors(params Int32[] values)
  {
     return values.Select(value => Color.FromArgb(255, Color.FromArgb(value))).ToArray(); // alpha channel of 255 for fully opaque
  }
 }
}
.

これが誰かにそこに役立つことを願っています...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top