vb.net 在屏幕或父级上居中表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19392083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 17:13:52  来源:igfitidea点击:

Center form on screen or on parent

vb.net

提问by Wine Too

Since built in functionality for positioning forms in VB.NET are not always suitable to use I try to make my sub to do that.

由于 VB.NET 中用于定位表单的内置功能并不总是适合使用,因此我尝试让我的子程序做到这一点。

But I missed something...

但我错过了一些东西......

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

How to get this sub to place form correctly in the middle of the screen or other form if is defined?

如果已定义,如何让这个 sub 将表单正确地放置在屏幕或其他表单的中间?

回答by mike100111

I know this is an old post and this doesn't directly answer the question, but for anyone else who stumbles upon this thread, centering a form can be done simply without requiring you to write your own procedure.

我知道这是一篇旧帖子,这并没有直接回答问题,但是对于偶然发现此线程的任何其他人,可以简单地将表单居中,而无需您编写自己的程序。

The System.Windows.Forms.Form.CenterToScreen()and System.Windows.Forms.Form.CenterToParent()allow you to center the a form in reference to the screen or in reference to the parent form depending on which one you need.

System.Windows.Forms.Form.CenterToScreen()System.Windows.Forms.Form.CenterToParent()允许您居中表格中提到的屏幕或引用具体取决于您需要的父窗体。

One thing to note is that these procedures MUSTbe called before the form is loaded. It is best to call them in the form_load event handler.

需要注意的一件事是,必须在加载表单之前调用这些过程。最好在 form_load 事件处理程序中调用它们。

EXAMPLE CODE:

示例代码:

  Private Sub Settings_Load(sender As Object, e As EventArgs) Handles Me.Load

    Me.CenterToScreen()

    'or you can use 

    Me.CenterToParent()

End Sub

回答by Hans Passant

The code is just wrong. It is also essential that this code runs late enough, the constructor is too early. Be sure to call it from the Load event, at that time the form is properly auto-scaled and adjusted for the user's preferences, the StartPosition property no longer matters then. Fix:

只是代码错了。这段代码运行得足够晚也很重要,构造函数太早了。一定要从 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

Incidentally, one of the very few reasons to actually implement the Load event handler.

顺便说一下,这是实际实现 Load 事件处理程序的极少数原因之一。

回答by user3012629

This might also be of use:

这也可能有用:

    myForm.StartPosition = FormStartPosition.CenterParent
    myForm.ShowDialog()

You can also use FormStartPosition.CenterScreen

您还可以使用 FormStartPosition.CenterScreen

回答by Marco Sanchez

I had the problem with StartPosition = CenterParentnot working. I solved it calling the form with .ShowDialog()instead of .Show():

我遇到了StartPosition = CenterParent无法工作的问题。我解决了它调用表单.ShowDialog()而不是.Show()

' first you should set your form's Start Position as Center Parent
Private Sub button_Click(sender As Object, e As EventArgs) Handles button.Click
    MyForm.ShowDialog()
End Sub