Vb.net 如何将TextFormatFlags用作继承控件的属性?
我已经在Vb.net 如何将TextFormatFlags用作继承控件的属性?,vb.net,Vb.net,我已经在类中创建了一个自定义OwnerDraw继承的ToolTip控件,并且还添加了一些自定义属性。我的textrender如下所示: TextRenderer.DrawText(e.Graphics, e.ToolTipText, Me.Font, e.Bounds, Me.ForeColor, Me.BackColor, TextFormatFlags.HorizontalCenter Or TextFo
类中创建了一个自定义OwnerDraw
继承的ToolTip
控件,并且还添加了一些自定义属性。我的textrender
如下所示:
TextRenderer.DrawText(e.Graphics, e.ToolTipText, Me.Font, e.Bounds,
Me.ForeColor, Me.BackColor,
TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
Me.Form_CustomToolTip.AlignHorizontal = System.Windows.Forms.TextFormatFlags.Left, Default, Top, HorizontalCenter
Me.Form_CustomToolTip.AlignVertical = System.Windows.Forms.TextFormatFlags.Left, Default, Top, VerticalCenter
我想使TextFormatFlags.HorizontalCenter
和TextFormatFlags.VerticalCenter
来自一个自定义属性,所以我这样做了:
Private _AlignVertical As TextFormatFlags
<Category("Custom Properties")> <DisplayName("Align Vertical")> <Description("ToolTip's text vertical align.")>
Public Property AlignVertical As TextFormatFlags
Get
Return _AlignVertical
End Get
Set(ByVal Value As TextFormatFlags)
_AlignVertical = Value
End Set
End Property
Private _AlignHorizontal As TextFormatFlags
<Category("Custom Properties")> <DisplayName("Align Horizontal")> <Description("ToolTip's text horizontal align.")>
Public Property AlignHorizontal As TextFormatFlags
Get
Return _AlignHorizontal
End Get
Set(ByVal Value As TextFormatFlags)
_AlignHorizontal = Value
End Set
End Property
我还将其添加到了我的Public Sub New()
:
我的问题是,当我进入属性面板,进入AlignHorizontal
和AlignVertical
时,我看到Left、Default、Top、HorizontalCenter
和Left、Default、Top、VerticalCenter
作为值,而不是我认为应该的HorizontalCenter
和VerticalCenter
。另外,在我的表单
sDesigner
中,我得到了错误,因为这些值如下所示:
TextRenderer.DrawText(e.Graphics, e.ToolTipText, Me.Font, e.Bounds,
Me.ForeColor, Me.BackColor,
TextFormatFlags.HorizontalCenter Or TextFormatFlags.VerticalCenter)
Me.Form_CustomToolTip.AlignHorizontal = System.Windows.Forms.TextFormatFlags.Left, Default, Top, HorizontalCenter
Me.Form_CustomToolTip.AlignVertical = System.Windows.Forms.TextFormatFlags.Left, Default, Top, VerticalCenter
Α任何形式的指导都将不胜感激 根据我的知识,我用下面这个简单的方法实现了它。如果有人有更好或更合适的东西
首先,我将TextFormatFlags
属性替换为ContentAlignment
:
Private _TextAlign As ContentAlignment
<Category("Custom Properties")> <DisplayName("Text Align")> <Description("The alignment of text in the ToolTip.")>
Public Property TextAlign As ContentAlignment
Get
Return _TextAlign
End Get
Set(ByVal Value As ContentAlignment)
_TextAlign = Value
End Set
End Property
Dim _Flags As TextFormatFlags
If Me.TextAlign = ContentAlignment.BottomCenter Then
_Flags = TextFormatFlags.Bottom Or TextFormatFlags.HorizontalCenter
ElseIf Me.TextAlign = ContentAlignment.BottomLeft Then
_Flags = TextFormatFlags.Bottom Or TextFormatFlags.Left
ElseIf Me.TextAlign = ContentAlignment.BottomRight Then
_Flags = TextFormatFlags.Bottom Or TextFormatFlags.Right
ElseIf Me.TextAlign = ContentAlignment.MiddleCenter Then
_Flags = TextFormatFlags.VerticalCenter Or TextFormatFlags.HorizontalCenter
ElseIf Me.TextAlign = ContentAlignment.MiddleLeft Then
_Flags = TextFormatFlags.VerticalCenter Or TextFormatFlags.Left
ElseIf Me.TextAlign = ContentAlignment.MiddleRight Then
_Flags = TextFormatFlags.VerticalCenter Or TextFormatFlags.Right
ElseIf Me.TextAlign = ContentAlignment.TopCenter Then
_Flags = TextFormatFlags.Top Or TextFormatFlags.HorizontalCenter
ElseIf Me.TextAlign = ContentAlignment.TopLeft Then
_Flags = TextFormatFlags.Top Or TextFormatFlags.Left
ElseIf Me.TextAlign = ContentAlignment.TopRight Then
_Flags = TextFormatFlags.Top Or TextFormatFlags.Right
End If
TextRenderer.DrawText(e.Graphics, e.ToolTipText, Me.Font, e.Bounds, Me.ForeColor, Me.BackColor, _Flags)
e.Graphics.Dispose()
对于C#开发者来说,他们来到这里,欣赏希腊文Simonetos的答案,就像我一样,这里是标志分配的C#翻译:
switch (TextAlign)
{
case ContentAlignment.BottomCenter:
return TextFormatFlags.Bottom | TextFormatFlags.HorizontalCenter;
case ContentAlignment.BottomLeft:
return TextFormatFlags.Bottom | TextFormatFlags.Left;
case ContentAlignment.BottomRight:
return TextFormatFlags.Bottom | TextFormatFlags.Right;
case ContentAlignment.MiddleCenter:
return TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter;
case ContentAlignment.MiddleLeft:
return TextFormatFlags.VerticalCenter | TextFormatFlags.Left;
case ContentAlignment.MiddleRight:
return TextFormatFlags.VerticalCenter | TextFormatFlags.Right;
case ContentAlignment.TopCenter:
return TextFormatFlags.Top | TextFormatFlags.HorizontalCenter;
case ContentAlignment.TopLeft:
return TextFormatFlags.Top | TextFormatFlags.Left;
case ContentAlignment.TopRight:
return TextFormatFlags.Top | TextFormatFlags.Right;
default:
return TextFormatFlags.Default;
}
这种枚举类型在设计器中不起作用。它充当位掩码,而不是值列表。以友好且合乎逻辑的方式执行此操作,将这些属性类型更改为Boolean。并编写一个返回TextFormatFlags的小助手方法,使用这些属性计算值。您还需要在属性设置器中调用Me.Invalidate(),以便重新绘制文本。