2

在 PropertyGrid默认颜色选择器对话框中不允许设置颜色的 alpha 值。

我已经制作了自己的颜色选择器对话框,并想在 PropertyGrid 中使用它,但不知道该怎么做。

4

2 回答 2

3

我设法在属性网格中使用我的自定义颜色选择器对话框并在此处复制它的代码,以防有些人也需要它:

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(RGBA))
            {
                return value;
            }

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

            if (svc != null)
            {
                using (DialogColor form = new DialogColor((RGBA)value))
                {
                    if (svc.ShowDialog(form) == DialogResult.OK)
                    {
                        return form.NewColor.RGBA;
                    }
                }
            }

            return value;
        }

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

        public override void PaintValue(PaintValueEventArgs e)
        {
            using (SolidBrush brush = new SolidBrush((RGBA)e.Value))
            {
                e.Graphics.FillRectangle(brush, e.Bounds);
            }

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

这就是它在属性网格中的样子:

截图1

当我单击它的按钮时,它将打开自定义颜色对话框

但是仍然有一个我无法解决的问题。我不能在这个 UITypeEditor 中使用 Color 结构,因此创建了 RGBA 类。当我使用颜色结构时,它看起来像这样:

截图2

我想我会为它打开另一个问题:Custom ColorEditor does not work proper on Color struct

于 2013-11-10T03:19:20.083 回答
1

要与之交互PropertyGrid,您必须创建自己的“属性类”(如此处所述)。您可以自定义不同的部分,因此有多种解决方案可以满足您的需求。作为解决问题的第一种方法,这里有一个代码propertyGrid1

Property curProperty = new Property();
propertyGrid1.SelectedObject = curProperty;

其中Property定义为:

public class Property
{
    private ColorDialog _dialog = new customColorDialogDialog();

    public ColorDialog dialog
    {
        get { return _dialog; }
        set { _dialog.ShowDialog(); }
    }
}
class customColorDialogDialog : ColorDialog
{

}

customColorDialogDialog在此代码中,当单击属性名称(“对话框”)右侧的单元格时,将触发您的颜色对话框 ( )。

于 2013-10-18T09:54:11.140 回答