语法突出显示的最佳算法(WINAPI)
我在WINAPI上写了一个程序。我必须实现语法高亮显示。此时,I使用以下算法:语法突出显示的最佳算法(WINAPI),winapi,syntax,highlight,richedit,Winapi,Syntax,Highlight,Richedit,我在WINAPI上写了一个程序。我必须实现语法高亮显示。此时,I使用以下算法: void PaintWords(const char *SearchWord,COLORREF rgb) { counter = TabCtrl_GetCurSel(TabControl_hWnd); ft.chrg.cpMin = 0; ft.chrg.cpMax = GetWindowTextLength(hWnd); ft.lpstrText = (LPCSTR)SearchWo
void PaintWords(const char *SearchWord,COLORREF rgb)
{
counter = TabCtrl_GetCurSel(TabControl_hWnd);
ft.chrg.cpMin = 0;
ft.chrg.cpMax = GetWindowTextLength(hWnd);
ft.lpstrText = (LPCSTR)SearchWord; //keyword
do
{
int poe_p = SendMessage(hWnd, EM_FINDTEXTEX, FR_DOWN | FR_WHOLEWORD | FR_MATCHCASE, (LPARAM)&ft);
if(poe_p != -1)
{
int selword = SendMessage(hWnd, EM_EXSETSEL,0,(LPARAM)&ft.chrgText);
ZeroMemory(&chd, sizeof(chd));
chd.cbSize = sizeof(CHARFORMAT);
chd.dwMask = CFM_SIZE | CFM_FACE | CFM_COLOR | CFM_CHARSET;
chd.crTextColor = rgb;
chd.bPitchAndFamily = FW_THIN;
lstrcpy(chd.szFaceName , "Courier New");
SendMessage(hWnd,EM_SETCHARFORMAT,SCF_WORD|SCF_SELECTION,(LPARAM)&chd);
ft.chrg.cpMin = ft.chrgText.cpMax;
}
else
{
break;
}
}while(ft.chrg.cpMin != ft.chrg.cpMax);
}
此代码太慢,因为这不是最佳选项,闪烁可见
我对其他变体感兴趣。我认为您需要使用双缓冲来减少闪烁。除此之外,您不应该对任何窗口(在同一线程内)使用
SendMessage
(甚至PostMessage
)。为什么必须在语法高亮显示绘制代码中进行选择
关于双缓冲区的文章之一是。我遇到了这个项目的RichTextbox,它用作xml编辑器: 它是C#,但发送的消息是可见的 使用此文本框时,在突出显示文本之前,应调用BeginUpdate和SuspendEvents函数
public IntPtr BeginUpdateAndSuspendEvents()
{
// Stop redrawing:
User32.SendMessage(this.Handle, (int) User32.Msgs.WM_SETREDRAW, 0, IntPtr.Zero);
// Stop sending of events:
IntPtr eventMask = User32.SendMessage(this.Handle, User32.Msgs.EM_GETEVENTMASK, 0, IntPtr.Zero);
return eventMask;
}
此函数用于在处理文本时防止重绘,完成编辑后,应调用
public void EndUpdateAndResumeEvents(IntPtr eventMask)
{
// turn on events
User32.SendMessage(this.Handle, User32.Msgs.EM_SETEVENTMASK, 0, eventMask);
// turn on redrawing
User32.SendMessage(this.Handle, User32.Msgs.WM_SETREDRAW, 1, IntPtr.Zero);
NeedRecomputeOfLineNumbers();
this.Invalidate();
}
双缓冲不能解决此问题,因为它不会阻止绘制的发生,在不禁用绘制事件的情况下突出显示编辑器可能会导致程序暂停5分钟以上,具体取决于文件大小和要突出显示的字数。您可以使用EM_STREAMOUT读取整个内容,在内存中编辑它们,然后使用EM_STREAMIN一次性将它们全部写出来。如何在内存中搜索和编辑文本?