MFC子窗口和父窗口(SetParent,SetOwner)
// SubDlgDlg.cpp : implementation file // #include "stdafx.h" #include "SubDlg.h" #include "SubDlgDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define WM_USER_REDRAW (WM_USER + 1) #define MY_REDRAW 1 ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSubDlgDlg dialog CSubDlgDlg::CSubDlgDlg(CWnd* pParent /*=NULL*/) : CDialog(CSubDlgDlg::IDD, pParent) { //{{AFX_DATA_INIT(CSubDlgDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_hSubwnd = NULL; } void CSubDlgDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CSubDlgDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CSubDlgDlg, CDialog) //{{AFX_MSG_MAP(CSubDlgDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_MESSAGE(WM_USER_REDRAW, OnRedrewSubwnd) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CSubDlgDlg message handlers BOOL CSubDlgDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here InsertSubwnd(); return TRUE; // return TRUE unless you set the focus to a control } void CSubDlgDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CSubDlgDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CSubDlgDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CSubDlgDlg::InsertSubwnd() { ::ShellExecute(m_hWnd, "open", ".\\科学计算器.exe", NULL, NULL, SW_SHOWMINIMIZED); do { Sleep(300); m_hSubwnd = ::FindWindow(NULL, "科学计算器"); } while (!IsWindow(m_hSubwnd)); if (IsWindow(m_hSubwnd)) { long style=::GetWindowLong(m_hSubwnd, GWL_STYLE); style &= ~WS_POPUP; style += WS_CHILD; ::SetWindowLong(m_hSubwnd, GWL_STYLE, style); ::SetParent(m_hSubwnd, m_hWnd); SendMessage(WM_USER_REDRAW, MY_REDRAW); } } void CSubDlgDlg::OnRedrewSubwnd(WPARAM wParam, LPARAM lParam) { switch (wParam) { case MY_REDRAW: { if (IsWindow(m_hSubwnd)) { CWnd *pWnd = NULL; pWnd = FromHandle(m_hSubwnd); CRect rect; GetClientRect(&rect); pWnd->MoveWindow(&rect, false); pWnd->ModifyStyle(WS_CAPTION|WS_BORDER|WS_THICKFRAME, 0); pWnd->ShowWindow(SW_SHOW); //::SetForegroundWindow(m_hSubwnd); } } break; default: break; } }
// GetWndHandleDlg.cpp : implementation file // #include "stdafx.h" #include "GetWndHandle.h" #include "GetWndHandleDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif typedef struct { HWND hWnd; DWORD dwPid; } WNDINFO; BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam) { WNDINFO* pInfo = (WNDINFO*)lParam; DWORD dwProcessId = 0; GetWindowThreadProcessId(hWnd, &dwProcessId); if (dwProcessId == pInfo->dwPid) { pInfo->hWnd = hWnd; return FALSE; } return TRUE; } HWND GetHwndByProcessId(DWORD dwProcessId) { WNDINFO info = {0}; info.hWnd = NULL; info.dwPid = dwProcessId; EnumWindows(EnumWindowsProc, (LPARAM)&info); while( GetParent(info.hWnd) != NULL ) { // 循环查找父窗口,以便保证返回的句柄是最顶层的窗口句柄 info.hWnd = GetParent(info.hWnd); } return info.hWnd; } HWND GetWindowHandleByPID(DWORD dwProcessID) { HWND h = GetTopWindow(0); while (h) { DWORD pid = 0; DWORD dwTheardId = GetWindowThreadProcessId( h,&pid); if (dwTheardId != 0) { if (pid == dwProcessID/*your process id*/) { // here h is the handle to the window return h; } } h = GetNextWindow(h , GW_HWNDNEXT); } return NULL; } ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGetWndHandleDlg dialog CGetWndHandleDlg::CGetWndHandleDlg(CWnd* pParent /*=NULL*/) : CDialog(CGetWndHandleDlg::IDD, pParent) { //{{AFX_DATA_INIT(CGetWndHandleDlg) // NOTE: the ClassWizard will add member initialization here //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); m_hSubwnd = NULL; } void CGetWndHandleDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CGetWndHandleDlg) // NOTE: the ClassWizard will add DDX and DDV calls here //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CGetWndHandleDlg, CDialog) //{{AFX_MSG_MAP(CGetWndHandleDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CGetWndHandleDlg message handlers BOOL CGetWndHandleDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here InsertSubwnd(); return TRUE; // return TRUE unless you set the focus to a control } void CGetWndHandleDlg::OnSysCommand(UINT nID, LPARAM lParam) { if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } // If you add a minimize button to your dialog, you will need the code below // to draw the icon. For MFC applications using the document/view model, // this is automatically done for you by the framework. void CGetWndHandleDlg::OnPaint() { if (IsIconic()) { CPaintDC dc(this); // device context for painting SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0); // Center icon in client rectangle int cxIcon = GetSystemMetrics(SM_CXICON); int cyIcon = GetSystemMetrics(SM_CYICON); CRect rect; GetClientRect(&rect); int x = (rect.Width() - cxIcon + 1) / 2; int y = (rect.Height() - cyIcon + 1) / 2; // Draw the icon dc.DrawIcon(x, y, m_hIcon); } else { CDialog::OnPaint(); } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CGetWndHandleDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CGetWndHandleDlg::InsertSubwnd() { STARTUPINFO si; PROCESS_INFORMATION pi; TCHAR tcAppName[] = _T("CListCtrl.exe"); ZeroMemory( &si, sizeof(si) ); ZeroMemory( &pi, sizeof(pi) ); si.cb = sizeof(si); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; // Start the child process. if( CreateProcess( NULL, // No module name (use command line). tcAppName, // Command line. NULL, // Process handle not inheritable. NULL, // Thread handle not inheritable. FALSE, // Set handle inheritance to FALSE. 0, // No creation flags. NULL, // Use parent's environment block. NULL, // Use parent's starting directory. &si, // Pointer to STARTUPINFO structure. &pi ) // Pointer to PROCESS_INFORMATION structure. ) { // Wait until child process exits. // WaitForSingleObject( pi.hProcess, INFINITE ); // Close process and thread handles. // CloseHandle( pi.hProcess ); // CloseHandle( pi.hThread ); } WaitForInputIdle(pi.hProcess, INFINITE); m_hSubwnd = GetHwndByProcessId(pi.dwProcessId); //Iterate all windows Sleep(500); if (::IsWindow(m_hSubwnd)) { long style=::GetWindowLong(m_hSubwnd, GWL_STYLE); style &= ~WS_POPUP; style += WS_CHILD; ::SetWindowLong(m_hSubwnd, GWL_STYLE, style); ::SetParent(m_hSubwnd, m_hWnd); CWnd *pWnd = NULL; pWnd = FromHandle(m_hSubwnd); CRect rect; GetClientRect(&rect); pWnd->MoveWindow(&rect, false); pWnd->ModifyStyle(WS_CAPTION|WS_BORDER|WS_THICKFRAME, 0); pWnd->ShowWindow(SW_SHOW); } RedrawWindow(); }