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

查看:1012
本文介绍了在PropertyGrid中使用自定义颜色选择器对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In PropertyGrid default color picker dialog not allow to set alpha value of color.

我已经作出的我自己拾色器对话框并希望在PropertyGrid中使用它,但不知道如何做到这一点。

I already made my own color picker dialog and want to use it in PropertyGrid but not sure how to do it.

推荐答案

我设法使用属性网格,并在这里的情况下将其复制代码我的自定义拾色器对话框一些需要太:

I managed to use my custom color picker dialog in property grid and copying code of it here in case some need it too:

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



这是它的外观在属性网格:

And this is how it looks in property grid:

当我点击它的按钮,它会打开的自定义颜色对话框

When i click button of it, it will open custom color dialog.

不过还是有我解决不了一个问题。
我不能使用Color结构与此UITypeEditor的,因此创建RGBA类。
,当我使用的颜色结构,它是这样的:

But still have one problem which i can't solve. I can't use Color struct with this UITypeEditor, therefore created RGBA class. When i use color struct, it look like this:

我会打开它的另一个问题我猜:的自定义ColorEditor不正常的颜色结构

I will open another question for it i guess: Custom ColorEditor does not work properly on Color struct

这篇关于在PropertyGrid中使用自定义颜色选择器对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆