在 MFC 應用中指定一個區域,用於顯示啟動的外部程序(如 Word、PDF 閱讀器)或文檔界面,而不是彈出獨立窗口。
將 MFC 窗口設置為外部進程的父窗口。具體步驟:
ShellExecuteEx 或 CreateProcess 啟動目標程序,獲取其主窗口句柄(HWND)。SetParent(hwndChild, hwndParent) 將外部窗口設置為 MFC 某控件(如 CStatic 或自定義 CWnd)的子窗口。WS_POPUP、WS_CAPTION 等,添加 WS_CHILD、WS_VISIBLE。WM_SIZE 消息)。// 啟動並嵌入
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.lpFile = L"notepad.exe";
sei.nShow = SW_SHOW;
ShellExecuteEx(&sei);
// 等待窗口創建...
HWND hExt = FindWindow(NULL, L"無標題 - 記事本");
if (hExt) {
SetParent(hExt, m_placeholder.GetSafeHwnd()); // m_placeholder 是佔位控件
LONG style = GetWindowLong(hExt, GWL_STYLE);
style &= ~(WS_POPUP | WS_CAPTION | WS_THICKFRAME);
style |= WS_CHILD | WS_VISIBLE;
SetWindowLong(hExt, GWL_STYLE, style);
MoveWindow(hExt, 0, 0, width, height, TRUE);
}