none
PropertyGrid 打开自定义编辑窗口 RRS feed

  • 问题

  •  以下代码,在PropertyGrid中点击BackBitmap属性可以打开选择图片的窗口,选择图片文件后,属性BackBitmap可以得到图片,但是和PropertyGrid默认的不同,BackBitmap属性没有了那个默认的打开图片的界面小图标,还要怎么设置,可以看到那个BackBitmap属性旁边的小图片

    Protected _BackBitmap As Drawing.Bitmap = Nothing
        ''' <summary>
        ''' 获取或设置背景图片
        ''' </summary>
        ''' <value></value>
        ''' <returns></returns>
        ''' <remarks></remarks>
        <Browsable(True), DefaultValue(GetType(Drawing.Bitmap), "Nothing"), Category("外观"),
        Description("获取或设置背景图片"), EditorAttribute(GetType(GetFileEditor), GetType(System.Drawing.Design.UITypeEditor))>
        Public Property BackBitmap() As Drawing.Bitmap
            Get
                Return _BackBitmap
            End Get
            Set(ByVal Value As Drawing.Bitmap)
                _BackBitmap=Value
            End Set
        End Property

    ''' <summary>
        ''' 自定义属性窗口编辑器
        ''' </summary>
        ''' <remarks></remarks>
        Public Class GetFileEditor
            Inherits UITypeEditor

            Public Overloads Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As UITypeEditorEditStyle
                Return UITypeEditorEditStyle.Modal
            End Function

            Public Overloads Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
                Dim edSvc As Windows.Forms.Design.IWindowsFormsEditorService = DirectCast(provider.GetService(GetType(Windows.Forms.Design.IWindowsFormsEditorService)), Windows.Forms.Design.IWindowsFormsEditorService)
                If edSvc IsNot Nothing Then
                    ' 可以打开任何特定的对话框
                    Dim dialog As New OpenFileDialog()
                    dialog.AddExtension = False
                    If dialog.ShowDialog().Equals(DialogResult.OK) Then
                        Return New Bitmap(dialog.FileName) 'dialog.FileName
                    End If
                End If
                Return value
            End Function

        End Class

    2015年2月28日 13:28

全部回复

  • 自己解决了,重写下面2个方法就可以了,就是不知道这个图标的尺寸是多少,自己测量一下手动输入e.Graphics.DrawImage(image, New Drawing.Rectangle(2, 2, 18, 12)),有没有其它属性可读取


      ''' <summary>
            ''' 返回真,才可在属性边加图标
            ''' </summary>
            ''' <param name="context"></param>
            ''' <returns></returns>
            ''' <remarks></remarks>
            Public Overrides Function GetPaintValueSupported(context As System.ComponentModel.ITypeDescriptorContext) As Boolean
                Return True
            End Function

            ''' <summary>
            ''' 返回显示的图标
            ''' </summary>
            ''' <param name="e"></param>
            ''' <remarks></remarks>
            Public Overrides Sub PaintValue(e As PaintValueEventArgs)
                If image IsNot Nothing Then
                    e.Graphics.DrawImage(image, New Drawing.Rectangle(2, 2, 18, 12))
                End If
            End Sub


    • 已编辑 raxhdq 2015年2月28日 23:32
    2015年2月28日 23:30
  • 自己解决,e.Bounds属性

    2015年3月1日 6:27
  • 感谢分享经验!

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年3月2日 2:31
    版主