本文介绍了自定义ColorEditor不会对色彩结构正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里问如何使用自定义颜色对话框中的属性网格彩色结构:

I asked here how to use custom color dialog in property grid for Color struct:

从该链接就可以看到代码如果需要的话MyColorEditor类。
现在我可以使用自定义颜色对话框,但只有当我用我自己的结构是RGBA在这个例子。

From that link you can see code of MyColorEditor class if needed.Now i can use custom color dialog but only if i use my own struct which is RGBA in that example.

如果我用彩色此自定义类型编辑器结构它看起来像这样的属性网格:

If i use this custom type editor on color struct it looks like this in property grid:

但是,如果我用我创建RGBA结构,它看起来正确的:

But if i use RGBA struct which i created, it looks properly:

问题发生,因为UITypeEditorEditStyle.Modal不GetEditStyle()我想申请。

Problem happens because UITypeEditorEditStyle.Modal not applying with GetEditStyle() i think.

使用颜色结构可以比使用我的自定义颜色结构对我来说更好,因为那时我可以为颜色属性设置默认值,而不需要写我自己的类型转换器。

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.

所以我的问题是如何使用的颜色结构定义编辑器。

So my question is how to use custom editor on Color struct.

推荐答案

终于想通了如何做到这一点,ColorConverter是造成这个问题,所以需要用我自己的ColorConverter是这样的:

Finally figured out how to do it, ColorConverter was causing this problem so need to use my own ColorConverter like this:

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

和用法示例:

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



截图:

Screenshot:

这篇关于自定义ColorEditor不会对色彩结构正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 19:42