aardio自定义控件一例
By
admin
at 2019-10-07 • 2人收藏 • 4714人看过
感谢 indertust 提供的示例代码
自定义控件可以方便的把一系列的功能封装到一个控件, 这样使用的时候就不需要繁杂的复制黏贴修改了,更方便管理。
例如下面是把采集到的模拟数据显示在界面上, 通道有8个,如果按照常规写法需要拖动很多个控件来实现,封装为一个控件之后只需要业务逻辑即可。
自定义的 analogchannelex.aardio 控件库代码如下;
//analogchannelex 模拟通道 import win.ui; import win.ui.ctrl.static; import win.ui.ctrl.combobox; import win.ui.ctrl.edit; import win.ui.ctrl.metaProperty; //上面导入所有要用到的库 namespace win.ui.ctrl{ //自定义控件 analogchannelex = class{ ctor(parent,tParam){ //添加界面样式 this = ..win.form(text="";right=733;bottom=33;border="none";exmode="none";mode="child";parent=parent;bgcolor=null;max=false;tParam=tParam) this.add( comboboxCell={cls="combobox";left=71;top=7;right=204;bottom=27;edge=1;items={};mode="dropdown";z=1}; comboboxSignalHi={cls="combobox";left=293;top=7;right=347;bottom=27;edge=1;items={};mode="dropdown";z=6}; comboboxSignalLo={cls="combobox";left=227;top=7;right=281;bottom=27;edge=1;items={};mode="dropdown";z=2}; editRandomHi={cls="edit";text="60";left=585;top=7;right=643;bottom=27;edge=1;z=8}; editRandomLo={cls="edit";text="40";left=519;top=7;right=577;bottom=27;edge=1;z=7}; editRangeHi={cls="edit";text="100";left=440;top=7;right=498;bottom=27;edge=1;z=5}; editRangeLo={cls="edit";text="0";left=373;top=7;right=431;bottom=27;edge=1;z=4}; editValue={cls="edit";text="0";left=664;top=7;right=722;bottom=27;edge=1;z=9}; staticId={cls="static";left=14;top=7;right=53;bottom=27;bgcolor=8388608;center=1;z=3} ); //这句必须放最前面,以保证自定义控件在下面可以被修改 ..table.mixin(this@,_metaProperty) import gdi; import win.ui.tooltip; //添加提示信息 var tooltipCtrl = win.ui.tooltip(this); tooltipCtrl.addTool(this.comboboxCell,"变量",0x10/*_TTF_SUBCLASS*/ ) tooltipCtrl.addTool(this.comboboxSignalHi,"最大信号",0x10/*_TTF_SUBCLASS*/ ) tooltipCtrl.addTool(this.comboboxSignalLo,"最小信号",0x10/*_TTF_SUBCLASS*/ ) tooltipCtrl.addTool(this.editRandomHi,"波动高点",0x10/*_TTF_SUBCLASS*/ ) tooltipCtrl.addTool(this.editRandomLo,"波动低点",0x10/*_TTF_SUBCLASS*/ ) tooltipCtrl.addTool(this.editRangeHi,"量程高点",0x10/*_TTF_SUBCLASS*/ ) tooltipCtrl.addTool(this.editRangeLo,"量程低点",0x10/*_TTF_SUBCLASS*/ ) tooltipCtrl.addTool(this.staticId,"通道 ID",0x10/*_TTF_SUBCLASS*/ ) //从自定义的控件属性框里传递参数到属性 this.staticId.text = tParam.text this.staticId.bgcolor = gdi.RGB(212,208,200) this.bgcolor = gdi.RGB(212,208,200) //填充默认值 this.comboboxSignalHi.add('5') this.comboboxSignalHi.add('20') this.comboboxSignalLo.add('1') this.comboboxSignalLo.add('4') if(tParam.signalRange){ this.signalRange = tParam.signalRange }else{ this.signalRange = {1;5} } if(tParam.range){ this.range = tParam.range }else{ this.range = {0.01;99.9} } if(tParam.simulationRange){ this.simulationRange = tParam.simulationRange }else{ this.simulationRange = {40;60} } if(tParam.cells){ for(i=1;#(tParam.cells);1){ this.comboboxCell.add(tParam.cells[i]) } } }; } namespace analogchannelex{ _metaProperty = ..win.ui.ctrl.metaProperty( range = { _get = function(){ var lo = tonumber(owner.editRangeLo.text); var hi = tonumber(owner.editRangeHi.text) return {lo; hi}; } _set = function(v){ owner.editRangeLo.text = tostring(v[1]) owner.editRangeHi.text = tostring(v[2]) } }; signalRange = { _get = function(){ var lo = tonumber(owner.comboboxSignalLo.text); var hi = tonumber(owner.comboboxSignalHi.text) return {lo; hi}; } _set = function(v){ owner.comboboxSignalLo.text = tostring(v[1]) owner.comboboxSignalHi.text = tostring(v[2]) } }; simulationRange = { _get = function(){ var lo = tonumber(owner.editRandomLo.text); var hi = tonumber(owner.editRandomHi.text) return {lo; hi}; } _set = function(v){ owner.editRandomLo.text = tostring(v[1]) owner.editRandomHi.text = tostring(v[2]) } }; ) } }
使用的时候只需要拖拽出custom控件出来, 然后修改类属性为analogchannelex即可
其他属性修改参考库内定义.
使用示例代码如下 winform.aardio
import console; console.open() //定义控制台的行和列数 颜色 execute("mode con cols=160 lines=500") execute("color 0A") var oh = console.getOutPutHandle() var ih = console.getInputHandle() console.modifyMode(ih,0,0x40) p = function(...){ console.print(time(), ...) } d = console.dump dj = console.dumpJson vd = console.varDump import win.ui; import win.ui.ctrl.analogchannelex; /*DSG{{*/ var winform = win.form(text="自定义控件测试";right=760;bottom=514) winform.add( ac1={cls="analogchannelex";text="1";left=30;top=34;right=733;bottom=67;bgcolor=32768;z=1}; ac2={cls="analogchannelex";text="2";left=30;top=81;right=733;bottom=114;bgcolor=32768;z=2}; ac3={cls="analogchannelex";text="3";left=30;top=129;right=733;bottom=162;bgcolor=32768;z=3}; ac4={cls="analogchannelex";text="4";left=30;top=176;right=733;bottom=209;bgcolor=32768;z=4}; ac5={cls="analogchannelex";text="5";left=30;top=224;right=733;bottom=257;bgcolor=32768;z=5}; ac6={cls="analogchannelex";text="6";left=30;top=271;right=733;bottom=304;bgcolor=32768;z=6}; ac7={cls="analogchannelex";text="7";left=30;top=319;right=733;bottom=352;bgcolor=32768;z=7}; ac8={cls="analogchannelex";text="8";left=30;top=366;right=733;bottom=399;bgcolor=32768;z=8}; button={cls="button";text="Dump 所有通道";left=50;top=424;right=166;bottom=457;z=9}; buttonDumpChannel1={cls="button";text="Dump 第一通道";left=334;top=465;right=453;bottom=498;z=10}; buttonDumpChannel2={cls="button";text="Dump 第二通道";left=195;top=467;right=314;bottom=500;z=13}; buttonTestChannel1={cls="button";text="测试第一通道";left=334;top=423;right=453;bottom=456;z=12}; buttonTestChannel2={cls="button";text="测试第二通道";left=195;top=423;right=314;bottom=456;z=11} ) /*}}*/ dumpInfo = function(){ for(name,ctrl in winform.eachControl("analogchannelex") ){ p(name, ..table.tostring(ctrl.signalRange), ..table.tostring(ctrl.range), ..table.tostring(ctrl.simulationRange)) } } winform.button.oncommand = function(id,event){ dumpInfo() } winform.buttonDumpChannel1.oncommand = function(id,event){ var ctrl = winform.ac1 p(..table.tostring(ctrl.signalRange), ..table.tostring(ctrl.range), ..table.tostring(ctrl.simulationRange)) } winform.buttonTestChannel2.oncommand = function(id,event){ var ctrl = winform.ac2 p('ctrl.signalRange', ctrl.signalRange) d(ctrl.signalRange) ctrl.signalRange = {math.random(1, 100);math.random(1, 100)} ctrl.range = {math.random(1, 100);math.random(1, 100)} ctrl.simulationRange = {math.random(1, 100);math.random(1, 100)} p('ctrl.signalRange', ctrl.signalRange) d(ctrl.signalRange) //d(ctrl) } winform.buttonTestChannel1.oncommand = function(id,event){ var ctrl = winform.ac1 p('ctrl.signalRange', ctrl.signalRange) d(ctrl.signalRange) ctrl.signalRange = {math.random(1, 100);math.random(1, 100)} ctrl.range = {math.random(1, 100);math.random(1, 100)} ctrl.simulationRange = {math.random(1, 100);math.random(1, 100)} ctrl.comboboxCell.text = string.random(10) p('ctrl.signalRange', ctrl.signalRange) d(ctrl.signalRange) //d(ctrl) ctrl.comboboxSignalHi.text = math.random(100, 200) } winform.buttonDumpChannel2.oncommand = function(id,event){ var ctrl = winform.ac2 p(..table.tostring(ctrl.signalRange), ..table.tostring(ctrl.range), ..table.tostring(ctrl.simulationRange)) } winform.show(); win.loopMessage(); return winform;
注意:
自定义的控件名 必须全部小写字母
完整示例工程代码下载:
链接:https://pan.baidu.com/s/1uDWeR-WHUhTQBiue1_-S4g
提取码:7iam
复制这段内容后打开百度网盘手机App,操作更方便哦
5 个回复 | 最后更新于 2023-05-30
登录后方可回帖
你的aadio 编辑器背景颜色是黑色的么?