请教:RichEdit着色(语法高亮)

zpzlj178 4月前 429


t={'a','b','aa','bb','bab'}

s=/*a=1
b=2
aa=12
bb=a+b+aa
bab=aabbab(a,b,aa,bb)*/

mainForm.richedit.text=s

var  ys =win.ui.ctrl.CHARFORMAT2()
ys.textColor = 5280 ; 
mainForm.zhese.oncommand = function(id,event){
	for(i=1;#t;1){
		do{
			j=mainForm.richedit.findText(t[i]);
			mainForm.richedit.setSelCharformat(ys,2/*_SCF_WORD*/|1/*_SCF_SELECTION*/)
		}while(j>0)
	
	}
	
}

想法是t中的5个字符在richedit的文本中标记红色,,结果

  1. 不是模式匹配

  2. 好像do循环执行i=1后findText到文件结尾了,不知道怎么改

    其实我是想实现代码编辑+语法着色,请教各位大佬如何修改,或者有什么好方案

最新回复 (4)
  • 蓝图 4月前
    0 2

    使用 .setsel(起始位置,结束位置) 设置选区后着色

    //论坛交流-RichEdit着色(语法高亮)
    import win.ui;
    /*DSG{{*/
    var winform = win.form(text="aardio form";right=759;bottom=469)
    winform.add(
    button={cls="button";text="文本高亮";left=654;top=7;right=752;bottom=58;z=1};
    richedit={cls="richedit";text='a=1\r\nb=2\r\naa=12\r\nbb=a+b+aa\r\nbab=aabbab(a,b,aa,bb)';left=7;top=8;right=646;bottom=461;edge=1;multiline=1;z=2}
    )
    /*}}*/
    
    //定义正则匹配表达式与对应RTF格式二维表
    var table = {
    	{ 'a',{ textColor = 0x0000FF; } };
    	{ 'b',{ textColor = 0x0000FF; } };
    	{ 'aa',{ textColor = 0x0000FF; } };
    	{ 'bb',{ textColor = 0x0000FF; } };
    	{ 'bab',{ textColor = 0x0000FF; } };
    };
    //使用这种方式可以更改每个表达式对应的文字颜色和背景,取消注释后试一下吧
    /*
    var table = {
    	{ 'a',{ backColor = 0xFFFFFF; textColor = 0x0000FF; } };
    	{ 'b',{ backColor = 0xFFFFFF; textColor = 0x2862BB; } };
    	{ 'aa',{ backColor = 0xFFFFFF; textColor = 0x6A9955; } };
    	{ 'bb',{ backColor = 0xFFFFFF; textColor = 0xCE6E3F; } };
    	{ 'bab',{ backColor = 0xCFCFFF; textColor = 0x0000FF; } };
    	{ '\\)',{ backColor = 0xFFFFFF; textColor = 0x0000FF; } };
    	// ↑因重置了新增字符的默认样式,即使最后一个字符是特殊样式的,也不会影响新增字符
    };
    */
    
    import string.regex;//VBS正则表达式支持库
    winform.button.oncommand = function(){
    	
    	var owner = winform.richedit;//这样写代码清晰一点
    	
    	owner.text = owner.text;//清除控件中的RTF格式为纯文本
    	var str = string.crlf( owner.text,' ' );
    	
    	for( i=1;#table;1 ){
    		//创建表达式对象
    		var regexObj = string.regex( table[i][1] );
    		
        	for k,smatch in regexObj.gmatch( str ){
        	    //将字符串中匹配到的字符选中后着色
                owner.setsel( smatch.FirstIndex + 1, smatch.FirstIndex + string.len( smatch.Value ) );
                owner.setSelCharformat( table[i][2] );
        	}
        }
        //重置新增字符的默认样式
        owner.setsel(-1);//无选区,移动光标到指定位置的字符后面
        owner.setSelCharformat( { backColor = 0xFFFFFF; textColor = 0x000003; } );//或者 ↓这样写
        //owner.setCharformat( { backColor = 0xFFFFFF; textColor = 0x000003; },1/*_SCF_SELECTION 选区的*/ );
    }
    
    winform.show();
    win.loopMessage();

    另外你若想在 richedit 中完整实现代码高亮的效果,

    应注意编辑写入时频繁操作带来的实时渲染卡顿问题,

    以及RTF文档能粘贴图片等资源导致着色位置存在差异的问题,

    以及调整格式后恢复光标位置的问题,

    还有需控制存在大量内容响应时滚动条的变化...

    不过很巧,这些问题之前我都解决过,可以参考此贴 https://www.aardio.cc/thread-106-1-1.html

  • zpzlj178 4月前
    0 3
    谢谢解答,我发现从rtf源码解决问题容易一些,但是有一个问题 rft文档源码是有反斜杠\的,richedit.streamin直接载入用双引号的标识的源码字符串可以正常显示, 但是载入字符串变量就加载失败,请教如何解决?
  • 蓝图 4月前
    0 4

    ...有问题时最好把能复现问题的示例代码也贴上来,这样别人理解起来、解决起来也方便些。

    以下是使用 “++”连接运算符 载入字符串变量的方法,也可以使用 格式化字符串  ,与之相关的还有 模板语法 。

    //论坛交流-RichEdit着色(语法高亮)-2
    import win.ui;
    /*DSG{{*/
    var winform = win.form(text="aardio form";right=759;bottom=469)
    winform.add(
    richedit={cls="richedit";text="RichEdit";left=8;top=8;right=661;bottom=464;edge=1;multiline=1;z=1}
    )
    /*}}*/
    
    var value = "\cf3\highlight2 abcdefg\cf0\highlight0 =2\par"; 
    
    winform.richedit.streamIn("{\rtf1\ansi\ansicpg936\deff0\nouicompat\deflang1033\deflangfe2052{\fonttbl{\f0\fnil Segoe UI;}}
    {\colortbl ;\red255\green0\blue0;\red255\green255\blue255;\red187\green98\blue40;\red85\green153\blue106;\red63\green110\blue206;\red255\green207\blue207;}
    {\*\generator Riched20 10.0.19041}\viewkind4\uc1 
    \pard\cf1\highlight2\f0\fs18\lang2052 a\cf0\highlight0 =1\par
    "++ value ++"
    \cf3\highlight2 b\cf0\highlight0 =2\par
    \cf4\highlight2 aa\cf0\highlight0 =12\par
    \cf5\highlight2 bb\cf0\highlight0 =\cf1\highlight2 a\cf0\highlight0 +\cf3\highlight2 b\cf0\highlight0 +\cf4\highlight2 aa\cf0\highlight0\par
    \cf1\highlight6 bab\cf0\highlight0 =\cf4\highlight2 aa\cf5 b\cf1\highlight6 bab\cf0\highlight0 (\cf1\highlight2 a\cf0\highlight0 ,\cf3\highlight2 b\cf0\highlight0 ,\cf4\highlight2 aa\cf0\highlight0 ,\cf5\highlight2 bb\cf1 )\cf0\highlight0\par
    }")
    
    winform.show();
    win.loopMessage();

    另外若你有 <在 richedit 中完整实现代码高亮的效果> 的其他思路,希望在解决问题的同时能分享出来,助力 aardio 社区发展。

  • zpzlj178 4月前
    0 5
    谢谢解答。问题解决了。我用的是python库,返回的是py对象,所以streamin不能识别,tostring一下就好了。 忽视这个的原因是edit.print自动转换了类型所以没问题,而richedit没有自动转换
返回