绘画时无法将标签粘贴到中心文本 [英] Cannot get label to center text when painted
问题描述
大家好......
我在表格上有一个标签控件,里面有不同大小的单词。
我只想让文本一直处于中间位置。
我将控件label1本身设置为textalign属性的中间位置控制。
如果这是正确的命令,我也会以编程方式尝试我不是百分之百确定。
我的问题是它总是涂成左对齐。
代码如下
谢谢
我尝试过:
Private Sub label1_Paint(ByVal sender As Object,ByVal e As System.Windows.Forms.PaintEventArgs)处理label1.Paint
label1.BringToFront()
label1.Text = label1.Text
e.Graphics.DrawString(label1.Text,label1.Font,Brushes.Yellow,RectangleF.op_Implicit(label1.ClientRectangle))
label1.Tex tAlign = ContentAlignment.MiddleCenter
End Sub
您好,
你不能使用ContentAlignment
和DrawString
,你需要创建一个StringFormat
对象然后应用这样的操作:
私人 Sub label1_Paint( ByVal sender As 对象, ByVal e As System.Windows.Forms .PaintEventArgs)句柄 label1.Paint
使用 sFormat As 新 StringFormat
sFormat.Alignment = StringAlignment.Center
sFormat.LineAlignment = StringAlignment.Ce nter
e.Graphics.DrawString(label1.Text,label1.Font,Brushes.Yellow,label1.ClientRectangle,sFormat)
End 使用
结束 Sub
我希望这会有所帮助,
基思
注意:我创建了此扩展程序以允许从ContentAlignment对象进行转换。
''' < 摘要 >
''' < span class =code-comment>使用StringFormat.GenericDefault将当前ContentAlignment转换为新的StringFormat
''' < / summary >
''' < 返回 > 具有当前对齐设置的新StringFormat.GenricDefault < / returns >
< Extension()>
公共 功能 ToStringFormat( ByRef cAlignment As ContentAlignment) As StringFormat
Dim rFormat As 新 StringFormat(StringFormat.GenericDefault)
使用 rFormat
选择 案例 cAlignment
案例 ContentAlignment.TopLeft,ContentAlignment.TopCenter,ContentAlignment.TopRight
.LineAlignment = StringAlignment.Near
案例 ContentAlignment.BottomLeft,ContentAlignment.BottomCenter,ContentAlignment.BottomRight
.LineAlignment = StringAlignment.Far
案例 其他
.LineAlignment = StringAlignment.Center
结束 选择
选择 案例 cAlignment
案例 ContentAlignment.TopLeft,ContentAlignment.MiddleLeft,ContentAlignment.BottomLeft
.Alignment = StringAlignment.Near
Case ContentAlignment.TopRight,ContentAlignment.MiddleRight,ContentAlignment.BottomRight
.Alignment = StringAlignment.Far
案例 其他
.Alignment = StringAlignment.Center
结束 选择
结束 使用
返回 rFormat
结束 功能
有了它你就可以打电话:
Dim MyAlignment as ContentAlignment = ContentAlignment.MiddleCenter
e.Graphics.DrawString(label1.Text,label1.Font,Brushes.Yellow,label1.ClientRectangle,MyAlignment.ToStringFormat)
Hi guys ...
I have a label control on a form that gets different sized words in it.
I just want to get the text to be middle center all the time.
I set the control label1 itself to middle center in the textalign property of the control.
I also tried programatically if it is the proper command I am not a hundred percent sure.
my prob is that it always paints left alligned.
the code is below
Thanks
What I have tried:
Private Sub label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles label1.Paint
label1.BringToFront()
label1.Text = label1.Text
e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Yellow, RectangleF.op_Implicit(label1.ClientRectangle))
label1.TextAlign = ContentAlignment.MiddleCenter
End Sub
Hello,
You cannot useContentAlignment
withDrawString
, you need to create aStringFormat
object and then apply the operation like so:
Private Sub label1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles label1.Paint Using sFormat As New StringFormat sFormat.Alignment = StringAlignment.Center sFormat.LineAlignment = StringAlignment.Center e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Yellow, label1.ClientRectangle, sFormat) End Using End Sub
I hope this helps,
Keith
NOTE: I created this extension to allow conversion from the ContentAlignment object.
''' <summary> ''' Converts current ContentAlignment to a new StringFormat using StringFormat.GenericDefault ''' </summary> ''' <returns>New StringFormat.GenricDefault with current alignment settings</returns> <Extension()> Public Function ToStringFormat(ByRef cAlignment As ContentAlignment) As StringFormat Dim rFormat As New StringFormat(StringFormat.GenericDefault) With rFormat Select Case cAlignment Case ContentAlignment.TopLeft, ContentAlignment.TopCenter, ContentAlignment.TopRight .LineAlignment = StringAlignment.Near Case ContentAlignment.BottomLeft, ContentAlignment.BottomCenter, ContentAlignment.BottomRight .LineAlignment = StringAlignment.Far Case Else .LineAlignment = StringAlignment.Center End Select Select Case cAlignment Case ContentAlignment.TopLeft, ContentAlignment.MiddleLeft, ContentAlignment.BottomLeft .Alignment = StringAlignment.Near Case ContentAlignment.TopRight, ContentAlignment.MiddleRight, ContentAlignment.BottomRight .Alignment = StringAlignment.Far Case Else .Alignment = StringAlignment.Center End Select End With Return rFormat End Function
With it you could just call:
Dim MyAlignment as ContentAlignment = ContentAlignment.MiddleCenter e.Graphics.DrawString(label1.Text, label1.Font, Brushes.Yellow, label1.ClientRectangle, MyAlignment.ToStringFormat)
这篇关于绘画时无法将标签粘贴到中心文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!