richedit中显示多彩文字-aardio编程

2023-10-21 10:48:37 浏览数 (1)

richedit显示多种颜色文字效果

aardio中richedit控件默认只能简单设置所有文字的颜色,有时候为了逼格高一点,或者人性化一点,需要用多种颜色显示文字,经过反复研究测试,得出了下面的代码:

代码语言:javascript复制
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=759;bottom=469)
winform.add(
richedit_log={cls="richedit";left=114;top=61;right=585;bottom=320;bgcolor=0;color=15793151;edge=1;multiline=1;z=1}
)
/*}}*/

winform.richedit_log.bgcolor=0;//设置控件背景色.因为不知道为啥直接在控件属性里面设置不起作用.

log_show=function(data,color,bkcolor){
	if(!data) return ;
	var color_tab={y=0xffff;r=0xff;g=0xcc00;f=0xff00ff;w=0xffffff;h=0xcccccc};
	var t=time();
	t.format=" [%H:%M:%S] ";
	t=tostring(t);
	var str=winform.richedit_log.text;
	var num=string.len(str);
	num =1;
	num_r=0;
	for m in string.gmatch(str,"rn") { 
		num_r =1;
	}
	num-=num_r;//文本内含有换行符时,计数会有错,只能这样纠正.
	winform.richedit_log.appendText(t  data  'rn');
	if(color){
		if(type(color)==type.string) color=color_tab[color];
		winform.richedit_log.setsel(num,num string.len(t  data));
		winform.richedit_log.setSelCharformat({textColor=color});
		winform.richedit_log.deselect();
	}
	if(bkcolor){
		if(type(bkcolor)==type.string) bkcolor=color_tab[bkcolor];
		winform.richedit_log.setsel(num,num string.len(t  data));
		winform.richedit_log.setSelCharformat({backColor=bkcolor});
		winform.richedit_log.deselect();
	}
}
log_show("测试文字1111-白色","w");
log_show("测试文字2222-红色","r");
log_show("测试文字2222-绿色","g");
log_show("测试文字2222-黄色","y");
log_show("测试文字2222-红色-白色背景","r","w");
log_show("测试文字2222-白色-绿色背景","w","g");

winform.show() 
win.loopMessage();

经过一鹤改进richedit相关的代码和指导,现在代码可以更简洁,如下:

代码语言:javascript复制
import win.ui;
/*DSG{{*/
var winform = win.form(text="aardio form";right=506;bottom=298)
winform.add(
richedit_log={cls="richedit";left=21;top=18;right=495;bottom=278;bgcolor=0;color=16777215;edge=1;multiline=1;z=1}
)
/*}}*/

log_show=function(data,color,bkcolor){
	if(!data) return ;
	var color_tab={y=0xffff;r=0xff;g=0xcc00;f=0xff00ff;w=0xffffff;h=0xcccccc};
	if(type(color)==type.string) color=color_tab[color];
	if(type(bkcolor)==type.string) bkcolor=color_tab[bkcolor];
	winform.richedit_log.appendText( tostring(time(," [%H:%M:%S] ") ),data,'rn');
	var start = winform.richedit_log.lineToChar(-2);//因为最后一行总是空白的(只有一个换行符),所以应该是设置倒数第二行
	winform.richedit_log.setsel(start,-1);
	winform.richedit_log.setSelCharformat(textColor = color;backColor = bkcolor);
	winform.richedit_log.deselect();
}
log_show("测试文字1111-白色","w");
log_show("测试文字2222-红色","r");
log_show("测试文字2222-绿色","g");
log_show("测试文字2222-黄色","y");
log_show("测试文字2222-红色-白色背景","r","w");
log_show("测试文字2222-白色-绿色背景","w","g");

winform.show() 
win.loopMessage();

0 人点赞