界面如下
。1.上面一個是普通文本框IDC_EDIT1,下面一個是CRICHEDIT(IDC_RICHEDIT2) , 第一個文本框默認內容已產生,但是下面一個文本框用 onchange事件來比對文字,對的用藍色顯示,錯的用紅色,但是結果總是出現上圖顯示的問題,不能根據文字內容的對錯正確的顯示顏色。
ONCHANGE代碼如下,請高手指導(用的是_mbcs編碼):
void CWztestDlg::OnChangeRichedit2()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
CWnd *wnd,*wndstatic;
CString wznum,txt1,txt2,temp1,temp2; //richedit文本框的內容長度,兩個文本框的內容(txt1和txt2)及每次截取的字符(temp1和temp2)
int wzlength;
wnd=GetDlgItem(IDC_RICHEDIT2);
wzlength=wnd->GetWindowTextLength();
wznum.Format("%d",wzlength);
wndstatic=GetDlgItem(IDC_STATIC);
CHARFORMAT cf; //設定字體
this->m_richedit.GetDefaultCharFormat(cf);
cf.dwMask=CFM_COLOR|CFM_OFFSET|CFM_SIZE|CFM_FACE;
cf.dwEffects =CFE_BOLD|CFE_ITALIC;
cf.yHeight =400; //設定高度
strcpy(cf.szFaceName ,_T("隸書"));
cf.cbSize=20;
GetDlgItem(IDC_EDIT1)->GetWindowText(txt1);//獲取第一個文本框內容
wnd->GetWindowText(txt2);
if(txt2.GetLength()>txt1.GetLength())
return;
for(int i=0;i<txt2.GetLength();i=i+1) //兩個文本中內容比對
{ if(IsDBCSLeadByte(txt2.GetAt(i)))
{temp1=txt1.Mid(i,2);
temp2=txt2.Mid(i,2);
}
else
{temp1=txt1.Mid(i,1);
temp2=txt2.Mid(i,1);
}
if(temp1==temp2)
{
//設定字體
cf.crTextColor = RGB(0,0,255); //設定顏色
}
else
{
cf.crTextColor = RGB(255,0,0); //設定顏色
}
if(IsDBCSLeadByte(txt2.GetAt(i)))
{ m_richedit.SetSel(i,2);
i++;
}
else
m_richedit.SetSel(i,1);
m_richedit.SetSelectionCharFormat(cf);
m_richedit.SetSel(txt2.GetLength(),txt2.GetLength()); //游標移動到文本末尾
}
wndstatic->SetWindowText(txt2);
}
uj5u.com熱心網友回復:
先把你的工程切換到Unicode編碼模式~uj5u.com熱心網友回復:
還是老樣子。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/8940.html
標籤:界面
