加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

vb.net – 屏幕上或父级上的中心表单

发布时间:2020-12-17 00:00:20 所属栏目:大数据 来源:网络整理
导读:由于在VB.NET中定位表单的内置功能并不总是适合使用我尝试让我的sub做到这一点. 但我错过了一些东西…… Public Sub form_center(ByVal frm As Form,Optional ByVal parent As Form = Nothing) Dim x As Integer Dim y As Integer Dim r As Rectangle If Not
由于在VB.NET中定位表单的内置功能并不总是适合使用我尝试让我的sub做到这一点.

但我错过了一些东西……

Public Sub form_center(ByVal frm As Form,Optional ByVal parent As Form = Nothing)

    Dim x As Integer
    Dim y As Integer
    Dim r As Rectangle

    If Not parent Is Nothing Then
        r = parent.ClientRectangle
        x = r.Width - frm.Width + parent.Left
        y = r.Height - frm.Height + parent.Top
    Else
        r = Screen.PrimaryScreen.WorkingArea
        x = r.Width - frm.Width
        y = r.Height - frm.Height
    End If

    x = CInt(x / 2)
    y = CInt(y / 2)

    frm.StartPosition = FormStartPosition.Manual
    frm.Location = New Point(x,y)
End Sub

如何定义,如何让这个子窗体正确放置在屏幕中间或其他形式?

代码是错的.此代码运行得足够晚也很重要,构造函数太早了.请务必从Load事件中调用它,此时表单已正确自动调整并根据用户的首选项进行调整,StartPosition属性不再重要.固定:
Public Shared Sub CenterForm(ByVal frm As Form,Optional ByVal parent As Form = Nothing)
    '' Note: call this from frm's Load event!
    Dim r As Rectangle
    If parent IsNot Nothing Then
        r = parent.RectangleToScreen(parent.ClientRectangle)
    Else
        r = Screen.FromPoint(frm.Location).WorkingArea
    End If

    Dim x = r.Left + (r.Width - frm.Width)  2
    Dim y = r.Top + (r.Height - frm.Height)  2
    frm.Location = New Point(x,y)
End Sub

顺便提一下,实际实现Load事件处理程序的极少数理由之一.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读