📄 runtimedlg.cpp
字号:
// make reasonably sure control is child
dwStyle |= WS_CHILD;
dwStyle &= ~WS_POPUP;
HWND hwnd = ::CreateWindowEx(dwExStyle, szClass, szCaption, dwStyle, x, y, cx, cy, *this, NULL, NULL, NULL);
if (hwnd)
{
SetWindowLong(hwnd, GWL_ID, nID);
HFONT hFont = (HFONT)SendMessage(WM_GETFONT); // gets the dialog's font
if (!hFont)
hFont = (HFONT)::GetStockObject(ANSI_VAR_FONT);
::SendMessage(hwnd, WM_SETFONT, (WPARAM)hFont, 0);
if (nIconID && stricmp(szClass, "static") == 0)
{
HICON hIcon = AfxGetApp()->LoadIcon(nIconID);
if (hIcon)
::SendMessage(hwnd, STM_SETIMAGE, IMAGE_ICON, (LPARAM)hIcon);
}
}
else
TRACE("CreateWindowEx(%s) failed. GetLastError returned %08X\n", szClass, GetLastError());
return hwnd;
}
BOOL CRuntimeDlg::CreateControl(CWnd* pWnd, LPCTSTR szCaption, DWORD dwStyle, DWORD dwExStyle,
int x, int y, int cx, int cy, UINT nID, BOOL bDLU, UINT nIconID)
{
ASSERT (!pWnd->GetSafeHwnd());
if (pWnd->GetSafeHwnd())
return FALSE;
CString sClass = GetControlClassName(pWnd);
if (sClass.IsEmpty())
return FALSE;
HWND hwnd = CreateControl(sClass, szCaption, dwStyle, dwExStyle, x, y, cx, cy, nID, bDLU, nIconID);
if (hwnd)
return pWnd->SubclassWindow(hwnd);
// else
return FALSE;
}
BOOL CRuntimeDlg::CreateControl(const RTCONTROL& rtc)
{
if (rtc.m_pWnd)
return CreateControl(rtc.m_pWnd, rtc.m_sCaption, rtc.m_dwStyle, rtc.m_dwExStyle,
rtc.m_rect.left, rtc.m_rect.top, rtc.m_rect.Width(), rtc.m_rect.Height(),
rtc.m_nID, rtc.m_bDLU, rtc.m_nIconID);
else if (!rtc.m_sClass.IsEmpty())
return (NULL != CreateControl(rtc.m_sClass, rtc.m_sCaption, rtc.m_dwStyle, rtc.m_dwExStyle,
rtc.m_rect.left, rtc.m_rect.top, rtc.m_rect.Width(), rtc.m_rect.Height(),
rtc.m_nID, rtc.m_bDLU, rtc.m_nIconID));
else
return FALSE;
}
void CRuntimeDlg::CreateControls()
{
ASSERT (GetSafeHwnd());
if (!GetSafeHwnd())
return;
while (m_lstControls.GetCount())
CreateControl(m_lstControls.RemoveHead());
}
CString CRuntimeDlg::GetClassName(CWnd* pWnd)
{
ASSERT (pWnd);
if (!pWnd)
return "";
CString sName;
::GetClassName(*pWnd, sName.GetBuffer(128), 127);
sName.ReleaseBuffer();
return sName;
}
CString CRuntimeDlg::GetControlClassName(CWnd* pWnd)
{
ASSERT (pWnd);
if (!pWnd)
return "";
// if there is no permanent mapping to this CWnd just return GetClassName()
// but only if it has a window handle attached else it must be a real CWnd
if (pWnd->GetSafeHwnd() && CWnd::FromHandlePermanent(*pWnd) != pWnd)
return GetClassName(pWnd);
CRuntimeClass* pRTClass = pWnd->GetRuntimeClass();
// work our way up the derivation chain till we find a match
while (pRTClass)
{
CString sWinClass, sRTClass = pRTClass->m_lpszClassName;
if (s_mapClasses.Lookup(sRTClass, sWinClass))
return sWinClass;
// try ancestor
#ifdef _AFXDLL
pRTClass = (*pRTClass->m_pfnGetBaseClass)();
#else
pRTClass = pRTClass->m_pBaseClass;
#endif
}
// means we did not find anything
ASSERT (0);
return "";
}
void CRuntimeDlg::BuildClassMap()
{
if (s_mapClasses.GetCount())
return; // already done
s_mapClasses["CStatic"] = "static";
s_mapClasses["CButton"] = "button";
s_mapClasses["CListBox"] = "listbox";
s_mapClasses["CComboBox"] = "combobox";
s_mapClasses["CEdit"] = "edit";
s_mapClasses["CScrollBar"] = "scrollbar";
s_mapClasses["CListCtrl"] = "SysListView32";
s_mapClasses["CTreeCtrl"] = "SysTreeView32";
s_mapClasses["CSpinButtonCtrl"] = "msctls_updown32";
s_mapClasses["CHeaderCtrl"] = "SysHeader32";
s_mapClasses["CSliderCtrl"] = "msctls_trackbar32";
s_mapClasses["CProgressCtrl"] = "msctls_progress32";
s_mapClasses["CHotKeyCtrl"] = "msctls_hotkey32";
s_mapClasses["CToolTipCtrl"] = "tooltips_class32";
s_mapClasses["CTabCtrl"] = "SysTabControl";
s_mapClasses["CAnimateCtrl"] = "SysAnimate32";
s_mapClasses["CToolBarCtrl"] = "toolbarwindow32";
s_mapClasses["CStatusBarCtrl"] = "msctls_statusbar32";
s_mapClasses["CRichEditCtrl"] = "RichEdit20W";
s_mapClasses["CIPAddressCtrl"] = "SysIPAddress32";
s_mapClasses["CDateTimeCtrl"] = "SysDateTimePick32";
s_mapClasses["CMonthCalCtrl"] = "SysMonthCal32";
s_mapClasses["CReBar"] = "ReBarWindow32";
// s_mapClasses[""] = "";
}
void CRuntimeDlg::SetBorders(int nLeft, int nTop, int nRight, int nBottom)
{
m_rBorders.SetRect(max(0, nLeft), max(0, nTop), max(0, nRight), max(0, nBottom));
}
void CRuntimeDlg::SetBordersDLU(int nLeft, int nTop, int nRight, int nBottom)
{
CDlgUnits().ToPixels(nLeft, nTop);
CDlgUnits().ToPixels(nRight, nBottom);
SetBorders(nLeft, nTop, nRight, nBottom);
}
// static
CRect CRuntimeDlg::OffsetCtrl(CWnd* pParent, UINT nCtrlID, int x, int y)
{
CWnd* pCtrl = pParent->GetDlgItem(nCtrlID);
if (pCtrl)
{
CRect rChild;
pCtrl->GetWindowRect(rChild);
pParent->ScreenToClient(rChild);
if (x || y)
{
rChild.OffsetRect(x, y);
pCtrl->MoveWindow(rChild);
}
return rChild;
}
return CRect(0, 0, 0, 0);
}
// static
CRect CRuntimeDlg::ResizeCtrl(CWnd* pParent, UINT nCtrlID, int cx, int cy)
{
CWnd* pCtrl = pParent->GetDlgItem(nCtrlID);
if (pCtrl)
{
CRect rChild, rParent;
pCtrl->GetWindowRect(rChild);
pParent->ScreenToClient(rChild);
pParent->GetClientRect(rParent);
if (cx || cy)
{
rChild.right += cx;
rChild.bottom += cy;
// make sure it also intersects with parent
if (rChild.IntersectRect(rChild, rParent))
pCtrl->MoveWindow(rChild);
}
return rChild;
}
return CRect(0, 0, 0, 0);
}
CRect CRuntimeDlg::OffsetCtrl(UINT nCtrlID, int x, int y)
{
return OffsetCtrl(this, nCtrlID, x, y);
}
CRect CRuntimeDlg::ResizeCtrl(UINT nCtrlID, int cx, int cy)
{
return ResizeCtrl(this, nCtrlID, cx, cy);
}
int CRuntimeDlg::AddRCControls(const CString& sRCControls)
{
CRCCtrlParser rccp(sRCControls);
return rccp.GetRCControls(m_lstControls);
}
BOOL CRuntimeDlg::PreTranslateMessage(MSG* pMsg)
{
// don't handle the enter/cancel keys in child dialogs
if (GetStyle() & WS_CHILD)
{
if (pMsg->message == WM_KEYDOWN && (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_CANCEL))
return FALSE;
}
// also filter out what look like accelerator keys
if (pMsg->message == WM_KEYDOWN && pMsg->wParam != VK_CONTROL && (GetKeyState(VK_CONTROL) & 0x8000))
return FALSE;
return CDialog::PreTranslateMessage(pMsg);
}
void CRuntimeDlg::EnableControls(CWnd* pParent, UINT nCtrlIDFrom, UINT nCtrlIDTo, BOOL bEnable)
{
for (UINT nID = nCtrlIDFrom; nID <= nCtrlIDTo; nID++)
::EnableWindow(::GetDlgItem(*pParent, nID), bEnable);
}
void CRuntimeDlg::SetControlsReadOnly(CWnd* pParent, UINT nCtrlIDFrom, UINT nCtrlIDTo, BOOL bReadOnly)
{
ASSERT (pParent);
for (UINT nID = nCtrlIDFrom; nID <= nCtrlIDTo; nID++)
SetControlReadOnly(::GetDlgItem(*pParent, nID), bReadOnly);
}
void CRuntimeDlg::SetControlReadOnly(HWND hCtrl, BOOL bReadOnly)
{
SetControlState(hCtrl, bReadOnly ? RTCS_READONLY : RTCS_ENABLED);
}
void CRuntimeDlg::SetControlState(CWnd* pParent, UINT nCtrlID, RT_CTRLSTATE nState)
{
SetControlState(::GetDlgItem(*pParent, nCtrlID), nState);
}
void CRuntimeDlg::SetControlState(HWND hCtrl, RT_CTRLSTATE nState)
{
if (hCtrl)
{
switch (nState)
{
case RTCS_ENABLED:
::EnableWindow(hCtrl, TRUE);
if (IsEdit(hCtrl))
::SendMessage(hCtrl, EM_SETREADONLY, FALSE, 0);
break;
case RTCS_DISABLED:
::EnableWindow(hCtrl, FALSE);
break;
case RTCS_READONLY:
if (IsEdit(hCtrl))
{
::EnableWindow(hCtrl, TRUE);
::SendMessage(hCtrl, EM_SETREADONLY, TRUE, 0);
}
else // disable
::EnableWindow(hCtrl, FALSE);
break;
}
}
}
BOOL CRuntimeDlg::IsEdit(HWND hCtrl)
{
CString sClass = CWinClasses::GetClass(hCtrl);
return (CWinClasses::IsClass(sClass, WC_EDIT) ||
CWinClasses::IsClass(sClass, WC_RICHEDIT) ||
CWinClasses::IsClass(sClass, WC_RICHEDIT20));
}
BOOL CRuntimeDlg::IsEdit(CWnd* pParent, UINT nCtrlID)
{
ASSERT (pParent);
return IsEdit(::GetDlgItem(*pParent, nCtrlID));
}
void CRuntimeDlg::ShowControls(CWnd* pParent, UINT nCtrlIDFrom, UINT nCtrlIDTo, BOOL bShow)
{
ASSERT (pParent);
for (UINT nID = nCtrlIDFrom; nID <= nCtrlIDTo; nID++)
::ShowWindow(::GetDlgItem(*pParent, nID), bShow ? SW_SHOW : SW_HIDE);
}
void CRuntimeDlg::ExcludeControls(CWnd* pParent, CDC* pDC, UINT nCtrlIDFrom, UINT nCtrlIDTo)
{
ASSERT (pParent);
for (UINT nID = nCtrlIDFrom; nID <= nCtrlIDTo; nID++)
{
if (::IsWindowVisible(::GetDlgItem(*pParent, nID)))
pDC->ExcludeClipRect(OffsetCtrl(pParent, nID));
}
}
void CRuntimeDlg::EnableControls(UINT nCtrlIDFrom, UINT nCtrlIDTo, BOOL bEnable)
{
EnableControls(this, nCtrlIDFrom, nCtrlIDTo, bEnable);
}
void CRuntimeDlg::ShowControls(UINT nCtrlIDFrom, UINT nCtrlIDTo, BOOL bShow)
{
ShowControls(this, nCtrlIDFrom, nCtrlIDTo, bShow);
}
void CRuntimeDlg::ExcludeControls(CDC* pDC, UINT nCtrlIDFrom, UINT nCtrlIDTo)
{
ExcludeControls(this, pDC, nCtrlIDFrom, nCtrlIDTo);
}
void CRuntimeDlg::OnSetFocus(CWnd* pOldWnd)
{
CDialog::OnSetFocus(pOldWnd);
CWnd* pChild = GetWindow(GW_CHILD);
if (pChild)
pChild->SetFocus();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -