简体   繁体   English

自定义ColorEditor在Color结构上无法正常工作

[英]Custom ColorEditor does not work properly on Color struct

I asked here how to use custom color dialog in property grid for Color struct: 我在这里问如何在“颜色”结构的属性网格中使用自定义颜色对话框:

Using custom color picker dialog in PropertyGrid 在PropertyGrid中使用自定义颜色选择器对话框

From that link you can see code of MyColorEditor class if needed. 如果需要,从该链接可以看到MyColorEditor类的代码。 Now i can use custom color dialog but only if i use my own struct which is RGBA in that example. 现在,我可以使用自定义颜色对话框,但前提是我要使用自己的结构(在该示例中为RGBA)。

If i use this custom type editor on color struct it looks like this in property grid: 如果我在颜色结构上使用此自定义类型编辑器,则它在属性网格中如下所示:

screenshot1

But if i use RGBA struct which i created, it looks properly: 但是,如果我使用我创建的RGBA结构,它看起来正确:

screenshot2

Problem happens because UITypeEditorEditStyle.Modal not applying with GetEditStyle() i think. 问题发生是因为我认为UITypeEditorEditStyle.Modal无法与GetEditStyle()一起应用。

Using Color struct can be better than using my custom color struct for me because then i can set DefaultValue for Color property without requiring to write my own type converter. 使用Color结构可能比使用我的自定义颜色结构更好,因为这样我就可以为Color属性设置DefaultValue,而无需编写自己的类型转换器。

So my question is how to use custom editor on Color struct. 所以我的问题是如何在Color结构上使用自定义编辑器。

Finally figured out how to do it, ColorConverter was causing this problem so need to use my own ColorConverter like this: 终于弄清楚了怎么做,ColorConverter导致了这个问题,因此需要像这样使用我自己的ColorConverter:

public class MyColorConverter : ColorConverter
{
    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return false;
    }
}

Editor codes: 编辑器代码:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace HelpersLib
{
    public class MyColorEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.Modal;
        }

        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (value.GetType() != typeof(Color))
            {
                return value;
            }

            IWindowsFormsEditorService svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

            if (svc != null)
            {
                Color color = (Color)value;

                using (DialogColor form = new DialogColor(color))
                {
                    if (svc.ShowDialog(form) == DialogResult.OK)
                    {
                        return (Color)form.NewColor;
                    }
                }
            }

            return value;
        }

        public override bool GetPaintValueSupported(ITypeDescriptorContext context)
        {
            return true;
        }

        public override void PaintValue(PaintValueEventArgs e)
        {
            Graphics g = e.Graphics;
            Color color = (Color)e.Value;

            if (color.A < 255)
            {
                using (Image checker = ImageHelpers.CreateCheckers(e.Bounds.Width / 2, e.Bounds.Height / 2, Color.LightGray, Color.White))
                {
                    g.DrawImage(checker, e.Bounds);
                }
            }

            using (SolidBrush brush = new SolidBrush(color))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }

            e.Graphics.DrawRectangleProper(Pens.Black, e.Bounds);
        }
    }
}

And example usage: 用法示例:

    [DefaultValue(typeof(Color), "Black"),
    Editor(typeof(MyColorEditor), typeof(UITypeEditor)),
    TypeConverter(typeof(MyColorConverter))]
    public Color Color { get; set; }

Screenshot: 截图:

截图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM