dialoghelper.cpp
来自「管理项目进度工具的原代码」· C++ 代码 · 共 740 行 · 第 1/2 页
CPP
740 行
{
CAutoFlag af(m_bInUpdateEx, TRUE);
CDataExchange dx(pWnd, bSaveAndValidate);
DDX_Text(&dx, nIDC, value);
return TRUE;
}
BOOL CDialogHelper::UpdateDataEx(CWnd* pWnd, int nIDC, DWORD& value, BOOL bSaveAndValidate)
{
CAutoFlag af(m_bInUpdateEx, TRUE);
CDataExchange dx(pWnd, bSaveAndValidate);
DDX_Text(&dx, nIDC, value);
return TRUE;
}
BOOL CDialogHelper::UpdateDataEx(CWnd* pWnd, int nIDC, CString& value, BOOL bSaveAndValidate)
{
CAutoFlag af(m_bInUpdateEx, TRUE);
CDataExchange dx(pWnd, bSaveAndValidate);
// this can be used for a variety of control types so we need
// to figure out what nIDC points to and then code accordingly
HWND hCtrl = dx.PrepareCtrl(nIDC);
if (!hCtrl)
return FALSE;
if (CWinClasses::IsEditControl(hCtrl))
DDX_Text(&dx, nIDC, value);
else if (CWinClasses::IsClass(hCtrl, WC_COMBOBOX))
DDX_CBString(&dx, nIDC, value);
else if (CWinClasses::IsClass(hCtrl, WC_LISTBOX))
DDX_LBString(&dx, nIDC, value);
else
{
// ASSERT (0);
return FALSE;
}
return TRUE;
}
BOOL CDialogHelper::UpdateDataExact(CWnd* pWnd, int nIDC, CString& value, BOOL bSaveAndValidate)
{
CAutoFlag af(m_bInUpdateEx, TRUE);
CDataExchange dx(pWnd, bSaveAndValidate);
// this can be used for a variety of control types so we need
// to figure out what nIDC points to and then code accordingly
HWND hCtrl = dx.PrepareCtrl(nIDC);
if (!hCtrl)
return FALSE;
else if (CWinClasses::IsClass(hCtrl, WC_COMBOBOX))
DDX_CBStringExact(&dx, nIDC, value);
else if (CWinClasses::IsClass(hCtrl, WC_LISTBOX))
DDX_LBStringExact(&dx, nIDC, value);
else
{
ASSERT (0);
return FALSE;
}
return TRUE;
}
BOOL CDialogHelper::UpdateDataEx(CWnd* pWnd, int nIDC, float& value, BOOL bSaveAndValidate)
{
CAutoFlag af(m_bInUpdateEx, TRUE);
CDataExchange dx(pWnd, bSaveAndValidate);
DDX_Text(&dx, nIDC, value);
return TRUE;
}
BOOL CDialogHelper::UpdateDataEx(CWnd* pWnd, int nIDC, double& value, BOOL bSaveAndValidate)
{
CAutoFlag af(m_bInUpdateEx, TRUE);
CDataExchange dx(pWnd, bSaveAndValidate);
DDX_Text(&dx, nIDC, value);
return TRUE;
}
BOOL CDialogHelper::UpdateDataEx(CWnd* pWnd, int nIDC, CWnd& ctrl, BOOL bSaveAndValidate)
{
CAutoFlag af(m_bInUpdateEx, TRUE);
CDataExchange dx(pWnd, bSaveAndValidate);
DDX_Control(&dx, nIDC, ctrl);
return TRUE;
}
void CDialogHelper::SetFont(CWnd* pWnd, HFONT hFont, BOOL bRedraw)
{
// don't send to toolbar as it causes all sorts of problems with drop buttons
// but do send to a toolbar's children
if (!pWnd->IsKindOf(RUNTIME_CLASS(CToolBar)))
pWnd->SendMessage(WM_SETFONT, (WPARAM)hFont, bRedraw);
// children
CWnd* pChild = pWnd->GetWindow(GW_CHILD);
while (pChild)
{
SetFont(pChild, hFont, bRedraw);
pChild = pChild->GetNextWindow(GW_HWNDNEXT);
}
}
HFONT CDialogHelper::GetFont(CWnd* pWnd)
{
if (pWnd)
return GetFont(pWnd->GetSafeHwnd());
return (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
}
HFONT CDialogHelper::GetFont(HWND hWnd)
{
if (hWnd)
{
HFONT hFont = (HFONT)::SendMessage(hWnd, WM_GETFONT, 0, 0);
if (hFont)
return hFont;
}
return (HFONT)::GetStockObject(DEFAULT_GUI_FONT);
}
int CDialogHelper::SetComboBoxItems(CComboBox& combo, const CStringArray& aItems)
{
combo.ResetContent();
for (int nItem = 0; nItem < aItems.GetSize(); nItem++)
combo.AddString(aItems[nItem]);
return combo.GetCount();
}
BOOL CDialogHelper::ProcessDialogControlShortcut(const MSG* pMsg)
{
// message id must be WM_KEYDOWN and alt key must be pressed
if (pMsg->message == WM_SYSKEYDOWN && (GetKeyState(VK_MENU) & 0x8000) &&
pMsg->wParam != VK_MENU)
{
// convert shortcut from virtual key to char
UINT nShortcut = MapVirtualKey(pMsg->wParam, 2);
// find the next control having this accelerator
CWnd* pNext = FindNextMatch(CWnd::GetFocus(), nShortcut);
if (pNext)
{
pNext->SetFocus();
return TRUE;
}
}
return FALSE;
}
void CDialogHelper::ClipChild(CDC* pDC, CWnd* pWnd, UINT nChildID)
{
CWnd* pChild = pWnd->GetDlgItem(nChildID);
if (pChild)
{
CRect rChild;
pChild->GetWindowRect(rChild);
pWnd->ScreenToClient(rChild);
pDC->ExcludeClipRect(rChild);
}
}
void CDialogHelper::ClipChild(CDC* pDC, CWnd* pChild)
{
CWnd* pParent = pChild->GetParent();
if (pParent)
{
CRect rChild;
pChild->GetWindowRect(rChild);
pParent->ScreenToClient(rChild);
pDC->ExcludeClipRect(rChild);
}
}
CWnd* CDialogHelper::FindNextMatch(CWnd* pCurrent, UINT nShortcut)
{
// only the brute force method would appear to work here
nShortcut = toupper(nShortcut);
// 1. find the first parent popup window
CWnd* pTop = pCurrent->GetParent();
while (pTop && !(pTop->GetStyle() & WS_CAPTION))
pTop = pTop->GetParent();
if (pTop && pTop != pCurrent)
{
// get a list of all tabbable items within this parent
CTypedPtrList<CPtrList, CWnd*> lstWnds;
CWnd* pFirst = pTop->GetNextDlgTabItem(pTop->GetWindow(GW_CHILD));
CWnd* pChild = pFirst;
while (pChild)
{
lstWnds.AddTail(pChild);
pChild = pTop->GetNextDlgTabItem(pChild);
if (pChild == pFirst)
break;
}
if (lstWnds.GetCount() > 1)
{
// keep moving the head to the tail until pCurrent is at the head
CWnd* pHead = lstWnds.GetHead();
while (pHead != pCurrent && !pHead->IsChild(pCurrent))
{
lstWnds.AddTail(lstWnds.RemoveHead());
pHead = lstWnds.GetHead();
}
// remove pCurrent
lstWnds.RemoveHead();
// now traverse the list looking for preceding static
// labels with a matching accelerator
POSITION pos = lstWnds.GetHeadPosition();
while (pos)
{
pChild = lstWnds.GetNext(pos);
CWnd* pPrev = pChild->GetWindow(GW_HWNDPREV);
if (CWinClasses::IsClass(pPrev->GetSafeHwnd(), WC_STATIC) &&
pPrev->IsWindowVisible())
{
CString sText;
pPrev->GetWindowText(sText);
if (!sText.IsEmpty() && GetShortcut(sText) == nShortcut)
return pChild;
}
}
}
}
// all else
return NULL;
}
UINT CDialogHelper::GetShortcut(const CString& sText)
{
for (int nChar = 0; nChar < sText.GetLength() - 1; nChar++)
{
if (sText[nChar] == '&' && sText[nChar + 1] != '&')
return toupper(sText[nChar + 1]);
}
// no shortcut
return 0;
}
UINT CDialogHelper::MessageBoxEx(CWnd* pWnd, UINT nIDText, UINT nIDCaption, UINT nType)
{
CString sText;
sText.LoadString(nIDText);
return MessageBoxEx(pWnd, sText, nIDCaption, nType);
}
UINT CDialogHelper::MessageBoxEx(CWnd* pWnd, LPCTSTR szText, UINT nIDCaption, UINT nType)
{
CString sCaption;
sCaption.LoadString(nIDCaption);
return pWnd->MessageBox(szText, sCaption, nType);
}
int CDialogHelper::RefreshMaxDropWidth(CComboBox& combo, CDC* pDCRef)
{
int nWidth = CalcMaxTextWidth(combo, 0, TRUE, pDCRef);
combo.SetDroppedWidth(nWidth);
return nWidth;
}
int CDialogHelper::CalcMaxTextWidth(CComboBox& combo, int nMinWidth, BOOL bDropped, CDC* pDCRef)
{
CDC* pDC = pDCRef;
CFont* pOldFont = NULL;
if (!pDC)
{
pDC = combo.GetDC();
pOldFont = pDC->SelectObject(combo.GetFont());
}
CString sText;
int nMaxWidth = nMinWidth;
int nItem = combo.GetCount();
while (nItem--)
{
combo.GetLBText(nItem, sText);
int nWidth = pDC->GetTextExtent(sText).cx;
nMaxWidth = max(nMaxWidth, nWidth);
}
// check window text too
combo.GetWindowText(sText);
int nWidth = pDC->GetTextExtent(sText).cx;
nMaxWidth = max(nMaxWidth, nWidth);
// dropped width needs some more space
if (bDropped)
{
// Add the avg width to prevent clipping
TEXTMETRIC tm;
pDC->GetTextMetrics(&tm);
nMaxWidth += tm.tmAveCharWidth;
// Adjust the width for the vertical scroll bar and the left and right border.
if (combo.GetStyle() & WS_VSCROLL)
nMaxWidth += ::GetSystemMetrics(SM_CXVSCROLL);
nMaxWidth += (2 * ::GetSystemMetrics(SM_CXEDGE));
}
// cleanup
if (!pDCRef)
{
pDC->SelectObject(pOldFont);
combo.ReleaseDC(pDC);
}
return nMaxWidth;
}
BOOL CDialogHelper::IsChildOrSame(HWND hWnd, HWND hwndChild)
{
return (hWnd == hwndChild || ::IsChild(hWnd, hwndChild));
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?