📄 dlgdata.cpp
字号:
else
{
TRACE0("Warning: skipping non-radio button in group.\n");
}
hWndCtrl = ::GetWindow(hWndCtrl, GW_HWNDNEXT);
} while (hWndCtrl != NULL &&
!(GetWindowLong(hWndCtrl, GWL_STYLE) & WS_GROUP));
}
/////////////////////////////////////////////////////////////////////////////
// Listboxes, comboboxes
void AFXAPI DDX_LBString(CDataExchange* pDX, int nIDC, CString& value)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
{
int nIndex = (int)::SendMessage(hWndCtrl, LB_GETCURSEL, 0, 0L);
if (nIndex != -1)
{
int nLen = (int)::SendMessage(hWndCtrl, LB_GETTEXTLEN, nIndex, 0L);
::SendMessage(hWndCtrl, LB_GETTEXT, nIndex,
(LPARAM)(LPVOID)value.GetBufferSetLength(nLen));
}
else
{
// no selection
value.Empty();
}
value.ReleaseBuffer();
}
else
{
// set current selection based on data string
if (::SendMessage(hWndCtrl, LB_SELECTSTRING, (WPARAM)-1,
(LPARAM)(LPCTSTR)value) == LB_ERR)
{
// no selection match
TRACE0("Warning: no listbox item selected.\n");
}
}
}
void AFXAPI DDX_LBStringExact(CDataExchange* pDX, int nIDC, CString& value)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
{
DDX_LBString(pDX, nIDC, value);
}
else
{
// set current selection based on data string
int i = (int)::SendMessage(hWndCtrl, LB_FINDSTRINGEXACT, (WPARAM)-1,
(LPARAM)(LPCTSTR)value);
if (i < 0)
{
// no selection match
TRACE0("Warning: no listbox item selected.\n");
}
else
{
// select it
SendMessage(hWndCtrl, LB_SETCURSEL, i, 0L);
}
}
}
void AFXAPI DDX_CBString(CDataExchange* pDX, int nIDC, CString& value)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
{
// just get current edit item text (or drop list static)
int nLen = ::GetWindowTextLength(hWndCtrl);
if (nLen > 0)
{
// get known length
::GetWindowText(hWndCtrl, value.GetBufferSetLength(nLen), nLen+1);
}
else
{
// for drop lists GetWindowTextLength does not work - assume
// max of 255 characters
::GetWindowText(hWndCtrl, value.GetBuffer(255), 255+1);
}
value.ReleaseBuffer();
}
else
{
// set current selection based on model string
if (::SendMessage(hWndCtrl, CB_SELECTSTRING, (WPARAM)-1,
(LPARAM)(LPCTSTR)value) == CB_ERR)
{
// just set the edit text (will be ignored if DROPDOWNLIST)
AfxSetWindowText(hWndCtrl, value);
}
}
}
void AFXAPI DDX_CBStringExact(CDataExchange* pDX, int nIDC, CString& value)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
{
DDX_CBString(pDX, nIDC, value);
}
else
{
// set current selection based on data string
int i = (int)::SendMessage(hWndCtrl, CB_FINDSTRINGEXACT, (WPARAM)-1,
(LPARAM)(LPCTSTR)value);
if (i < 0)
{
// just set the edit text (will be ignored if DROPDOWNLIST)
AfxSetWindowText(hWndCtrl, value);
}
else
{
// select it
SendMessage(hWndCtrl, CB_SETCURSEL, i, 0L);
}
}
}
void AFXAPI DDX_LBIndex(CDataExchange* pDX, int nIDC, int& index)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
index = (int)::SendMessage(hWndCtrl, LB_GETCURSEL, 0, 0L);
else
::SendMessage(hWndCtrl, LB_SETCURSEL, (WPARAM)index, 0L);
}
void AFXAPI DDX_CBIndex(CDataExchange* pDX, int nIDC, int& index)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
index = (int)::SendMessage(hWndCtrl, CB_GETCURSEL, 0, 0L);
else
::SendMessage(hWndCtrl, CB_SETCURSEL, (WPARAM)index, 0L);
}
void AFXAPI DDX_Scroll(CDataExchange* pDX, int nIDC, int& value)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
value = GetScrollPos(hWndCtrl, SB_CTL);
else
SetScrollPos(hWndCtrl, SB_CTL, value, TRUE);
}
void AFXAPI DDX_Slider(CDataExchange* pDX, int nIDC, int& value)
{
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (pDX->m_bSaveAndValidate)
value = (int) ::SendMessage(hWndCtrl, TBM_GETPOS, 0, 0l);
else
::SendMessage(hWndCtrl, TBM_SETPOS, TRUE, value);
}
/////////////////////////////////////////////////////////////////////////////
// Range Dialog Data Validation
AFX_STATIC void AFXAPI _AfxFailMinMaxWithFormat(CDataExchange* pDX,
long minVal, long maxVal, LPCTSTR lpszFormat, UINT nIDPrompt)
// error string must have '%1' and '%2' strings for min and max values
// wsprintf formatting uses long values (format should be '%ld' or '%lu')
{
ASSERT(lpszFormat != NULL);
if (!pDX->m_bSaveAndValidate)
{
TRACE0("Warning: initial dialog data is out of range.\n");
return; // don't stop now
}
TCHAR szMin[32];
TCHAR szMax[32];
wsprintf(szMin, lpszFormat, minVal);
wsprintf(szMax, lpszFormat, maxVal);
CString prompt;
AfxFormatString2(prompt, nIDPrompt, szMin, szMax);
AfxMessageBox(prompt, MB_ICONEXCLAMATION, nIDPrompt);
prompt.Empty(); // exception prep
pDX->Fail();
}
//NOTE: don't use overloaded function names to avoid type ambiguities
void AFXAPI DDV_MinMaxByte(CDataExchange* pDX, BYTE value, BYTE minVal, BYTE maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
_AfxFailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%u"),
AFX_IDP_PARSE_INT_RANGE);
}
void AFXAPI DDV_MinMaxShort(CDataExchange* pDX, short value, short minVal, short maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
_AfxFailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%ld"),
AFX_IDP_PARSE_INT_RANGE);
}
void AFXAPI DDV_MinMaxInt(CDataExchange* pDX, int value, int minVal, int maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
_AfxFailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%ld"),
AFX_IDP_PARSE_INT_RANGE);
}
void AFXAPI DDV_MinMaxLong(CDataExchange* pDX, long value, long minVal, long maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
_AfxFailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%ld"),
AFX_IDP_PARSE_INT_RANGE);
}
void AFXAPI DDV_MinMaxUInt(CDataExchange* pDX, UINT value, UINT minVal, UINT maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
_AfxFailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%lu"),
AFX_IDP_PARSE_INT_RANGE);
}
void AFXAPI DDV_MinMaxDWord(CDataExchange* pDX, DWORD value, DWORD minVal, DWORD maxVal)
{
ASSERT(minVal <= maxVal);
if (value < minVal || value > maxVal)
_AfxFailMinMaxWithFormat(pDX, (long)minVal, (long)maxVal, _T("%lu"),
AFX_IDP_PARSE_INT_RANGE);
}
void AFXAPI DDV_MinMaxSlider(CDataExchange* pDX, DWORD value, DWORD minVal, DWORD maxVal)
{
ASSERT(minVal <= maxVal);
if (!pDX->m_bSaveAndValidate)
{
if (minVal > value || maxVal < value)
{
#ifdef _DEBUG
int nIDC = GetWindowLong(pDX->m_hWndLastControl, GWL_ID);
TRACE1("Warning: initial dialog data is out of range in control ID %d.\n", nIDC);
#endif
return; // don't stop now
}
}
::SendMessage(pDX->m_hWndLastControl, TBM_SETRANGEMIN, FALSE, (LPARAM) minVal);
::SendMessage(pDX->m_hWndLastControl, TBM_SETRANGEMIN, TRUE, (LPARAM) maxVal);
}
/////////////////////////////////////////////////////////////////////////////
// Max Chars Dialog Data Validation
void AFXAPI DDV_MaxChars(CDataExchange* pDX, CString const& value, int nChars)
{
ASSERT(nChars >= 1); // allow them something
if (pDX->m_bSaveAndValidate && value.GetLength() > nChars)
{
TCHAR szT[32];
wsprintf(szT, _T("%d"), nChars);
CString prompt;
AfxFormatString1(prompt, AFX_IDP_PARSE_STRING_SIZE, szT);
AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_STRING_SIZE);
prompt.Empty(); // exception prep
pDX->Fail();
}
else if (pDX->m_hWndLastControl != NULL && pDX->m_bEditLastControl)
{
// limit the control max-chars automatically
::SendMessage(pDX->m_hWndLastControl, EM_LIMITTEXT, nChars, 0);
}
}
/////////////////////////////////////////////////////////////////////////////
// Special DDX_ proc for subclassing controls
void AFXAPI DDX_Control(CDataExchange* pDX, int nIDC, CWnd& rControl)
{
if (rControl.m_hWnd == NULL) // not subclassed yet
{
ASSERT(!pDX->m_bSaveAndValidate);
HWND hWndCtrl = pDX->PrepareCtrl(nIDC);
if (!rControl.SubclassWindow(hWndCtrl))
{
ASSERT(FALSE); // possibly trying to subclass twice?
AfxThrowNotSupportedException();
}
#ifndef _AFX_NO_OCC_SUPPORT
else
{
// If the control has reparented itself (e.g., invisible control),
// make sure that the CWnd gets properly wired to its control site.
if (pDX->m_pDlgWnd->m_hWnd != ::GetParent(rControl.m_hWnd))
rControl.AttachControlSite(pDX->m_pDlgWnd);
}
#endif //!_AFX_NO_OCC_SUPPORT
}
}
/////////////////////////////////////////////////////////////////////////////
// Global failure dialog helpers (used by database classes)
void AFXAPI AfxFailMaxChars(CDataExchange* pDX, int nChars)
{
TCHAR lpszTemp[32];
wsprintf(lpszTemp, _T("%d"), nChars);
CString prompt;
AfxFormatString1(prompt, AFX_IDP_PARSE_STRING_SIZE, lpszTemp);
AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_STRING_SIZE);
prompt.Empty(); // exception prep
pDX->Fail();
}
void AFXAPI AfxFailRadio(CDataExchange* pDX)
{
CString prompt;
AfxFormatStrings(prompt, AFX_IDP_PARSE_RADIO_BUTTON, NULL, 0);
AfxMessageBox(prompt, MB_ICONEXCLAMATION, AFX_IDP_PARSE_RADIO_BUTTON);
prompt.Empty(); // exception prep
pDX->Fail();
}
/////////////////////////////////////////////////////////////////////////////
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -