• 调试代码

    import debug.log;
    debug.log.setPath("/log.txt");//设置路径
    debug.log.dump(var) //支持普通变量,table等
    
  • 随机在表中取出N个元素,php中有官方代码

    math.randomize(); //随机种子
    var table_rand = function(tab, num = 1){
          if(type(tab) != type.table){ 
                  var bynum = tab;
                  tab = {}
                  for(i=1; bynum; 1){
                          table.push(tab,i);
                  }
          }
          var newtab={};
          if(!num) num = #tab; 
          for(i=1;num;1){
                  var index = math.random(1,#tab);
                  table.push(newtab, tab[index]);
                  table.remove(tab, index);
          }
      return newtab;
    }
    
  • 输入对话框

    var inputx=function(tishi,title="输入内容"){
      import win.inputBox;
    var inputBox = win.inputBox(mainForm)
    inputBox.text = title
    inputBox.info.text = tishi
    var name = inputBox.doModal();
    return name
    };
    
  • 创建文件夹,根据月份新建

    var tm = time.now();
      tm.format="%Y%m"
      var ym = tostring(tm);
      path="C:\app\caddy\"+ym;
      if(!io.exist(path)){
      fsys.createDir(path)
      }
    
  • 读取json接口文件,下载图片

    import web.rest.jsonLiteClient;
      url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=8&mkt=en-US"
      var http = web.rest.jsonLiteClient();
      var api = http.api(url).get()["images"];
      for k,v in api{
          var enddate =v["enddate"];
          var urls = "https://cn.bing.com/"+v["url"];
          }
    var data = http.api(urls); //下载图片
    data.receiveFile("1.jpg").get();
    
  • 设置壁纸

    vv = "C:\app\caddy\wallpaper\0.jpg";
    sys.wallpaper.setCurrent(vv);
    
  • 设置快捷键,全局有效,只要程序运行,类似qq截图功能快捷键 这里只需要修改CTRL 和Q 改其他快捷键

    var hkId;
    mainForm.unreghotkey(hkId);
    hkId = mainForm.reghotkey(    
          function(id,mod,vk){
          process.execute("/res/jietu.exe")
          }
          ,0x2/*_MOD_CTRL*/,'Q'# 
      );
    
  • 创建定时任务,比如每分钟运行一次

    import win.taskScheduler;
    var taskScheduler = win.taskScheduler( mainForm );
    var task1  = taskScheduler.create("每30分钟",function(){
    //具体执行功能
    } )
    task1.interval = {  
     minute = 30;//每30分钟切换一次
    }
    taskScheduler.start();
    
  • 创建托盘和托盘菜单

    mainForm.tray = win.util.tray(mainForm,"/res/ico.ico") ;
    mainForm.tray.tip ="鼠标移动上边提示"
    mainForm.tray.pop('托盘',"通知");
    mainForm.onTrayMessage = {
      [0x205/*_WM_RBUTTONUP*/  ] = function(wParam){ 
         win.setForeground(mainForm.hwnd)
          mainForm.popmenu = win.ui.popmenu(mainForm);//创建弹出菜单
      
          mainForm.popmenu.add('截图Ctrl+Q',function(id){
              process.execute("/res/jietu.exe")
          });
      
          mainForm.popmenu.add();//添加菜单横线
      
          mainForm.popmenu.add('关闭显示器',function(id){
               mainForm.setTimeout( 
          function(){ 
              ::User32.SendNotifyMessage( 0xFFFF/*_HWND_BROADCAST*/ ,0x112/*_WM_SYSCOMMAND*/, 0xF170/*_SC_MONITORPOWER*/ ,2);
          },200
      )    
          });
          //开机启动功能
          import win.reg;
          var reg = win.reg("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run")
          var istrue,_ = reg.queryValue("yoby");
          istrue = !!istrue
          var ids = mainForm.popmenu.add('开机启动',function(id){    
          if(istrue==false){
              reg.setSzValue("yoby", io._exepath )
      
              }else{
      
              reg.delValue("yoby");
      
              }
              reg.close();        
          });
          mainForm.popmenu.check(ids,istrue,0/*_MF_BYCOMMAND*/);
          mainForm.popmenu.add('&退出',function(id){ mainForm.close() })        
      
          mainForm.popmenu.popup();
          mainForm.popmenu.close();
      };
      [0x202/*_WM_LBUTTONUP*/] = function(wParam){ 
      
      };
      [0x203/*_WM_LBUTTONDBLCLK*/] = function(wParam){ //双击
      
           win.setForeground(mainForm.hwnd)
           mainForm.show(true) 
      };
      [0x404/*_PARAM_DESTROY*/] = function(wParam){ 
      
      };
      [0x405/*_PARAM_CLICKED*/] = function(wParam){ 
      
      };
    }
      
    
  • 限制程序只能运行一个

    var atom,hwnd= mainForm.atom("2475484B-9952-466B-AAA1-BD209D02D63C");
    if(!atom){
     win.setForeground(mainForm.hwnd);
      win.quitMessage();return;
    }
      
    
  • 程序退出提示

    mainForm.onClose = function(hwnd,message,wParam,lParam){
      if(win.msgboxTest("是否退出程序")==true){
      mainForm.close()
      }else{
      mainForm.show(false);
      return true;
      }
    }
    
  • 判断exe是否运行,运行的话直接关闭

    if(process.find("caddy.exe")){
      process.kill("caddy.exe")
      win.msgboxTimeout("caddy停止成功")
      }else{
      win.msgboxTimeout("应用未运行")
      }
    
  • 访问网址,记事本打开文本文件,运行bat批处理隐藏黑窗口

    process.openUrl("http://localhost")
    process.execute("notepad","1.txt")
    process.execute("app.php.bat",'','',0x0/*_SW_HIDE*/)
    
  • 打开控制面板

    process.popen.cmd("control");