本文通過一個Demo示例講述把A應用程序嵌入到B應用程序中顯示的方法。
主要代碼如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
An application can use the SetParent function to set the parent window of a pop-up, overlapped, or child window. The new parent window and the child window must belong to the same application。
這是從msdn裏面對SetParent的使用描述,說SetParent要嵌入的窗口必須和被嵌入的窗口屬於同一個應用程序,其實不然,不過SetParent返回的確實是一個Null,這是因爲A進程中的窗口並不存在父窗口,而SetParent調用後會返回父窗口的句柄,故返回Null。但是卻不會影響A進程窗口嵌入。
在B進程窗口顯示時收到的WM_ACTIVE中先會收到WA_ACTIVE或者WA_CLICKACTIVE,然後也會收到WA_INACTIVE參數,也就是說在某個時候,B進程沒有被置爲最前窗口,這應該是當調用SetParent時,影響了B進程窗口。如果你希望B進程窗口收到WM_ACTIVE消息,必須調用SetForegroundWindow(hWnd),如果當前窗口不是最前窗口時,退出或者最小化時不會收到WM_ACTIVE消息中的WA_INACTIVE。尤其做嵌套應用程序的代碼中,應該牢記於心。
除此以外,當使用SetParent(hWndChild,hWnd)把A進程到B進程窗口中時,這時使用FindWindow查詢到A進程的窗口句柄爲null。