首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >隐藏表单的DoubleBuffered属性而不使其非功能性

隐藏表单的DoubleBuffered属性而不使其非功能性
EN

Stack Overflow用户
提问于 2018-04-26 07:38:42
回答 1查看 157关注 0票数 1

使用Class,我试图将DoubleBuffered属性隐藏在窗体的属性窗口中,但不使其非功能性。所以我在下面的代码示例中做了这样的事情..。DoubleBuffered属性仍然出现。那么,我们真的可以隐藏DoubleBuffered属性吗?如果是,我们如何做到这一点?

代码语言:javascript
复制
Imports System.ComponentModel
Imports System.ComponentModel.Design

Public Class MyForm
    Inherits Form

    <Browsable(False)>
    Public Overloads Property DoubleBuffered As Boolean
        Get
            Return MyBase.DoubleBuffered
        End Get
        Set(ByVal value As Boolean)
            MyBase.DoubleBuffered = value
        End Set
    End Property

    Public Sub New()
        Me.DoubleBuffered = True
    End Sub

End Class
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-04-27 00:24:51

您可以为窗体创建自定义组件设计器,但仅重新创建不可访问的System.Windows.Forms.Design.FormDocumentDesigner的功能是一项艰巨的任务。更简单的方法是使用表单的Site属性(正如我之前向您展示的那样)来访问设计器服务。

在这种情况下,您需要覆盖设计器主机的ITypeDescriptorFilterService服务。此服务由设计器用于所有类型发现/筛选操作,而不限于特定组件。

第一步是创建一个实现ITypeDescriptorFilterService的类。以下是一个这样的实现。它是一个通用实现,允许它筛选指定类型的组件,并获取要从PropertyGrid显示中排除的属性名称列表。它需要的最后一项是对设计器主机使用的现有服务的引用。

代码语言:javascript
复制
Friend Class FilterService(Of T) : Implements ITypeDescriptorFilterService
    Private namesOfPropertiesToRemove As String()

    Public Sub New(baseService As ITypeDescriptorFilterService, ParamArray NamesOfPropertiesToRemove As String())
        Me.BaseService = baseService
        Me.namesOfPropertiesToRemove = NamesOfPropertiesToRemove
    End Sub

    Public ReadOnly Property BaseService As ITypeDescriptorFilterService
    Public Function FilterAttributes(component As IComponent, attributes As IDictionary) As Boolean Implements ITypeDescriptorFilterService.FilterAttributes
        Return BaseService.FilterAttributes(component, attributes)
    End Function

    Public Function FilterEvents(component As IComponent, events As IDictionary) As Boolean Implements ITypeDescriptorFilterService.FilterEvents
        Return BaseService.FilterEvents(component, events)
    End Function

    Public Function FilterProperties(component As IComponent, properties As IDictionary) As Boolean Implements ITypeDescriptorFilterService.FilterProperties
        ' ref: ITypeDescriptorFilterService Interface: https://msdn.microsoft.com/en-us/library/system.componentmodel.design.itypedescriptorfilterservice(v=vs.110).aspx
        ' 
        ' The return value of FilterProperties determines if this set of properties is fixed.
        ' If this method returns true, the TypeDescriptor for this component can cache the 
        ' results. This cache is maintained until either the component is garbage collected or the Refresh method of the type descriptor is called.

        ' allow other filters 1st chance to modify the properties collection
        Dim ret As Boolean = BaseService.FilterProperties(component, properties)

        ' only remove properties if component is of type T
        If TypeOf component Is T AndAlso Not (properties.IsFixedSize Or properties.IsReadOnly) Then
            For Each propName As String In namesOfPropertiesToRemove
                ' If the IDictionary object does not contain an element with the specified key, 
                ' the IDictionary remains unchanged. No exception is thrown.
                properties.Remove(propName)
            Next
        End If
        Return ret
    End Function
End Class

表单中的示例用法:

代码语言:javascript
复制
Imports System.ComponentModel
Imports System.ComponentModel.Design

Public Class TestForm : Inherits Form
    Private host As IDesignerHost
    Private altTypeDescriptorProvider As FilterService(Of TestForm)

    ' spelling and character casing of removedPropertyNames is critical
    ' it is a case-sensative lookup
    Private Shared removedPropertyNames As String() = {"DoubleBuffered"}

    Public Overrides Property Site As ISite
        Get
            Return MyBase.Site
        End Get
        Set(value As ISite)
            If host IsNot Nothing Then
                UnwireDesignerCode()
            End If

            MyBase.Site = value
            If value IsNot Nothing Then
                host = CType(Site.GetService(GetType(IDesignerHost)), IDesignerHost)
                If host IsNot Nothing Then
                    If host.Loading Then
                        AddHandler host.LoadComplete, AddressOf HostLoaded
                    Else
                        WireUpDesignerCode()
                    End If
                End If
            End If
        End Set
    End Property

    Private Sub HostLoaded(sender As Object, e As EventArgs)
        RemoveHandler host.LoadComplete, AddressOf HostLoaded
        WireUpDesignerCode()
    End Sub

    Private Sub WireUpDesignerCode()
        AddFilter()
    End Sub

    Private Sub UnwireDesignerCode()
        If host IsNot Nothing Then
            RemoveFilter()
        End If
        host = Nothing
    End Sub

    Private Sub AddFilter()
        Dim baseFilter As ITypeDescriptorFilterService = CType(host.GetService(GetType(ITypeDescriptorFilterService)), ITypeDescriptorFilterService)
        If baseFilter IsNot Nothing Then
            ' remove existing filter service
            host.RemoveService(GetType(ITypeDescriptorFilterService))
            ' create our replacement service and add it to the host's services
            altTypeDescriptorProvider = New FilterService(Of TestForm)(baseFilter, removedPropertyNames)
            host.AddService(GetType(ITypeDescriptorFilterService), altTypeDescriptorProvider)
            TypeDescriptor.Refresh(Me.GetType) ' force a type description rescan 
        End If
    End Sub

    Private Sub RemoveFilter()
        If altTypeDescriptorProvider IsNot Nothing Then
            host.RemoveService(GetType(ITypeDescriptorFilterService))
            host.AddService(GetType(ITypeDescriptorFilterService), altTypeDescriptorProvider.BaseService)
            altTypeDescriptorProvider = Nothing
        End If
    End Sub
End Class

现在,当您创建继承自TestForm的窗体时,DoubleBuffered属性将被排除在PropertyGrid显示之外。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50037383

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档