📄 dlgcore.cpp
字号:
if (m_pParentWnd == NULL)
m_pParentWnd = pParentWnd;
m_lpDialogTemplate = lpDialogTemplate;
m_lpDialogInit = lpDialogInit;
return TRUE; // always ok (DoModal actually brings up dialog)
}
HWND CDialog::PreModal()
{
// cannot call DoModal on a dialog already constructed as modeless
ASSERT(m_hWnd == NULL);
// allow OLE servers to disable themselves
CWinApp* pApp = AfxGetApp();
if (pApp != NULL)
pApp->EnableModeless(FALSE);
// find parent HWND
HWND hWnd = CWnd::GetSafeOwner_(m_pParentWnd->GetSafeHwnd(), &m_hWndTop);
// hook for creation of dialog
AfxHookWindowCreate(this);
// return window to use as parent for dialog
return hWnd;
}
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 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;
}
void CDialog::EndDialog(int nResult)
{
ASSERT(::IsWindow(m_hWnd));
if (m_nFlags & (WF_MODALLOOP|WF_CONTINUEMODAL))
EndModalLoop(nResult);
::EndDialog(m_hWnd, nResult);
}
/////////////////////////////////////////////////////////////////////////////
// Standard CDialog implementation
LRESULT CDialog::HandleSetFont(WPARAM wParam, LPARAM)
{
OnSetFont(CFont::FromHandle((HFONT)wParam));
return Default();
}
void CDialog::PreInitDialog()
{
// ignore it
}
LRESULT CDialog::HandleInitDialog(WPARAM, LPARAM)
{
PreInitDialog();
#ifndef _AFX_NO_OCC_SUPPORT
// create OLE controls
COccManager* pOccManager = afxOccManager;
if ((pOccManager != NULL) && (m_pOccDialogInfo != NULL))
{
BOOL bDlgInit;
if (m_lpDialogInit != NULL)
bDlgInit = pOccManager->CreateDlgControls(this, m_lpDialogInit,
m_pOccDialogInfo);
else
bDlgInit = pOccManager->CreateDlgControls(this, m_lpszTemplateName,
m_pOccDialogInfo);
if (!bDlgInit)
{
TRACE0("Warning: CreateDlgControls failed during dialog init.\n");
EndDialog(-1);
return FALSE;
}
}
#endif
// Default will call the dialog proc, and thus OnInitDialog
BOOL bResult = Default();
#ifndef _AFX_NO_OCC_SUPPORT
if (bResult && (m_nFlags & WF_OLECTLCONTAINER))
{
CWnd* pWndNext = GetNextDlgTabItem(NULL);
if (pWndNext != NULL)
{
pWndNext->SetFocus(); // UI Activate OLE control
bResult = FALSE;
}
}
#endif
return bResult;
}
BOOL AFXAPI AfxHelpEnabled()
{
if (AfxGetApp() == NULL)
return FALSE;
// help is enabled if the app has a handler for ID_HELP
AFX_CMDHANDLERINFO info;
// check main window first
CWnd* pWnd = AfxGetMainWnd();
if (pWnd != NULL && pWnd->OnCmdMsg(ID_HELP, CN_COMMAND, NULL, &info))
return TRUE;
// check app last
return AfxGetApp()->OnCmdMsg(ID_HELP, CN_COMMAND, NULL, &info);
}
void CDialog::OnSetFont(CFont*)
{
// ignore it
}
BOOL CDialog::OnInitDialog()
{
// execute dialog RT_DLGINIT resource
BOOL bDlgInit;
if (m_lpDialogInit != NULL)
bDlgInit = ExecuteDlgInit(m_lpDialogInit);
else
bDlgInit = ExecuteDlgInit(m_lpszTemplateName);
if (!bDlgInit)
{
TRACE0("Warning: ExecuteDlgInit failed during dialog init.\n");
EndDialog(-1);
return FALSE;
}
// transfer data into the dialog from member variables
if (!UpdateData(FALSE))
{
TRACE0("Warning: UpdateData failed during dialog init.\n");
EndDialog(-1);
return FALSE;
}
// enable/disable help button automatically
CWnd* pHelpButton = GetDlgItem(ID_HELP);
if (pHelpButton != NULL)
pHelpButton->ShowWindow(AfxHelpEnabled() ? SW_SHOW : SW_HIDE);
return TRUE; // set focus to first one
}
void CDialog::OnOK()
{
if (!UpdateData(TRUE))
{
TRACE0("UpdateData failed during dialog termination.\n");
// the UpdateData routine will set focus to correct item
return;
}
EndDialog(IDOK);
}
void CDialog::OnCancel()
{
EndDialog(IDCANCEL);
}
BOOL CDialog::CheckAutoCenter()
{
// load resource as necessary
LPCDLGTEMPLATE lpDialogTemplate = m_lpDialogTemplate;
HGLOBAL hDialogTemplate = m_hDialogTemplate;
if (m_lpszTemplateName != NULL)
{
HINSTANCE 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);
// determine if dialog should be centered
BOOL bResult = TRUE;
if (lpDialogTemplate != NULL)
{
DWORD dwStyle = lpDialogTemplate->style;
short x;
short y;
if (((DLGTEMPLATEEX*)lpDialogTemplate)->signature == 0xFFFF)
{
// it's a DIALOGEX resource
dwStyle = ((DLGTEMPLATEEX*)lpDialogTemplate)->style;
x = ((DLGTEMPLATEEX*)lpDialogTemplate)->x;
y = ((DLGTEMPLATEEX*)lpDialogTemplate)->y;
}
else
{
// it's a DIALOG resource
x = lpDialogTemplate->x;
y = lpDialogTemplate->y;
}
bResult = !(dwStyle & (DS_CENTER|DS_CENTERMOUSE|DS_ABSALIGN)) &&
x == 0 && y == 0;
}
// unlock/free resources as necessary
if (m_lpszTemplateName != NULL || m_hDialogTemplate != NULL)
UnlockResource(hDialogTemplate);
if (m_lpszTemplateName != NULL)
FreeResource(hDialogTemplate);
return bResult; // TRUE if auto-center is ok
}
/////////////////////////////////////////////////////////////////////////////
// Gray background support
#ifndef _AFX_NO_GRAYDLG_SUPPORT
HBRUSH CDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
// use helper in CWnd
return OnGrayCtlColor(pDC, pWnd, nCtlColor);
}
#endif //!_AFX_NO_GRAYDLG_SUPPORT
/////////////////////////////////////////////////////////////////////////////
// CDialog support for context sensitive help.
LRESULT CDialog::OnCommandHelp(WPARAM, LPARAM lParam)
{
if (lParam == 0 && m_nIDHelp != 0)
lParam = HID_BASE_RESOURCE + m_nIDHelp;
if (lParam != 0)
{
CWinApp* pApp = AfxGetApp();
if (pApp != NULL)
pApp->WinHelp(lParam);
return TRUE;
}
return FALSE;
}
LRESULT CDialog::OnHelpHitTest(WPARAM, LPARAM)
{
if (m_nIDHelp != 0)
return HID_BASE_RESOURCE + m_nIDHelp;
return 0;
}
/////////////////////////////////////////////////////////////////////////////
// CDialog Diagnostics
#ifdef _DEBUG
void CDialog::AssertValid() const
{
CWnd::AssertValid();
}
void CDialog::Dump(CDumpContext& dc) const
{
CWnd::Dump(dc);
dc << "m_lpszTemplateName = ";
if (HIWORD(m_lpszTemplateName) == 0)
dc << (int)LOWORD((DWORD)m_lpszTemplateName);
else
dc << m_lpszTemplateName;
dc << "\nm_hDialogTemplate = " << (UINT)m_hDialogTemplate;
dc << "\nm_lpDialogTemplate = " << (UINT)m_lpDialogTemplate;
dc << "\nm_pParentWnd = " << (void*)m_pParentWnd;
dc << "\nm_nIDHelp = " << m_nIDHelp;
dc << "\n";
}
// diagnostic routine to check for and decode dialog templates
// return FALSE if a program error occurs (i.e. bad resource ID or
// bad dialog styles).
BOOL AFXAPI _AfxCheckDialogTemplate(LPCTSTR lpszResource, BOOL bInvisibleChild)
{
ASSERT(lpszResource != NULL);
HINSTANCE hInst = AfxFindResourceHandle(lpszResource, RT_DIALOG);
HRSRC hResource = ::FindResource(hInst, lpszResource, RT_DIALOG);
if (hResource == NULL)
{
if (HIWORD(lpszResource) != 0)
TRACE1("ERROR: Cannot find dialog template named '%s'.\n",
lpszResource);
else
TRACE1("ERROR: Cannot find dialog template with IDD 0x%04X.\n",
LOWORD((DWORD)lpszResource));
return FALSE;
}
if (!bInvisibleChild)
return TRUE; // that's all we need to check
// we must check that the dialog template is for an invisible child
// window that can be used for a form-view or dialog-bar
HGLOBAL hTemplate = LoadResource(hInst, hResource);
if (hTemplate == NULL)
{
TRACE0("Warning: LoadResource failed for dialog template.\n");
// this is only a warning, the real call to CreateDialog will fail
return TRUE; // not a program error - just out of memory
}
DLGTEMPLATEEX* pTemplate = (DLGTEMPLATEEX*)LockResource(hTemplate);
DWORD dwStyle;
if (pTemplate->signature == 0xFFFF)
dwStyle = pTemplate->style;
else
dwStyle = ((DLGTEMPLATE*)pTemplate)->style;
UnlockResource(hTemplate);
FreeResource(hTemplate);
if (dwStyle & WS_VISIBLE)
{
if (HIWORD(lpszResource) != 0)
TRACE1("ERROR: Dialog named '%s' must be invisible.\n",
lpszResource);
else
TRACE1("ERROR: Dialog with IDD 0x%04X must be invisible.\n",
LOWORD((DWORD)lpszResource));
return FALSE;
}
if (!(dwStyle & WS_CHILD))
{
if (HIWORD(lpszResource) != 0)
TRACE1("ERROR: Dialog named '%s' must have the child style.\n",
lpszResource);
else
TRACE1("ERROR: Dialog with IDD 0x%04X must have the child style.\n",
LOWORD((DWORD)lpszResource));
return FALSE;
}
return TRUE;
}
#endif //_DEBUG
#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif
IMPLEMENT_DYNAMIC(CDialog, CWnd)
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -