I am writing 开发者_JS百科a programme to be used internaly within our company and have come across the problem below:
How can you get a Child form to centre on the screen when using the MDI parent maximised form as the backgroung
In the MDI child screen, create a Form_Initialize function like this:
Private Sub Form_Initialize()
Me.Left = (MDIForm1.ScaleWidth - Me.Width) / 2
Me.Top = (MDIForm1.ScaleHeight - Me.Height) / 2
End Sub
Of course, you'll need to substitute the name of your MDI form where you see MDIForm1 in the code above.
From Microsoft: "The initial size and placement of MDI child forms are controlled by the Microsoft Windows operating environment unless you specifically set them in the Load event procedure."
From the parent:
Private Sub MDIForm_Load()
CenterChildForm MDIForm1, Form1
End Sub
Sub CenterChildForm(Parent As Form, Child As Form)
If Parent.WindowState = 1 Then Exit Sub 'The Parent is minimized, centering is invalid.
Child.Top = (Parent.ScaleHeight - Child.Height) / 2
Child.Left = (Parent.ScaleWidth - Child.Width) / 2
End Sub
From the Child:
Private Sub Form_Load()
Me.Left = (MDIForm1.ScaleWidth - Me.Width) / 2
Me.Top = (MDIForm1.ScaleHeight - Me.Height) / 2
End Sub
Select from the properties in the IDE on the bottom right the WINDOWS PROPERTY - CENTER PARENT. It may be named something a little difference but is in the drop down with CENTER SCREEN
EDIT: I think it is WINDOWS POSITION - CENTER PARENT
As an addition to the above use the me.Move [left], [top], [width], [height] method
it is quicker and performs the positioning in a single action.
精彩评论