0
点赞
收藏
分享

微信扫一扫

CEdit 选中文字实时更新到另一个控件中


有时候,我们会遇到需求,软件中需要让选中一个CEdit控件中的文字实时更新到另一个控件中,实现效果如下所示:

CEdit 选中文字实时更新到另一个控件中_控件

 代码如下:

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;
                 //如果位置相同的时候,表示当前没有选择, 需要清空下测的内容
                 if (nStartCharContent != nEndCharContent)
                 {
                     CString strTempText;
                     pOldEdit->GetWindowText(strTempText);                    strResult = strTempText.Mid(nStartCharContent, (nEndCharContent - nStartCharContent));
                     pEdit->SetWindowText(strResult);
                 }
             }
         }
     }
     return CDialog::PreTranslateMessage(pMsg);
 }

举报

相关推荐

0 条评论