////////////////// // DoModal override copied mostly from MFC, with modification to use // m_ofnEx instead of m_ofn. // int CFileDialogEx::DoModal() { ASSERT_VALID(this); ASSERT(m_ofn.Flags & OFN_ENABLEHOOK); ASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook // zero out the file buffer for consistent parsing later ASSERT(AfxIsValidAddress(m_ofn.lpstrFile, m_ofn.nMaxFile)); DWORD nOffset = lstrlen(m_ofn.lpstrFile)+1; ASSERT(nOffset <= m_ofn.nMaxFile); memset(m_ofn.lpstrFile+nOffset, 0, (m_ofn.nMaxFile-nOffset)*sizeof(TCHAR)); // WINBUG: This is a special case for the file open/save dialog, // which sometimes pumps while it is coming up but before it has // disabled the main window. HWND hWndFocus = ::GetFocus(); BOOL bEnableParent = FALSE; m_ofn.hwndOwner = PreModal(); AfxUnhookWindowCreate(); if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner)) { bEnableParent = TRUE; ::EnableWindow(m_ofn.hwndOwner, FALSE); } _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); ASSERT(pThreadState->m_pAlternateWndInit == NULL); if (m_ofn.Flags & OFN_EXPLORER) pThreadState->m_pAlternateWndInit = this; else AfxHookWindowCreate(this); memset(&m_ofnEx, 0, sizeof(m_ofnEx)); memcpy(&m_ofnEx, &m_ofn, sizeof(m_ofn)); if (IsWin2000()) m_ofnEx.lStructSize = sizeof(m_ofnEx); int nResult; if (m_bOpenFileDialog) nResult = ::GetOpenFileName((OPENFILENAME*)&m_ofnEx); else nResult = ::GetSaveFileName((OPENFILENAME*)&m_ofnEx); memcpy(&m_ofn, &m_ofnEx, sizeof(m_ofn)); m_ofn.lStructSize = sizeof(m_ofn); if (nResult) ASSERT(pThreadState->m_pAlternateWndInit == NULL); pThreadState->m_pAlternateWndInit = NULL; // WINBUG: Second part of special case for file open/save dialog. if (bEnableParent) ::EnableWindow(m_ofnEx.hwndOwner, TRUE); if (::IsWindow(hWndFocus)) ::SetFocus(hWndFocus); PostModal(); return nResult ? nResult : IDCANCEL; }
BOOL CFindDialog::Create(LPCTSTR lpszFindWhat, DWORD dwFlags, WhereType where, CWnd* pParentWnd) { m_nFindPos=where; m_nIDHelp = AFX_IDD_FIND; m_fr.Flags |= dwFlags; ASSERT_VALID(pParentWnd); m_fr.hwndOwner = pParentWnd->m_hWnd; ASSERT(m_fr.hwndOwner != NULL); // must have a parent for modeless dialog m_fr.wFindWhatLen = sizeof(m_szFindWhat); int n=min(sizeof(m_szFindWhat)-1,_tcslen(lpszFindWhat)); _tcsncpy(m_szFindWhat, lpszFindWhat, n); m_szFindWhat[n]=_TCHAR('\0'); AfxHookWindowCreate(this); HWND hWnd = ::FindText(&m_fr); if (!AfxUnhookWindowCreate()){ PostNcDestroy(); } ASSERT(hWnd == NULL || hWnd == m_hWnd); return hWnd != NULL; }
BOOL CMDIChildWnd::Create( LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT &rect, CMDIFrameWnd *pParentWnd, CCreateContext *pContext ) /***************************************************/ { if( pParentWnd == NULL ) { pParentWnd = (CMDIFrameWnd *)AfxGetMainWnd(); } ASSERT( pParentWnd != NULL ); ASSERT( pParentWnd->IsKindOf( RUNTIME_CLASS( CMDIFrameWnd ) ) ); CREATESTRUCT cs; cs.dwExStyle = 0L; cs.lpszClass = lpszClassName; cs.lpszName = lpszWindowName; cs.style = dwStyle; cs.x = rect.left; cs.y = rect.top; cs.cx = rect.right - rect.left; cs.cy = rect.bottom - rect.top; cs.hwndParent = pParentWnd->m_hWnd; cs.hMenu = NULL; cs.hInstance = AfxGetInstanceHandle(); cs.lpCreateParams = pContext; if( !PreCreateWindow( cs ) ) { PostNcDestroy(); return( FALSE ); } MDICREATESTRUCT mcs; mcs.szClass = cs.lpszClass; mcs.szTitle = cs.lpszName; mcs.hOwner = cs.hInstance; mcs.x = cs.x; mcs.y = cs.y; mcs.cx = cs.cx; mcs.cy = cs.cy; mcs.style = cs.style; mcs.lParam = (LPARAM)cs.lpCreateParams; AfxHookWindowCreate( this ); HWND hWnd = (HWND)::SendMessage( pParentWnd->m_hWndMDIClient, WM_MDICREATE, 0, (LPARAM)&mcs ); if( !AfxUnhookWindowCreate() ) { PostNcDestroy(); return( FALSE ); } if( hWnd == NULL ) { PostNcDestroy(); return( FALSE ); } ASSERT( m_hWnd == hWnd ); return( TRUE ); }
void CPrintDialog::OnPrintSetup() { ASSERT_VALID(this); CPrintDialog* pDlgSetup = AttachOnSetup(); ASSERT(pDlgSetup != NULL); AfxHookWindowCreate(pDlgSetup); Default(); AfxUnhookWindowCreate(); delete pDlgSetup; }
void CDialog::PostModal() { AfxUnhookWindowCreate(); // just in case Detach(); // just in case // re-enable windows if (::IsWindow(m_hWndTop)) ::EnableWindow(m_hWndTop, TRUE); m_hWndTop = NULL; CWinApp* pApp = AfxGetApp(); if (pApp != NULL) pApp->EnableModeless(TRUE); }
int CFileDialog::DoModal() { ASSERT_VALID(this); ASSERT(m_ofn.Flags & OFN_ENABLEHOOK); ASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook #ifdef _MAC ASSERT((m_ofn.Flags & OFN_ALLOWMULTISELECT) == 0); #endif // WINBUG: This is a special case for the file open/save dialog, // which sometimes pumps while it is coming up but before it has // disabled the main window. HWND hWndFocus = ::GetFocus(); BOOL bEnableParent = FALSE; m_ofn.hwndOwner = PreModal(); AfxUnhookWindowCreate(); if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner)) { bEnableParent = TRUE; ::EnableWindow(m_ofn.hwndOwner, FALSE); } _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); ASSERT(pThreadState->m_pAlternateWndInit == NULL); if (m_ofn.Flags & OFN_EXPLORER) pThreadState->m_pAlternateWndInit = this; else AfxHookWindowCreate(this); int nResult; if (m_bOpenFileDialog) nResult = ::GetOpenFileName(&m_ofn); else nResult = ::GetSaveFileName(&m_ofn); if (nResult) ASSERT(pThreadState->m_pAlternateWndInit == NULL); pThreadState->m_pAlternateWndInit = NULL; // WINBUG: Second part of special case for file open/save dialog. if (bEnableParent) ::EnableWindow(m_ofn.hwndOwner, TRUE); if (::IsWindow(hWndFocus)) ::SetFocus(hWndFocus); PostModal(); return nResult ? nResult : IDCANCEL; }
BOOL BaseDialog::CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd, HINSTANCE hInst) { if(!hInst) hInst = AfxGetResourceHandle(); HGLOBAL hTemplate = NULL; HWND hWnd = NULL; TRY { CDialogTemplate dlgTemp(lpDialogTemplate); SetFont(dlgTemp); hTemplate = dlgTemp.Detach(); lpDialogTemplate = (DLGTEMPLATE*)GlobalLock(hTemplate); m_nModalResult = -1; m_nFlags |= WF_CONTINUEMODAL; AfxHookWindowCreate(this); hWnd = ::CreateDialogIndirect(hInst, lpDialogTemplate, pParentWnd->GetSafeHwnd(), AfxDlgProc); } CATCH_ALL(e) { e->Delete(); m_nModalResult = -1; } END_CATCH_ALL if (!AfxUnhookWindowCreate()) PostNcDestroy(); if (hWnd != NULL && !(m_nFlags & WF_CONTINUEMODAL)) { ::DestroyWindow(hWnd); hWnd = NULL; } if (hTemplate != NULL) { GlobalUnlock(hTemplate); GlobalFree(hTemplate); } if (hWnd == NULL) return FALSE; return TRUE; }
INT_PTR SimpleFileDialog::DoModal() { // Don't use a hook procedure for Vista OSVERSIONINFO osvi; osvi.dwOSVersionInfoSize = sizeof osvi; ::GetVersionEx(&osvi); if (osvi.dwMajorVersion >= 6) m_ofn.Flags &= ~OFN_ENABLEHOOK; DWORD nOffset = lstrlen(m_ofn.lpstrFile)+1; memset(m_ofn.lpstrFile+nOffset, 0, (m_ofn.nMaxFile-nOffset)*sizeof(TCHAR)); HWND hWndFocus = ::GetFocus(); BOOL bEnableParent = FALSE; m_ofn.hwndOwner = PreModal(); AfxUnhookWindowCreate(); if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner)) { bEnableParent = TRUE; ::EnableWindow(m_ofn.hwndOwner, FALSE); } _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); if (m_ofn.Flags & OFN_EXPLORER) pThreadState->m_pAlternateWndInit = this; else AfxHookWindowCreate(this); INT_PTR nResult; if (m_bOpenFileDialog) nResult = ::GetOpenFileName(&m_ofn); else nResult = ::GetSaveFileName(&m_ofn); pThreadState->m_pAlternateWndInit = NULL; if (bEnableParent) ::EnableWindow(m_ofn.hwndOwner, TRUE); if (::IsWindow(hWndFocus)) ::SetFocus(hWndFocus); PostModal(); return nResult ? nResult : IDCANCEL; }
BOOL CFindReplaceDialog::Create(BOOL bFindDialogOnly, LPCTSTR lpszFindWhat, LPCTSTR lpszReplaceWith, DWORD dwFlags, CWnd* pParentWnd) { ASSERT_VALID(this); ASSERT(m_fr.Flags & FR_ENABLEHOOK); ASSERT(m_fr.lpfnHook != NULL); m_nIDHelp = bFindDialogOnly ? AFX_IDD_FIND : AFX_IDD_REPLACE; m_fr.wFindWhatLen = _countof(m_szFindWhat); m_fr.lpstrReplaceWith = (LPTSTR)m_szReplaceWith; m_fr.wReplaceWithLen = _countof(m_szReplaceWith); m_fr.Flags |= dwFlags; if (pParentWnd == NULL) m_fr.hwndOwner = AfxGetMainWnd()->GetSafeHwnd(); else { ASSERT_VALID(pParentWnd); m_fr.hwndOwner = pParentWnd->m_hWnd; } ASSERT(m_fr.hwndOwner != NULL); // must have a parent for modeless dialog if (lpszFindWhat != NULL) lstrcpyn(m_szFindWhat, lpszFindWhat, _countof(m_szFindWhat)); if (lpszReplaceWith != NULL) lstrcpyn(m_szReplaceWith, lpszReplaceWith, _countof(m_szReplaceWith)); HWND hWnd; AfxHookWindowCreate(this); if (bFindDialogOnly) hWnd = ::FindText(&m_fr); else hWnd = ::ReplaceText(&m_fr); if (!AfxUnhookWindowCreate()) PostNcDestroy(); ASSERT(hWnd == NULL || hWnd == m_hWnd); return hWnd != NULL; }
BOOL CFormView::Create( LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT &rect, CWnd *pParentWnd, UINT nID, CCreateContext *pContext ) /************************************************/ { UNUSED_ALWAYS( lpszClassName ); UNUSED_ALWAYS( lpszWindowName ); CREATESTRUCT cs; memset( &cs, 0, sizeof( CREATESTRUCT ) ); cs.style = dwStyle; if( !PreCreateWindow( cs ) ) { PostNcDestroy(); return( FALSE ); } m_pCreateContext = pContext; AfxHookWindowCreate( this ); HWND hWnd = ::CreateDialogParam( AfxGetInstanceHandle(), m_lpszTemplateName, pParentWnd->GetSafeHwnd(), AfxDlgProc, 0L ); AfxUnhookWindowCreate(); m_pCreateContext = NULL; if( hWnd == NULL ) { PostNcDestroy(); return( FALSE ); } ASSERT( m_hWnd == hWnd ); CRect rectDialog; ::GetWindowRect( hWnd, &rectDialog ); SetScrollSizes( MM_TEXT, rectDialog.Size() ); ::SetWindowLong( hWnd, GWL_ID, nID ); ::SetWindowPos( hWnd, NULL, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW ); return( TRUE ); }
BOOL CDialogBar::Create( CWnd *pParentWnd, LPCTSTR lpszTemplateName, UINT nStyle, UINT nID ) /*******************************************************************************/ { m_dwStyle = nStyle & CBRS_ALL; CREATESTRUCT cs; memset( &cs, 0, sizeof( CREATESTRUCT ) ); cs.lpszClass = _T("AfxControlBar"); cs.style = nStyle | WS_CHILD; cs.hwndParent = pParentWnd->GetSafeHwnd(); cs.hMenu = (HMENU)nID; cs.hInstance = AfxGetInstanceHandle(); if( !PreCreateWindow( cs ) ) { PostNcDestroy(); return( FALSE ); } AfxHookWindowCreate( this ); HWND hWnd = ::CreateDialogParam( AfxGetInstanceHandle(), lpszTemplateName, pParentWnd->GetSafeHwnd(), AfxDlgProc, 0L ); AfxUnhookWindowCreate(); if( hWnd == NULL ) { PostNcDestroy(); return( FALSE ); } ASSERT( m_hWnd == hWnd ); CRect rect; ::GetWindowRect( hWnd, &rect ); m_sizeDefault = rect.Size(); ::SetWindowLong( hWnd, GWL_ID, nID ); ::SetWindowPos( hWnd, NULL, 0, 0, 0, 0, SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW ); return( TRUE ); }
INT_PTR COleBusyDialog::DoModal() { // Note: we don't call EnableModeless because that in itself implies // outgoing calls. This dialog is normally brought up when an outgoing // call cannot be made. // find parent HWND HWND hWndTop; HWND hParent = CWnd::GetSafeOwner_(m_pParentWnd->GetSafeHwnd(), &hWndTop); m_bz.hWndOwner = hParent; // run the dialog AfxHookWindowCreate(this); int iResult = ::OleUIBusy(&m_bz); AfxUnhookWindowCreate(); // just in case Detach(); // just in case // enable top level window if (hWndTop != NULL) ::EnableWindow(hWndTop, TRUE); // map the result if (iResult == OLEUI_CANCEL) return IDCANCEL; if (iResult == OLEUI_BZ_SWITCHTOSELECTED) m_selection = switchTo; else if (iResult == OLEUI_BZ_RETRYSELECTED) m_selection = retry; else if (iResult == OLEUI_BZ_CALLUNBLOCKED) m_selection = callUnblocked; else m_selection = (Selection)MapResult(iResult); return IDOK; }
int CWebInterfaceDlg::DoModal() { ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL || m_lpDialogTemplate != NULL); LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate; HGLOBAL hDialogTemplate = m_hDialogTemplate; HINSTANCE hInst = AfxGetResourceHandle(); if (m_lpszTemplateName != NULL) { hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG); HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG); hDialogTemplate = LoadResource(hInst, hResource); } if (hDialogTemplate != NULL) lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate); if (lpDialogTemplate == NULL) return -1; HWND hWndParent = PreModal(); AfxUnhookWindowCreate(); BOOL bEnableParent = FALSE; if (hWndParent != NULL && ::IsWindowEnabled(hWndParent)) { ::EnableWindow(hWndParent, FALSE); bEnableParent = TRUE; } TRY { AfxHookWindowCreate(this); if (CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), hInst)) { if (m_nFlags & WF_CONTINUEMODAL) { DWORD dwFlags = 0; if (GetStyle() & DS_NOIDLEMSG) dwFlags |= MLF_NOIDLEMSG; VERIFY(RunModalLoop(dwFlags) == m_nModalResult); } if (m_hWnd != NULL) SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW| SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER); } } CATCH_ALL(e) { DELETE_EXCEPTION(e); m_nModalResult = -1; } END_CATCH_ALL if (bEnableParent) ::EnableWindow(hWndParent, TRUE); if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd) ::SetActiveWindow(hWndParent); DestroyWindow(); PostModal(); if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL) UnlockResource(hDialogTemplate); if (m_lpszTemplateName != NULL) FreeResource(hDialogTemplate); return m_nModalResult; }
INT_PTR C_PrintDialogEx::DoModal() { ASSERT_VALID(this); m_pd.hwndOwner = PreModal(); AfxUnhookWindowCreate(); // expand m_pd data into the PRINTDLGEX structure m_pdex.hwndOwner = m_pd.hwndOwner; m_pdex.hDevMode = m_pd.hDevMode; m_pdex.hDevNames = m_pd.hDevNames; m_pdex.hDC = m_pd.hDC; // m_pdex.Flags = (m_pd.Flags & ~(PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK | PD_PRINTSETUP)); m_pdex.nMinPage = m_pd.nMinPage; m_pdex.nMaxPage = m_pd.nMaxPage; m_pdex.hInstance = m_pd.hInstance; m_pdex.nStartPage = START_PAGE_GENERAL; m_pdex.nCopies = m_pd.nCopies; m_pdex.lpCallback = &m_xPrintDialogCallback; // initialize page ranges PRINTPAGERANGE ourPageRange; if (m_pdex.Flags & PD_NOPAGENUMS) { m_pdex.lpPageRanges = NULL; m_pdex.nPageRanges = 0; m_pdex.nMaxPageRanges = 0; } else { ourPageRange.nFromPage = m_pd.nFromPage; ourPageRange.nToPage = m_pd.nToPage; m_pdex.nPageRanges = 1; m_pdex.nMaxPageRanges = 1; m_pdex.lpPageRanges = &ourPageRange; } HMODULE hCommDlg = GetModuleHandleA("COMDLG32.DLL"); HRESULT (STDAPICALLTYPE* pfn)(LPPRINTDLGEX); #ifdef UNICODE pfn = (HRESULT (STDAPICALLTYPE*)(LPPRINTDLGEX)) GetProcAddress(hCommDlg, "PrintDlgExW"); #else pfn = (HRESULT (STDAPICALLTYPE*)(LPPRINTDLGEX)) GetProcAddress(hCommDlg, "PrintDlgExA"); #endif HRESULT hResult = E_NOTIMPL; if (pfn != NULL) { _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); ASSERT(pThreadState->m_pAlternateWndInit == NULL); pThreadState->m_pAlternateWndInit = this; hResult = pfn(&m_pdex); if (SUCCEEDED(hResult)) ASSERT(pThreadState->m_pAlternateWndInit == NULL); pThreadState->m_pAlternateWndInit = NULL; } // pull data back... PostModal(); if (!(m_pdex.Flags & PD_NOPAGENUMS)) { m_pd.nToPage = (WORD) m_pdex.lpPageRanges->nToPage; m_pd.nFromPage = (WORD) m_pdex.lpPageRanges->nFromPage; m_pd.nMinPage = (WORD) m_pdex.nMinPage; m_pd.nMaxPage = (WORD) m_pdex.nMaxPage; } m_pd.hDevMode = m_pdex.hDevMode; m_pd.hDevNames = m_pdex.hDevNames; m_pd.hDC = m_pdex.hDC; m_pd.nCopies = (WORD)m_pdex.nCopies; // calculate return code int nResult = IDCANCEL; if (SUCCEEDED(hResult)) { if (m_pdex.dwResultAction == PD_RESULT_PRINT) nResult = IDOK; } return nResult; }
int CRuntimeDlg::DoModal(LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle, const CRect& rect, CWnd* pParentWnd, UINT nID) { // fail immediately if no controls have been added if (!m_lstControls.GetCount()) return IDCANCEL; // setup for modal loop and creation m_nModalResult = -1; m_nFlags |= WF_CONTINUEMODAL; // disable parent (before creating dialog) HWND hWndParent = PreModal(); AfxUnhookWindowCreate(); BOOL bEnableParent = FALSE; if (hWndParent != NULL && ::IsWindowEnabled(hWndParent)) { ::EnableWindow(hWndParent, FALSE); bEnableParent = TRUE; } try { // create modeless dialog if (Create(szCaption, dwStyle, dwExStyle, rect, pParentWnd ? pParentWnd : CWnd::FromHandle(hWndParent), nID)) { if (m_nFlags & WF_CONTINUEMODAL) { // enter modal loop DWORD dwFlags = MLF_SHOWONIDLE; if (GetStyle() & DS_NOIDLEMSG) dwFlags |= MLF_NOIDLEMSG; VERIFY(RunModalLoop(dwFlags) == m_nModalResult); } // hide the window before enabling the parent, etc. if (m_hWnd != NULL) SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER); } } catch(...) { m_nModalResult = -1; } if (bEnableParent) ::EnableWindow(hWndParent, TRUE); if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd) ::SetActiveWindow(hWndParent); // save pos SaveCurrentPos(); // destroy modal window DestroyWindow(); PostModal(); // cleanup resources GraphicsMisc::VerifyDeleteObject(m_hFont); ::DestroyIcon(m_hILarge); ::DestroyIcon(m_hISmall); return m_nModalResult; }
//============================================================================= // DoModal override copied mostly from MFC, with modification to use // m_ofnEx instead of m_ofn. INT_PTR CXFolderDialog::DoModal() //============================================================================= { TRACE(_T("in CXFolderDialog::DoModal\n")); ASSERT_VALID(this); ASSERT(m_ofn.Flags & OFN_ENABLEHOOK); ASSERT(m_ofn.lpfnHook != NULL); // can still be a user hook AFX_MANAGE_STATE(AFXMANAGESTATE()) m_bFirstTime = TRUE; m_wndListView.Init(this); // zero out the file buffer for consistent parsing later TCHAR * pBuf = new TCHAR [m_ofn.nMaxFile + 100]; memset(pBuf, 0, (m_ofn.nMaxFile + 100) * sizeof(TCHAR)); if (m_ofn.lpstrFile) { // copy file buffer _tcsncpy(pBuf, m_ofn.lpstrFile, m_ofn.nMaxFile); // zero out file buffer in OPENFILENAME struct memset(m_ofn.lpstrFile, 0, m_ofn.nMaxFile*sizeof(TCHAR)); // restore copied file buffer _tcsncpy(m_ofn.lpstrFile, pBuf, m_ofn.nMaxFile); } if (pBuf) delete [] pBuf; pBuf = NULL; // WINBUG: This is a special case for the file open/save dialog, // which sometimes pumps while it is coming up but before it has // disabled the main window. HWND hWndFocus = ::GetFocus(); BOOL bEnableParent = FALSE; m_ofn.hwndOwner = PreModal(); AfxUnhookWindowCreate(); if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner)) { bEnableParent = TRUE; ::EnableWindow(m_ofn.hwndOwner, FALSE); } _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); ASSERT(pThreadState->m_pAlternateWndInit == NULL); if (m_ofn.Flags & OFN_EXPLORER) pThreadState->m_pAlternateWndInit = this; else AfxHookWindowCreate(this); memset(&m_ofnEx, 0, sizeof(m_ofnEx)); memcpy(&m_ofnEx, &m_ofn, sizeof(m_ofn)); if (m_ofnEx.hInstance == 0) m_ofnEx.hInstance = AfxGetInstanceHandle(); m_ofnEx.Flags |= OFN_ENABLESIZING; #if (_WIN32_WINNT >= 0x0500) TRACE(_T("_WIN32_WINNT=0x%X\n"), _WIN32_WINNT); #pragma message(__FILE__ "(" makestring(__LINE__) \ ") : OPENFILENAME is large size (3 extra items are included)") if (IsWin2000()) m_ofnEx.lStructSize = sizeof(OPENFILENAME); // OS >= 2000, force to large size else m_ofnEx.lStructSize = OPENFILENAME_SIZE_VERSION_400; // use small size #else // _WIN32_WINNT < 0x0500, or isn't defined #pragma message(__FILE__ "(" makestring(__LINE__) \ ") : OPENFILENAME is small size (3 extra items are NOT included)") if (IsWin2000()) m_ofnEx.lStructSize = sizeof(OPENFILENAMEEX_FOLDER); // OS >= 2000, force to large size else m_ofnEx.lStructSize = OPENFILENAME_SIZE_VERSION_400; // use small size #endif m_ofnEx.lpstrInitialDir = m_strInitialFolder; //m_ofnEx.lpstrFilter = m_strFilter; int nResult = 0; if (m_bOpenFileDialog) nResult = ::GetOpenFileName((OPENFILENAME*)&m_ofnEx); else nResult = ::GetSaveFileName((OPENFILENAME*)&m_ofnEx); memcpy(&m_ofn, &m_ofnEx, sizeof(m_ofn)); m_ofn.lStructSize = sizeof(m_ofn); if (nResult) { ASSERT(pThreadState->m_pAlternateWndInit == NULL); } pThreadState->m_pAlternateWndInit = NULL; // WINBUG: Second part of special case for file open/save dialog. if (bEnableParent) ::EnableWindow(m_ofnEx.hwndOwner, TRUE); if (::IsWindow(hWndFocus)) ::SetFocus(hWndFocus); PostModal(); return nResult ? nResult : IDCANCEL; }
BOOL CWnd::CreateDlgIndirect(LPCDLGTEMPLATE lpDialogTemplate, CWnd* pParentWnd, HINSTANCE hInst) { ASSERT(lpDialogTemplate != NULL); if (pParentWnd != NULL) ASSERT_VALID(pParentWnd); if (hInst == NULL) hInst = AfxGetInstanceHandle(); #ifndef _AFX_NO_OCC_SUPPORT _AFX_OCC_DIALOG_INFO occDialogInfo; COccManager* pOccManager = afxOccManager; #endif HGLOBAL hTemplate = NULL; HWND hWnd = NULL; #ifdef _DEBUG DWORD dwError = 0; #endif TRY { VERIFY(AfxDeferRegisterClass(AFX_WNDCOMMCTLS_REG)); AfxDeferRegisterClass(AFX_WNDCOMMCTLSNEW_REG); #ifndef _AFX_NO_OCC_SUPPORT // separately create OLE controls in the dialog template if (pOccManager != NULL) { if (!SetOccDialogInfo(&occDialogInfo)) return FALSE; lpDialogTemplate = pOccManager->PreCreateDialog(&occDialogInfo, lpDialogTemplate); } if (lpDialogTemplate == NULL) return FALSE; #endif //!_AFX_NO_OCC_SUPPORT // If no font specified, set the system font. CString strFace; WORD wSize = 0; BOOL bSetSysFont = !CDialogTemplate::GetFont(lpDialogTemplate, strFace, wSize); // On DBCS systems, also change "MS Sans Serif" or "Helv" to system font. if ((!bSetSysFont) && GetSystemMetrics(SM_DBCSENABLED)) { bSetSysFont = (strFace == _T("MS Shell Dlg") || strFace == _T("MS Sans Serif") || strFace == _T("Helv")); if (bSetSysFont && (wSize == 8)) wSize = 0; } if (bSetSysFont) { CDialogTemplate dlgTemp(lpDialogTemplate); dlgTemp.SetSystemFont(wSize); hTemplate = dlgTemp.Detach(); } if (hTemplate != NULL) lpDialogTemplate = (DLGTEMPLATE*)GlobalLock(hTemplate); // setup for modal loop and creation m_nModalResult = -1; m_nFlags |= WF_CONTINUEMODAL; // create modeless dialog AfxHookWindowCreate(this); hWnd = ::CreateDialogIndirect(hInst, lpDialogTemplate, pParentWnd->GetSafeHwnd(), AfxDlgProc); #ifdef _DEBUG dwError = ::GetLastError(); #endif } CATCH_ALL(e) { DELETE_EXCEPTION(e); m_nModalResult = -1; } END_CATCH_ALL #ifndef _AFX_NO_OCC_SUPPORT if (pOccManager != NULL) { pOccManager->PostCreateDialog(&occDialogInfo); if (hWnd != NULL) SetOccDialogInfo(NULL); } #endif //!_AFX_NO_OCC_SUPPORT if (!AfxUnhookWindowCreate()) PostNcDestroy(); // cleanup if Create fails too soon // handle EndDialog calls during OnInitDialog if (hWnd != NULL && !(m_nFlags & WF_CONTINUEMODAL)) { ::DestroyWindow(hWnd); hWnd = NULL; } if (hTemplate != NULL) { GlobalUnlock(hTemplate); GlobalFree(hTemplate); } // help with error diagnosis (only if WM_INITDIALOG didn't EndDialog()) if (hWnd == NULL && (m_nFlags & WF_CONTINUEMODAL)) { #ifdef _DEBUG #ifndef _AFX_NO_OCC_SUPPORT if (afxOccManager == NULL) { TRACE0(">>> If this dialog has OLE controls:\n"); TRACE0(">>> AfxEnableControlContainer has not been called yet.\n"); TRACE0(">>> You should call it in your app's InitInstance function.\n"); } else if (dwError != 0) { TRACE1("Warning: Dialog creation failed! GetLastError returns 0x%8.8X\n", dwError); } #endif //!_AFX_NO_OCC_SUPPORT #endif //_DEBUG return FALSE; } ASSERT(hWnd == m_hWnd); return TRUE; }
BOOL CMDIChildWnd::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CMDIFrameWnd* pParentWnd, CCreateContext* pContext) { if (pParentWnd == NULL) { CWinThread *pThread = AfxGetThread(); ENSURE_VALID(pThread); CWnd* pMainWnd = pThread->m_pMainWnd; ENSURE_VALID(pMainWnd); ASSERT_KINDOF(CMDIFrameWnd, pMainWnd); pParentWnd = (CMDIFrameWnd*)pMainWnd; } ASSERT(::IsWindow(pParentWnd->m_hWndMDIClient)); // insure correct window positioning pParentWnd->RecalcLayout(); // first copy into a CREATESTRUCT for PreCreate CREATESTRUCT cs; cs.dwExStyle = 0L; cs.lpszClass = lpszClassName; cs.lpszName = lpszWindowName; cs.style = dwStyle; cs.x = rect.left; cs.y = rect.top; cs.cx = rect.right - rect.left; cs.cy = rect.bottom - rect.top; cs.hwndParent = pParentWnd->m_hWnd; cs.hMenu = NULL; cs.hInstance = AfxGetInstanceHandle(); cs.lpCreateParams = (LPVOID)pContext; if (!PreCreateWindow(cs)) { PostNcDestroy(); return FALSE; } // extended style must be zero for MDI Children (except under Win4) ASSERT(cs.hwndParent == pParentWnd->m_hWnd); // must not change // now copy into a MDICREATESTRUCT for real create MDICREATESTRUCT mcs; mcs.szClass = cs.lpszClass; mcs.szTitle = cs.lpszName; mcs.hOwner = cs.hInstance; mcs.x = cs.x; mcs.y = cs.y; mcs.cx = cs.cx; mcs.cy = cs.cy; mcs.style = cs.style & ~(WS_MAXIMIZE | WS_VISIBLE); mcs.lParam = (LPARAM)cs.lpCreateParams; // create the window through the MDICLIENT window AfxHookWindowCreate(this); HWND hWnd = (HWND)::SendMessage(pParentWnd->m_hWndMDIClient, WM_MDICREATE, 0, (LPARAM)&mcs); if (!AfxUnhookWindowCreate()) PostNcDestroy(); // cleanup if MDICREATE fails too soon if (hWnd == NULL) return FALSE; // special handling of visibility (always created invisible) if (cs.style & WS_VISIBLE) { // place the window on top in z-order before showing it ::BringWindowToTop(hWnd); // show it as specified if (cs.style & WS_MINIMIZE) ShowWindow(SW_SHOWMINIMIZED); else if (cs.style & WS_MAXIMIZE) ShowWindow(SW_SHOWMAXIMIZED); else ShowWindow(SW_SHOWNORMAL); // make sure it is active (visibility == activation) pParentWnd->MDIActivate(this); // refresh MDI Window menu ::SendMessage(pParentWnd->m_hWndMDIClient, WM_MDIREFRESHMENU, 0, 0); } ASSERT(hWnd == m_hWnd); return TRUE; }
int CDialog::DoModal() { // can be constructed with a resource template or InitModalIndirect ASSERT(m_lpszTemplateName != NULL || m_hDialogTemplate != NULL || m_lpDialogTemplate != NULL); // load resource as necessary LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate; HGLOBAL hDialogTemplate = m_hDialogTemplate; HINSTANCE hInst = AfxGetResourceHandle(); if (m_lpszTemplateName != NULL) { hInst = AfxFindResourceHandle(m_lpszTemplateName, RT_DIALOG); HRSRC hResource = ::FindResource(hInst, m_lpszTemplateName, RT_DIALOG); hDialogTemplate = LoadResource(hInst, hResource); } if (hDialogTemplate != NULL) lpDialogTemplate = (LPCDLGTEMPLATE)LockResource(hDialogTemplate); // return -1 in case of failure to load the dialog template resource if (lpDialogTemplate == NULL) return -1; // disable parent (before creating dialog) HWND hWndParent = PreModal(); AfxUnhookWindowCreate(); BOOL bEnableParent = FALSE; if (hWndParent != NULL && ::IsWindowEnabled(hWndParent)) { ::EnableWindow(hWndParent, FALSE); bEnableParent = TRUE; } TRY { // create modeless dialog AfxHookWindowCreate(this); if (CreateDlgIndirect(lpDialogTemplate, CWnd::FromHandle(hWndParent), hInst)) { if (m_nFlags & WF_CONTINUEMODAL) { // enter modal loop DWORD dwFlags = MLF_SHOWONIDLE; if (GetStyle() & DS_NOIDLEMSG) dwFlags |= MLF_NOIDLEMSG; VERIFY(RunModalLoop(dwFlags) == m_nModalResult); } // hide the window before enabling the parent, etc. if (m_hWnd != NULL) SetWindowPos(NULL, 0, 0, 0, 0, SWP_HIDEWINDOW| SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_NOZORDER); } } CATCH_ALL(e) { DELETE_EXCEPTION(e); m_nModalResult = -1; } END_CATCH_ALL if (bEnableParent) ::EnableWindow(hWndParent, TRUE); if (hWndParent != NULL && ::GetActiveWindow() == m_hWnd) ::SetActiveWindow(hWndParent); // destroy modal window DestroyWindow(); PostModal(); // unlock/free resources as necessary if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL) UnlockResource(hDialogTemplate); if (m_lpszTemplateName != NULL) FreeResource(hDialogTemplate); return m_nModalResult; }
INT_PTR CMyFileDialog::DoModal() { // From MFC: CFileDialog::DoModal // Uses the OpenFileNameEx structure on Win2k+ set<CWnd*> disabled; theApp.DisableTopLevelWindows(disabled); ASSERT_VALID(this); ASSERT((m_ofn.Flags & OFN_EXPLORER) != 0); ASSERT((m_ofn.Flags & OFN_ENABLEHOOK) == 0); ASSERT(m_ofn.lpfnHook == NULL); // Not using hooks // zero out the file buffer for consistent parsing later ASSERT(AfxIsValidAddress(m_ofn.lpstrFile, m_ofn.nMaxFile)); DWORD nOffset = lstrlen(m_ofn.lpstrFile) + 1; ASSERT(nOffset <= m_ofn.nMaxFile); ZeroMemory(m_ofn.lpstrFile + nOffset, (m_ofn.nMaxFile - nOffset)*sizeof(TCHAR)); // WINBUG: This is a special case for the file open/save dialog, // which sometimes pumps while it is coming up but before it has // disabled the main window. HWND hWndFocus = ::GetFocus(); BOOL bEnableParent = FALSE; m_ofn.hwndOwner = PreModal(); if (m_ofn.hwndOwner != NULL && ::IsWindowEnabled(m_ofn.hwndOwner)) { bEnableParent = TRUE; ::EnableWindow(m_ofn.hwndOwner, FALSE); } AfxUnhookWindowCreate(); _AFX_THREAD_STATE* pThreadState = AfxGetThreadState(); ASSERT(pThreadState->m_pAlternateWndInit == NULL); pThreadState->m_pAlternateWndInit = this; // Install our own hook to subclass the file dialog MyFileDialogData* pMyData = _myFileDlgData.GetData(); ASSERT(pMyData->hHook == NULL); pMyData->hHook = ::SetWindowsHookEx(WH_CBT, &HookProc, NULL, ::GetCurrentThreadId()); int nResult; if (m_bOpenFileDialog) nResult = ::GetOpenFileName(&m_ofn); else nResult = ::GetSaveFileName(&m_ofn); if (nResult) ASSERT(pThreadState->m_pAlternateWndInit == NULL); pThreadState->m_pAlternateWndInit = NULL; ::UnhookWindowsHookEx(pMyData->hHook); pMyData->hHook = NULL; // WINBUG: Second part of special case for file open/save dialog. if (bEnableParent) ::EnableWindow(m_ofn.hwndOwner, TRUE); if (::IsWindow(hWndFocus)) ::SetFocus(hWndFocus); PostModal(); theApp.EnableWindows(disabled); return nResult ? nResult : IDCANCEL; }