【问题标题】:PropertyGrid - get chosen bitmap's file pathPropertyGrid - 获取所选位图的文件路径
【发布时间】:2013-01-03 20:10:10
【问题描述】:

我将PropertyGridSelectedObject 设置为具有Bitmap 属性的对象:

public class Obj
{   
    private Bitmap myimage;
    [Category("Main Category")]
    [Description("Your favorite image")]
    public Bitmap MyImage
    {
        get { return myimage; }
        set
        {
            myimage = value;
        }
    }
}

这会自动在PropertyGrid 中显示属性,并带有一个小按钮,用户可以单击该小按钮来选择图像。如何获取这张图片的文件路径?

(另外,有什么方法可以覆盖弹出的对话框并放入我自己的对话框?我知道这是一个单独的问题,所以我可能会单独发布。)

【问题讨论】:

    标签: c# .net winforms bitmap propertygrid


    【解决方案1】:

    如何获取此图片的文件路径? 另外,有什么办法可以覆盖弹出的对话框并放入我自己的对话框?

    要实现这两种方法,一种方法是创建一个新的UITypeEditor

    public class BitmapLocationEditor : UITypeEditor
    {
    
    }
    

    我重写了GetEditStyleEditValueGetPaintvalueSupportedPaintValue 方法。

    // Displays an ellipsis (...) button to start a modal dialog box
    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
    {
        return UITypeEditorEditStyle.Modal;
    }
    
    // Edits the value of the specified object using the editor style indicated by the GetEditStyle method.
    public override object EditValue(ITypeDescriptorContext context,
                                     IServiceProvider provider,
                                     object value)
    {
        // Show the dialog we use to open the file.
        // You could use a custom one at this point to provide the file path and the image.
        using (OpenFileDialog openFileDialog = new OpenFileDialog())
        {
            openFileDialog.Filter = "Image files (*.bmp | *.bmp;";
    
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Bitmap bitmap = new Bitmap(openFileDialog.FileName);
                bitmap.SetPath(openFileDialog.FileName);
    
                return bitmap;
            }
        }
    
        return value;
    }
    
    // Indicates whether the specified context supports painting
    // a representation of an object's value within the specified context.
    public override bool GetPaintValueSupported(ITypeDescriptorContext context)
    {
        return true;
    }
    
    // Paints a representation of the value of an object using the specified PaintValueEventArgs.
    public override void PaintValue(PaintValueEventArgs e)
    {
        if (e.Value != null)
        {
            // Get image
            Bitmap bmp = (Bitmap)e.Value;
    
            // This rectangle indicates the area of the Properties window to draw a representation of the value within.
            Rectangle destRect = e.Bounds;
    
            // Optional to set the default transparent color transparent for this image.
            bmp.MakeTransparent();
    
            // Draw image
            e.Graphics.DrawImage(bmp, destRect);
        }
    }
    

    您的Obj 类应该保持不变,除了添加一个属性以利用我们新的BitmapLocationEditor 类:

    public class Obj
    {
        private Bitmap myImage;
    
        [Category("Main Category")]
        [Description("Your favorite image")]
        [Editor(typeof(BitmapLocationEditor), typeof(UITypeEditor))]
        public Bitmap MyImage
        {
            get { return myImage; }
            set
            {
                myImage = value;
            }
        }
    }
    

    EditValue 方法中使用的SetPath 方法只是设置Bitmap.Tag 属性的扩展方法。您可以稍后使用GetPath 方法来检索图像文件的实际路径。

    public static class BitmapExtension
    {
        public static void SetPath(this Bitmap bitmap, string path)
        {
            bitmap.Tag = path;
        }
    
        public static string GetPath(this Bitmap bitmap)
        {
            return bitmap.Tag as string;
        }
    }
    

    Article about PropertyGrid

    【讨论】:

    • 尽管我在一月份确实需要这个答案,但我仍然非常感谢这个答案。顺便说一句,如果你想知道我需要这个做什么:www.jutanium.com/maze
    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 2013-12-17
    • 2015-09-13
    • 2021-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多