將MdiForm內的Form移出MdiForm
來源:cww
事實上,這是設定Container的問題,但Form中沒有Container的屬性,所以使用SetParent
來做,不過,這和VB中Parent的屬性是不太相同的,VB的Parent指的是這個控制項(或
window)是哪一個Window的子Window(Parent Window destory時Child Window也會Destory)
而VB的Container指的是子控制項會隨著Container所指的Window來定位置,例如:command1
放在Frame中,則Command Button的Parent是Form,而Container是Frame,即Command Button
隨Frame移動,但Form結束時也隨之Destory。
而SetParent是指Container的轉換,而非VB中Parent屬性的轉換。

'需一個MdiForm ,一個Form1並事先設其MdiChild = True, 另有2個Command Button
' below is in Form1
Option Explicit
Private Declare Function SetParent Lib "user32" (ByVal hWndChild As Long, ByVal hWndNewParent As Long) As Long
Private OldContainer As Long
Private Sub Command1_Click()
OldContainer = SetParent(Me.hWnd, 0)
End Sub

Private Sub Command2_Click()
Call SetParent(Me.hWnd, OldContainer)
Me.Move 0, 0
End Sub

Private Sub Form_Load()
Command1.Caption = "移出"
Command2.Caption = "移入"
End Sub

' below is in MDIForm1
Private Sub MDIForm_Load()
Form1.Show
End Sub