对话框中如何动态变字体颜色,紧急!
其中我的俩个函数如下,实现不了将关键字,非法字符,常量的变色。尝试过将网上有的变色函数替换我的那个仍然不变色,请问是为什么 应该怎么做才会变色?
void CBianyiDlg::OnChangeEdit1()
{
// 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
int i, j = 0, z = 0;
//int key_word_flag = 0, alert_flag = 0, comment_flag = 0, const_flag = 0;
//int n;
char word[20];
char *edit1_content;
clr = RGB(0,0,0);
UpdateData(TRUE);
edit1_content = (char*)(LPCTSTR)m_edit1;
int content_len = strlen(edit1_content);
int word_len = 0;
for(i = 0; i < content_len; i++){
if(edit1_content[i] != ' '){
word[j++]=edit1_content[i];
}
else
{
word_len = j;
word[j] = 0;
j = 0;
int index = 1;
if(!isalnum(word[0]) && word[0] != '_'){
//第一个字符不是字母或是'_'或者数字,是非法字符
clr=RGB(255,0,0);// 红色
}
else{
if(isdigit(word[0]) ){
while(index < word_len && isdigit(word[index])){
index ++;
}
if(index != word_len){
// 开头是数字,但不全是数字,是非法字符
clr=RGB(255,0,0);// 红色
}
else{
// 是常量
clr=RGB(192,192,192);// 灰色
}
}
else{
//第一个字符是字母或者'_'
while(index < word_len && !is_alert_character(word[index])){
index ++;
}
if(index != word_len){
// 出现非法字符
clr=RGB(255,0,0);// 红色
}
else{
//是合法字,那么,是不是关键字?
if(is_keyword(word)){
//是关键字
clr=RGB(166,202,240);// 蓝色
}
else{
//是普通字
}
}
}
}
}
}
word_len = j;
word[j] = 0;
j = 0;
int index = 1;
if(!isalnum(word[0]) && word[0] != '_'){
//第一个字符不是字母或是'_'或者数字,是非法字符
clr=RGB(255,0,0);// 红色
}
else{
if(isdigit(word[0])){
while(index < word_len && isdigit(word[index])){
index ++;
}
if(index != word_len){
// 开头是数字,但不全是数字,是非法字符
clr=RGB(255,0,0);// 红色
}
else{
// 是常量
clr=RGB(192,192,192);// 灰色
}
}
else{
//第一个字符是字母或者'_'
while(index < word_len && !is_alert_character(word[index])){
index ++;
}
if(index != word_len){
// 出现非法字符
clr=RGB(255,0,0);// 红色
}
else{
//是合法字,那么,是不是关键字?
if(is_keyword(word)){
//是关键字
clr=RGB(166,202,240);// 蓝色
}
else{
//是普通字
}
}
}
}
}
bool CBianyiDlg::is_alert_character(char a)
{
int index = 0;
while( index < n_alert_character){
if(alert_character[index] == a){
return true;
}
index ++;
}
return false;
}
bool CBianyiDlg::is_keyword(char *a)
{
int index = 0;
while( index < n_keyword_num){
if(strcmp(keyword[index], a)==0){
return true;
}
index ++;
}
return false;
}
HBRUSH CBianyiDlg::OnCtlColor(CDC *pDC, CWnd *pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(nCtlColor == CTLCOLOR_EDIT){
if(pWnd->GetDlgCtrlID()== IDC_EDIT1){
pDC->SetTextColor(clr);
pDC->SetBkColor(RGB(251, 247, 200));
pDC->SetBkMode(TRANSPARENT);
return hbr;
}
}
// TODO: Return a different brush if the default is not desired
return hbr;
}