对话框中如何动态变字体颜色,紧急!

zhanglu11111 2008-10-15 01:22:25
其中我的俩个函数如下,实现不了将关键字,非法字符,常量的变色。尝试过将网上有的变色函数替换我的那个仍然不变色,请问是为什么 应该怎么做才会变色?

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;
}
...全文
110 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
scklotz 2008-10-16
  • 打赏
  • 举报
回复
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;
}


这段代码将IDC_EDIT1变色为clr是没有问题的

建议你先调通以上这段代码,再加入你的语法部分
实在不行你就新建一个工程试试
carylin 2008-10-15
  • 打赏
  • 举报
回复
自己画字不就得了
jia_xiaoxin 2008-10-15
  • 打赏
  • 举报
回复
首先你要在CEdit中实现多种颜色,必须从CEdit派生新的类,然后在新的CMYEdit中实现输出多种前景色的功能。
因为CEdit只能设置一种前景色。

或者你直接用DrawText在对话框上直接画字符,对不同的字符用不同的颜色画,也可以实现你的要求。
returnnofalse 2008-10-15
  • 打赏
  • 举报
回复
一次textout 只能有一种颜色
你可以分成多次textout 输出文本
minorcxx 2008-10-15
  • 打赏
  • 举报
回复
设置前景色

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧