16,473
社区成员
发帖
与我相关
我的任务
分享
#include"editcolor.h"
#include <stdio.h>
#include <ctype.h>
#include <list>
#include <string>
#include <map>
#include <iterator>
using namespace std;
#define LEN 256
struct ToKen
{
string word;
int start,end;
};
//全局变量
list<ToKen>ToKenList;//保存源码中的单词
int IChar[LEN]={0};//常量返回是否是_ 数字或字母
char szTEXT[MAXWORDS];//保存可见的源码
map<string,int>KeyWordMap;
LRESULT CALLBACK(*pProc)(HWND hWnd , UINT uMsg, WPARAM wParam, LPARAM lParam);
void init(HINSTANCE hinst,HWND hWnd)
{
ReadKeyWordFromFile();
HWND ChildhWnd= CreateWindow("richedit20a", "", WS_CHILD|
WS_VISIBLE|ES_MULTILINE|WS_BORDER|WS_HSCROLL|WS_VSCROLL,
10, 20,350,350,hWnd, HMENU(IDC_EDIT), hinst, NULL);
pProc=(LRESULT CALLBACK(*)(HWND, UINT, WPARAM , LPARAM))
SetWindowLong(ChildhWnd,GWL_WNDPROC,LONG(EditProc));
//*pProc函数指针保存 richedit 的callback
IChar['_']=true;
for(int i='a';i<='z';i++)
{
IChar[i]=true;
}
for(int i='A';i<='Z';i++)
{
IChar[i]=true;
}
}
LRESULT CALLBACK EditProc(HWND hWnd , UINT uMsg, WPARAM wParam, LPARAM lParam)
{
static PAINTSTRUCT ps;
POINT pt;
static HDC hDC;
RECT rt;
HFONT font;
switch(uMsg)
{
case WM_PAINT:
{
font=(HFONT)GetStockObject(SYSTEM_FONT);
SendMessage(hWnd,WM_SETFONT,(WPARAM) font,0);
pProc(hWnd,uMsg,wParam,lParam);
GetShowWords(hWnd,szTEXT);
}
break;
default:
pProc(hWnd,uMsg,wParam,lParam);
}
}
//得到正在显示的所有字符
void GetShowWords(HWND hWnd, char *szTEXT)
{
TEXTRANGE tr;
static POINTL pt;
static RECT rt;
static DWORD wordpos;
static DWORD linepos;
DWORD firstcharpos,lastcharpos;
SendMessage(hWnd,EM_GETRECT,0,LPARAM(&rt));
pt.x=rt.left;pt.y=rt.top;
wordpos =SendMessage(hWnd,EM_CHARFROMPOS,0,LPARAM(&pt));
linepos=SendMessage(hWnd,EM_LINEFROMCHAR,WPARAM(wordpos),0);
firstcharpos=SendMessage(hWnd,EM_LINEINDEX,WPARAM(linepos),0);
pt.x=rt.right;pt.y=rt.bottom;
lastcharpos=SendMessage(hWnd,EM_CHARFROMPOS,0,LPARAM(&pt));
tr.chrg.cpMin=firstcharpos;tr.chrg.cpMax=lastcharpos;
tr.lpstrText=szTEXT;
SendMessage(hWnd,EM_GETTEXTRANGE,0,(LPARAM)&tr);
ReadToken(szTEXT,firstcharpos,lastcharpos);
SetCharColor(hWnd,szTEXT,firstcharpos,lastcharpos);
}
//设置关键字的字体
通过drawtext 画上去盖掉原来的字体
void SetCharColor(HWND hWnd,char *szTEXT,int start,int end)
{
if (start >= end)
return;
HDC hdc=GetDC(hWnd);
RECT rt;
GetClientRect(hWnd,&rt);
DWORD ppt;
SetBkMode(hdc,TRANSPARENT);
SetTextColor(hdc,RGB(255,0,0));
HideCaret(hWnd);
for(list<ToKen>::iterator it=ToKenList.begin();it!=ToKenList.end();it++)
{
if(KeyWordMap.find( it->word)!=KeyWordMap.end())
{
ppt=SendMessage(hWnd,EM_POSFROMCHAR,WPARAM(it->start),0);
rt.left= LOWORD(ppt);
rt.top= HIWORD(ppt);
rt.top+=2;
DrawText(hdc,it->word.c_str(),it->end,&rt,0);
}
}
ShowCaret(hWnd);
ReleaseDC(hWnd,hdc);
}
//把代码分解成单词
int ReadToken(char *szText,int begin,int end)
{
ToKenList.clear();
int k=0;int len=end-begin;
ToKen Temp;
while(k<len)
{
if(IChar[szText[k]])
{
Temp.word.clear();
Temp.start=k+begin;
while(IChar[szText[k]]&&k<len)
{
Temp.word+=szText[k];
k++;
}
Temp.end=Temp.word.size();
ToKenList.push_back(Temp);
}
else
{
k++;
}
}
}
//从keyword.txt 中读取关键字
void ReadKeyWordFromFile()
{
FILE *fp=fopen("keyword.txt","r");
if(NULL==fp)
return ;
char szKey[LEN];
string strKey;
while(fscanf(fp,"%s",szKey)>0)
{
strKey=szKey;
KeyWordMap.insert(map<string,int>::value_type(strKey,1));
}
fclose(fp);
}