虚表 —— 绘制整行背景进度条功能

光庆 1天前 88

基础用法:

import win.ui;
import godking.vlistEx;
import thread.works
/*DSG{{*/
var mainForm = win.form(text="aardio form";right=1031;bottom=551)
mainForm.add(
button2={cls="button";text="随机进度赋值";left=408;top=480;right=560;bottom=528;z=2};
vlist={cls="vlistEx";left=10;top=10;right=1024;bottom=464;db=1;dl=1;dr=1;dt=1;edge=1;transparent=1;z=1}
)
/*}}*/
mainForm.show();
var t = { fields={"序号","网址","文件","进度"} };
for(i=1;30;1){
    ..table.push(t,{"序号"="[@rowindex]"});
}
mainForm.vlist.setTable(t,/*列标题*/,{scale=true,1,1,1,2}/*列宽*/,1/*对齐方式*/,/*字典转为数组*/,/*字符串转为Utf8*/,/*是否克隆新表*/);
mainForm.vlist.fillParent(-1/*列号支持负数*/,-1/*滚动条计算方式-1或-2*/);

//使用下面的函数,启用绘制整行背景进度条功能
mainForm.vlist.setRowProgress(true,4);

//设置随机进度值
mainForm.button2.oncommand = function(id,event){
	//数字开头的,可转为数值。仔细对比下列几行的处理效果
	mainForm.vlist.setCellText( 4/*行号*/,4/*列号或字典key*/,math.random(0,100)/*单元格文本*/);
	mainForm.vlist.setCellText( 5/*行号*/,4/*列号或字典key*/,"非数值文本:"++math.random(0,100)/*单元格文本*/);
	mainForm.vlist.setCellText( 6/*行号*/,4/*列号或字典key*/,math.random(0,100)++":我也是数值,因为我数字开头"/*单元格文本*/);
	mainForm.vlist.setCellText( 7/*行号*/,4/*列号或字典key*/,"我是一串文本"/*单元格文本*/);
}
win.loopMessage();


使用内置函数启用绘制整行背景进度条,多线程下载示例:


import win.ui;
import godking.vlistEx;
import thread.works
/*DSG{{*/
var mainForm = win.form(text="aardio form";right=1031;bottom=551)
mainForm.add(
button={cls="button";text="停止下载";left=296;top=480;right=448;bottom=528;z=2};
button2={cls="button";text="开始下载";left=520;top=480;right=672;bottom=528;z=3};
vlist={cls="vlistEx";left=10;top=10;right=1024;bottom=464;db=1;dl=1;dr=1;dt=1;edge=1;transparent=1;z=1}
)
/*}}*/

var t = { fields={"序号","url","保存文件","下载进度"} };
for(i=1;30;1){
    ..table.push(t,{"序号"="[@rowindex]"});
}

mainForm.vlist.setTable(t,/*列标题*/,{scale=true,0.5,5,3,2}/*列宽*/,1/*对齐方式*/,/*字典转为数组*/,/*字符串转为Utf8*/,true/*是否克隆新表*/);
mainForm.vlist.fillParent();
..io.createDir("\down");

//调用默认函数,启用绘制整行背景进度条功能
mainForm.vlist.setRowProgress(true,4);

import thread.command
thread.command.instance().setinfo = function(row,v,url,file){
   if #url mainForm.vlist.setCellText(row,2,url);
   if #file mainForm.vlist.setCellText(row,3,file);
   mainForm.vlist.setCellText(row,4,v);
}

var work = thread.works(5,
    function(param) {
        if thread.var("stop").get() return ;
        import godking.http;
        import thread.command;
        thread.command.setinfo(param.index,"开始 "++param.index,param.url);
        var t,n={info = "超时或终止"},0;
        var callback = function(id,v,all,url,file){
            thread.command.setinfo(id,..math.round((v:0)*100/all,2)++"%",url,file);
            return thread.var("stop").get();
        }; 
        do{
            n++;
            if n>3 or thread.var("stop").get() break; // 下载失败重试,共3次后结束。
            _,_,t = godking.http({
                url = param.url; 
                callback = callback; 
                callbackID = param.index;
                file = "\down\QQ_"+param.index+".exe"; 
                fileResume = true; 
            })
        } while ( !t.success and t.info!=="下载被终止" )
        if t.success===true {
            thread.command.setinfo(param.index,"下载完毕",param.url,t.file);
        } elseif t.success===false {
            thread.command.setinfo(param.index,"下载失败:"++(t.info:"")++..math.round(t.downsize*100/t.allsize,2)++"%",param.url,t.file);
        } else {
           thread.command.setinfo(param.index,"下载失败:"++(t.info:""));
        }
        return ;
    } 
);
mainForm.show();

mainForm.button.oncommand = function(id,event){
    thread.var("stop").set(true);
}

mainForm.button2.oncommand = function(id,event){
    thread.var("stop").set(false);
    mainForm.vlist.setTable(t,false/*列标题*/,/*列宽*/,/*对齐方式*/,/*字典转为数组*/,/*字符串转为Utf8*/,true/*是否克隆新表*/);
    for(i=1;50;1){
        if thread.var("stop").get() return ;
        work.push({index=i,url="https://dldir1.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.15_240902_x86_01.exe"});
    }
}

mainForm.onClose = function(hwnd,message,wParam,lParam){
    import process
    process().terminate();
} 

// 定时刷新虚表
mainForm.vlist.autoRedraw = false;
mainForm.setInterval( 
    function(){
        mainForm.vlist.redraw(true);
    },200
)

win.loopMessage();


高级点的用法,使用事件函数启用绘制整行背景进度条,多线程下载示例:


import win.ui;
import godking.vlistEx;
import thread.works
/*DSG{{*/
var mainForm = win.form(text="aardio form";right=1031;bottom=551)
mainForm.add(
button={cls="button";text="停止下载";left=296;top=480;right=448;bottom=528;z=2};
button2={cls="button";text="开始下载";left=520;top=480;right=672;bottom=528;z=3};
vlist={cls="vlistEx";left=10;top=10;right=1024;bottom=464;db=1;dl=1;dr=1;dt=1;edge=1;transparent=1;z=1}
)
/*}}*/

var t = { fields={"序号","url","保存文件","下载进度"} };
for(i=1;30;1){
    ..table.push(t,{"序号"="[@rowindex]"});
}

mainForm.vlist.setTable(t,/*列标题*/,{scale=true,0.5,5,3,2}/*列宽*/,1/*对齐方式*/,/*字典转为数组*/,/*字符串转为Utf8*/,true/*是否克隆新表*/);
mainForm.vlist.fillParent();
..io.createDir("\down");

//使用事件函数,实现绘制整行背景进度条功能
mainForm.vlist.onDrawCellRectBg = function(row,col,hdc,rect,bkcolor,text,font,align,valign,recall){
	//成功绘制进度条的直接返回
	if mainForm.vlist.drawRowProgrss(row,4,hdc,rect) return true;
	//否则将下载完毕的单元格改变背景色
	if owner.getCellText(row,"下载进度")=="下载完毕" return false,0xE6F0FA;
}

import thread.command;
thread.command.instance().setinfo = function(row,v,url,file,rowprogress){
   if #url mainForm.vlist.setCellText(row,2,url);
   if #file mainForm.vlist.setCellText(row,3,file);
   mainForm.vlist.setCellText(row,4,v);
}

var work = thread.works(5,
    function(param) {
        if thread.var("stop").get() return ;
        import godking.http;
        import thread.command;
        thread.command.setinfo(param.index,"开始 "++param.index,param.url,,true);
        var t,n={info = "超时或终止"},0;
        var callback = function(id,v,all,url,file){
            thread.command.setinfo(id,..math.round((v:0)*100/all,2)++"%",url,file,null);
            return thread.var("stop").get();
        }; 
        do{
            n++;
            if n>3 or thread.var("stop").get() break; // 下载失败重试,共3次后结束。
            _,_,t = godking.http({
                url = param.url;
                callback = callback;
                callbackID = param.index;
                file = "\down\QQ_"+param.index+".exe";
                fileResume = true;
            })
        } while ( !t.success and t.info!=="下载被终止" );
        if t.success===true {
            thread.command.setinfo(param.index,"下载完毕",param.url,t.file,false);
        } elseif t.success===false {
            thread.command.setinfo(param.index,"下载失败:"++(t.info:"")++..math.round(t.downsize*100/t.allsize,2)++"%",param.url,t.file,false);
        } else {
           thread.command.setinfo(param.index,"下载失败:"++(t.info:""),,,false);
        }
        return ;
    } 
);
mainForm.show();

mainForm.button.oncommand = function(id,event){
    thread.var("stop").set(true);
}

mainForm.button2.oncommand = function(id,event){
    thread.var("stop").set(false);
    mainForm.vlist.setTable(t,false/*列标题*/,/*列宽*/,/*对齐方式*/,/*字典转为数组*/,/*字符串转为Utf8*/,true/*是否克隆新表*/);
    for(i=1;50;1){
        if thread.var("stop").get() return;
        work.push({index=i,url="https://dldir1.qq.com/qqfile/qq/QQNT/Windows/QQ_9.9.15_240902_x86_01.exe"});
    }
}

mainForm.onClose = function(hwnd,message,wParam,lParam){
    import process;
    process().terminate();
} 

// 定时刷新虚表
mainForm.vlist.autoRedraw = false;
mainForm.setInterval(
    function(){
        mainForm.vlist.redraw(true);
    },200
)

win.loopMessage();


最新回复 (1)
  • aika107 15小时前
    0 引用 2

返回