科技密码
记录网络点滴生活

listview灵活控制单元格颜色的代码(aardio)

listview颜色控制效果

listview颜色控制效果

import win.ui;
/*DSG{{*/
var winform = win.form(text="listview color";right=627;bottom=294;border="dialog frame")
winform.add(
button={cls="button";text="增加项目:指定整行同一颜色";left=393;top=39;right=591;bottom=83;z=2};
button2={cls="button";text="设置颜色";left=395;top=175;right=519;bottom=219;z=3};
button3={cls="button";text="增加项目:每列设置不同颜色";left=392;top=93;right=593;bottom=137;z=4};
listview={cls="listview";left=12;top=13;right=350;bottom=287;bgcolor=16777215;dl=1;dr=1;fullRow=1;gridLines=1;z=1}
)
/*}}*/

import util;

winform.listview.insertColumn("标题3",60,,0x0/*_LVCFMT_LEFT*/) 
winform.listview.insertColumn("标题2",60,,0x0/*_LVCFMT_LEFT*/) 
winform.listview.insertColumn("标题1",150,,0x0/*_LVCFMT_LEFT*/) 

winform.listview.addItem({text={"新增内容";"2";"2,2"};color=0xff0000;colorBk=0xff00});//演示:在onnotify之前增加的项目用color、colorBk设置的颜色无效。
//自绘
winform.listview.onnotify = function(id,code,ptr){ 
    if( code == 0xFFFFFFF4/*_NM_CUSTOMDRAW*/ ){
    	owner._clrText:=..table.array(owner.count,owner.columnCount,0);
		owner._clrTextBk:=..table.array(owner.count,owner.columnCount,0xffffff);
    	if(!owner._modified){//修改addItem函数,增加项目时,更新颜色table
        	owner._modified=true;
        	owner.addItem=..util.before(owner,owner.addItem,function(data){
        		/*如果不考虑可以新增项目时指定每列颜色,这里仅需要2行,此处注释以外的2个if语句删除即可。
        		owner._clrText[owner.count+1]=..table.array(owner.columnCount,data.color:0);
        		owner._clrTextBk[owner.count+1]=..table.array(owner.columnCount,data.colorBk:0xffffff);
        		*/
				if(data.color && type(data.color)==type.table){
					owner._clrText[owner.count+1]=table.array(owner.columnCount,0);
					table.mixin(owner._clrText[owner.count+1],data.color);
				}else {
					owner._clrText[owner.count+1]=..table.array(owner.columnCount,data.color:0);
				}
				if(data.colorBk && type(data.colorBk)==type.table){
					owner._clrTextBk[owner.count+1]=..table.array(owner.columnCount,0xffffff);
					table.mixin(owner._clrTextBk[owner.count+1],data.colorBk);
				}else {
					owner._clrTextBk[owner.count+1]=..table.array(owner.columnCount,data.colorBk:0xffffff);
				}
        	});
    	}
        var lvcd = owner.getNotifyCustomDraw(code,ptr);
        if( lvcd.nmcd.dwDrawStage == 0x10001/*_CDDS_ITEMPREPAINT*/)
                return 0x20/*_CDRF_NOTIFYSUBITEMDRAW*/
        elseif( lvcd.nmcd.dwDrawStage == 1/*_CDDS_PREPAINT*/ ){
                return 0x20/*_CDRF_NOTIFYITEMDRAW*/;
        }
        elseif( lvcd.nmcd.dwDrawStage == ( 0x10001/*_CDDS_ITEMPREPAINT*/ | 0x20000/*_CDDS_SUBITEM*/) ){
			lvcd.clrText=owner._clrText[lvcd.nmcd.dwItemSpec + 1][lvcd.iSubItem + 1];
			lvcd.clrTextBk=owner._clrTextBk[lvcd.nmcd.dwItemSpec + 1][lvcd.iSubItem + 1];
			lvcd.update()
			return 0/*_CDRF_DODEFAULT*/
        }
    }
}
winform.button2.oncommand = function(id,event){//更新指定单元格的颜色
	winform.listview._clrText[1][1]=0xff
	winform.listview._clrTextBk[1][1]=0xff00
    winform.listview.redraw()
}

winform.button.oncommand = function(id,event){
	winform.listview.addItem({text={"新增内容";"2";"2,2"};color=0xff0000;colorBk=0xff00})//指定整列的颜色
}
winform.button3.oncommand = function(id,event){
	//winform.msgbox( winform.button3.text );
	winform.listview.addItem({text={"新增内容";"2";"未指定还是默认颜色"};color={0xff0000;0xff};colorBk={0xff00;0xcccccc}})//指定每列的颜色
}
winform.show() 
win.loopMessage();

赞(2) 打赏
猛牛哥原创:科技密码 » listview灵活控制单元格颜色的代码(aardio)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏