检查表单的图标是否为空或设置为默认图标?

时间:2018-05-15 11:05:56

标签: vb.net visual-studio-2017

任何想法如何检查表单的图标值是否为空或设置为默认值?我试过这样的事,但没有运气......

If Me.Icon Is Nothing Then
     MsgBox("Nothing")
End If

我注意到form的icon属性已经有了值。默认的一个。那么,如何检查我的表单是否使用此默认值?

提前谢谢!!!

1 个答案:

答案 0 :(得分:3)

似乎无法确定当前Icon是否为DefaultIcon。但是你可以通过反思访问static/shared property DefaultIcon

Imports System.Reflection
Imports System.Runtime.CompilerServices

Module FormsExtensions

    <Extension()>
    Public Function HasDefaultIcon(form As System.Windows.Forms.Form) As Boolean
        ' relies on reflection, so might break in future
        ' necessary because the DefaultIcon property is internal
        Dim fType = GetType(Windows.Forms.Form)
        Dim defaultIconProp = fType.GetProperty("DefaultIcon", BindingFlags.NonPublic Or BindingFlags.Static)
        Dim defaultIcon = TryCast(defaultIconProp?.GetValue(form), System.Drawing.Icon)
        Return form.Icon Is defaultIcon
    End Function

End Module

使用此扩展方法,检查很简单:

Dim hasDefaultIcon As Boolean = Me.HasDefaultIcon()