我正在嘗試使用richedit實現語法高亮顯示編輯器,它與當前選定的行很好地工作,但我可能會丟失一些東西。 CRichEdit是我自己的richedit控制器的包裝器實現,問題似乎是即使我確保使用代碼生成的選定範圍是我通過EM_EXGETSEL消息得到的,但文本沒有被正確選擇。 選擇似乎增加1,因爲線條下降,所以我決定ed_source.sendMessage(EM_LINEFROMCHAR,pos,0)到部分解決問題的範圍,除了一些幾何線,其中着色似乎有一次或定位之前和真正的適當的,所以這就是爲什麼我的事情我可能不會理解的東西。語法突出顯示richedit控件不能正常工作
void parse(WIN::CRichEdit &ed_source, bool curseline)
{
int pos, offset = 0;
char delimiter[]={" \n\r(){};"}, *tok, *start;
CStringA s;
CString text;
CWnd api;
if(curseline){
ed_source.getLine(ed_source.getRow() - 1, text);
offset = ed_source.sendMessage(EM_LINEINDEX, -1, 0);
}else{
text = ed_source.getCaption();
}
s = text;
start = s.c_str();
if(!start) return;
tok = strtok(s.c_str(), delimiter);
CHARRANGE cr = ed_source.getSelecteRange();
ed_source.sendMessage(EM_HIDESELECTION, 1, 0) ;
CHARRANGE range;
while(tok)
{
int len = strlen(tok);
pos = (tok - start);
int x = ed_source.sendMessage(EM_LINEFROMCHAR, pos, 0);
range.cpMin = offset + pos - x;
range.cpMax = range.cpMin + len;
ed_source.selectRange(range);
if(isReserved(tok)){
ed_source.setTextStyle(true, false);
ed_source.setTextColor(keyboardColor);
}else
if(isType(tok)){
ed_source.setTextStyle(false, false);
ed_source.setTextColor(typeColor);
}else {
ed_source.setTextStyle(false, true);
ed_source.setTextColor(textColor);
}
tok = strtok(0, delimiter);
}
ed_source.sendMessage(EM_HIDESELECTION, 0, 0) ;
ed_source.selectRange(cr);
}
只是更具體的時刻,我呼籲上述功能是在加載文本後立即。我認爲你可能想看看上面的一些功能的實現,所以他們在這裏。
CHARRANGE CRichEdit::getSelecteRange()
{
CHARRANGE crg = {0} ;
sendMessage(EM_EXGETSEL, 0, (LPARAM)&crg);
return crg;
}
void CRichEdit::selectRange(const CHARRANGE &cr)
{
sendMessage(EM_EXSETSEL, 0, (LPARAM) &cr);
}
void CRichEdit::setTextColor(COLORREF col)
{
CHARFORMAT format;
memset(&format, 0, sizeof(CHARFORMAT));
format.cbSize = sizeof(CHARFORMAT);
format.dwMask = CFM_COLOR;
format.crTextColor = col;
sendMessage(EM_SETCHARFORMAT, SCF_SELECTION, (LPARAM) &format);
}
在richedit控件頂部的編輯器中高亮顯示語法將無法工作。您將永遠回頭瀏覽已經格式化的文本,然後重新進行處理。 –