问个VB.net的label控件的问题

yanchangshan 2013-01-10 09:23:29
请教个label控件的问题

怎样让label控件固定在窗口中部

就是我我窗体无论怎么放大缩小 label控件始终在窗体的水平中间

而且label控件也随着字体的变大自己也在变大
...全文
428 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sqldy313313 2013-04-17
  • 打赏
  • 举报
回复
很好,很有帮助。
yanchangshan 2013-01-10
  • 打赏
  • 举报
回复
Label1.Location = New Drawing.Point((Me.Width - Label1.Width) / 2, Label1.Top) 用这个就可以了 谢谢 结贴
hztltgg 2013-01-10
  • 打赏
  • 举报
回复
涉及到不按照等比例缩放的问题,你这儿要求不知道是什么,下面的代码根据高度的变化缩放,可以先讲标签的字体设置的稍微大点测试。

Public Class Form1

    Dim labsize As Single
    Dim formsize As Size

    Private Sub Form1_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize

        Dim hc = Me.Height / formsize.Height
        Dim wc = Me.Width / formsize.Width

        Dim p As Double

        ' If hc < wc Then
        p = labsize * hc
        ' Else
        ' p = labsize * wc
        ' End If

        Dim font As New Font(Label1.Font.Name, CSng(p), GraphicsUnit.Point)
        Label1.Font = font

        Label1.Location = New Point((Me.ClientSize.Width - Label1.Width) \ 2, (Me.ClientSize.Height - Label1.Height) \ 2)

    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        formsize = Me.Size
        labsize = Label1.Font.Size

    End Sub

End Class
gxingmin 2013-01-10
  • 打赏
  • 举报
回复
引用 2 楼 gxingmin 的回复:
Visual Basic code?123456789101112131415161718192021Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Label1.AutoSize = True ……
只是水平居中的话,把 Label1.Location = New Drawing.Point((Me.Width - Label1.Width) / 2, (Me.Height - Label1.Height) / 2) 改成下面这句即可 Label1.Location = New Drawing.Point((Me.Width - Label1.Width) / 2, Label1.Top)
51Crack 2013-01-10
  • 打赏
  • 举报
回复
Label1.Anchor = AnchorStyles.None
gxingmin 2013-01-10
  • 打赏
  • 举报
回复

Public Class Form1

  
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label1.AutoSize = True
        Size_Changed(sender, e)

        '这一句可以不要
        'AddHandler Label1.FontChanged, AddressOf Size_Changed
        AddHandler Label1.SizeChanged, AddressOf Size_Changed
        AddHandler Label1.TextChanged, AddressOf Size_Changed
        AddHandler Me.SizeChanged, AddressOf Size_Changed

    End Sub

    Private Sub Size_Changed(sender As Object, e As EventArgs)
        Label1.Location = New Drawing.Point((Me.Width - Label1.Width) / 2, (Me.Height - Label1.Height) / 2)
    End Sub

   
End Class
hztltgg 2013-01-10
  • 打赏
  • 举报
回复
难点是字体大小不能根据标签大小改变,他们单位不同,标签大小倒是可以根据字体大小改变。

16,722

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧