上海市建設(shè)安全協(xié)會網(wǎng)站j搜索引擎優(yōu)化關(guān)鍵字
有時候,我們會遇到需求,軟件中需要讓選中一個CEdit控件中的文字實(shí)時更新到另一個控件中,實(shí)現(xiàn)效果如下所示:
?代碼如下:
BOOL CEditDemoDlg::PreTranslateMessage(MSG* pMsg)
{
?? ?CEdit* pOldEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
?? ?if (pOldEdit->GetSafeHwnd() == pMsg->hwnd)
?? ?{
?? ??? ?if (WM_LBUTTONUP == pMsg->message)
?? ??? ?{
?? ??? ??? ?CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT2);
?? ??? ??? ?if (pEdit != nullptr)
?? ??? ??? ?{
?? ??? ??? ??? ?int nStartCharContent = -1, nEndCharContent = -1;
?? ??? ??? ??? ?pOldEdit->GetSel(nStartCharContent, nEndCharContent);
?? ??? ??? ??? ?CString strResult;
?? ??? ??? ??? ?//如果位置相同的時候,表示當(dāng)前沒有選擇, 需要清空下測的內(nèi)容
?? ??? ??? ??? ?if (nStartCharContent != nEndCharContent)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?CString strTempText;
?? ??? ??? ??? ??? ?pOldEdit->GetWindowText(strTempText);
?? ??? ??? ??? ??? ?strResult = strTempText.Mid(nStartCharContent, (nEndCharContent - nStartCharContent));
?? ??? ??? ??? ??? ?pEdit->SetWindowText(strResult);
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ?return CDialog::PreTranslateMessage(pMsg);
}