【发布时间】:2021-05-11 18:14:18
【问题描述】:
由于在 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
如果已定义,如何让这个 sub 将表单正确放置在屏幕中间或其他表单中?
【问题讨论】:
-
这比原生的 CenterScreen 或 CenterParent 更合适或更容易吗?您需要检查父表单实际上是否大于您尝试显示的表单...
-
这是合适的,因为我可以将表单放置在 _Load 处理程序或更高版本中,并且我没有考虑将开始位置设置为“手动”。
-
另外,父表单不应该是真正的父表单,可以是任何类型的表单!
标签: vb.net