標籤:range htm 對象學習 war false simple 錯誤 params 大小
view對象的詳細文檔請看:
https://sciter.com/docs/content/sciter/View.htm
demo9.html代碼如下:
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>view對象學習</title> <style> .br { width: 100%; border: 1px solid #ccc; margin: 10px auto; } </style></head><body><div class="br"> <button id="selFile">選擇檔案</button> <span id="filePath"></span> <br> <button id="selFolder">選擇目錄</button> <span id="folderPath"></span> <br> <button id="moveFile">移動</button> <span id="moveRet"></span></div><div class="br"> <button id="msgbox1">訊息框</button> <br> <button id="msgbox2">資訊框</button> <br> <button id="msgbox3">問題框</button> <br> <button id="msgbox4">警告框</button> <br> <button id="msgbox5">錯誤框</button></div><div class="br"> <button id="dlg1">對話方塊</button></div><div class="br"> <button id="win1">視窗</button> <br></div><div class="br"> <button id="state1">全屏</button> <button id="state2">預設</button> <button id="state3">關閉</button></div><div class="br"> <button id="req">請求</button> 結果: <iframe id="reqRet"></iframe></div><script type="text/tiscript"> self.ready = function() { var file = ""; var folder = ""; //選擇檔案 $(#selFile).on("click", function() { //檔案篩選項 const filter = "Image Files (*.jpg,*.jpeg,*.gif,*.png)|*.jpg;*.jpeg;*.gif;*.png|All Files (*.*)|*.*"; //預設尾碼 const defaultExt = "jpg"; //初始化路徑,就是檔案選擇窗開啟時所在路徑 const initialPath = System.path(#USER_DOCUMENTS); //標題 const caption = "選擇圖片"; file = view.selectFile(#open, filter, defaultExt, initialPath, caption ); $(#filePath).text = file; return true; }); //選擇檔案夾 $(#selFolder).on("click", function() { folder = view.selectFolder("選擇你的檔案夾"); $(#folderPath).text = folder; }); //移動檔案 $(#moveFile).on("click", function() { if(file && folder) { //調用go中定義的函數進行移動檔案處理 var ret = view.moveFile(file, folder); $(#moveRet).text = ret; } }); //訊息框 $(#msgbox1).on("click", function() { view.msgbox(#alert, "我是訊息框"); }); //資訊框 $(#msgbox2).on("click", function() { view.msgbox(#information, "我是資訊框", "資訊框"); }); //問題框 $(#msgbox3).on("click", function() { view.msgbox(#question, "請選擇是或否", "問題框", //按鈕組 [{id:#yes,text:"是"},{id:#no,text:"否"}], //onLoad,視窗載入時調用 function(root) { }, //onClose,視窗關閉時調用 function(root, id) { //id表示你選擇按鈕的ID if(id == "yes") { view.msgbox(#alert, "你選擇的yes"); } else { view.msgbox(#alert, "你選擇的no"); } }); }); //警告框 $(#msgbox4).on("click", function() { view.msgbox(#warning, "我是警告框"); }); //錯誤框 $(#msgbox5).on("click", function() { view.msgbox(#error, "我是錯誤框"); }); //新對話方塊 $(#dlg1).on("click", function() { var dlg = view.dialog({ //載入到對話方塊中的URL,可載入本地檔案 url: self.url("simple.html"), //對話方塊的x,y座標 x: 50, y: 50, //視窗的寬度,高度 width: 200, height: 200, //是否是客戶區,如果為真,那麼x,y,width,height作為客戶區座標,而不是視窗 client: false, //對話方塊參數 parameters: {"test":"test"}, //對話方塊標題 caption: "新對話方塊", //相對於螢幕視窗對齊 alignment: 3, }); }); //新視窗 $(#win1).on("click", function() { var win = view.window({ //視窗類別型 //View.FRAME_WINDOW 架構視窗 //View.TOOL_WINDOW 工具視窗 //View.POPUP_WINDOW 快顯視窗 type: View.TOOL_WINDOW, //載入到視窗中的URL,可載入本地檔案 url: self.url("simple.html"), //視窗的x,y座標 x: 200, y: 200, //視窗的寬度,高度 width: 400, height: 400, //是否是客戶區,如果為真,那麼x,y,width,height作為客戶區座標,而不是視窗 client: false, //視窗狀態 //View.WINDOW_SHOWN 顯示 //View.WINDOW_HIDDEN 隱藏 //View.WINDOW_MINIMIZED 最小化 //View.WINDOW_MAXIMIZED 最大化 //View.WINDOW_FULL_SCREEN 全屏 state: View.WINDOW_SHOWN, //視窗參數 parameters: {"test":"test"}, //視窗標題 caption: "新視窗", //相對於螢幕視窗對齊 alignment: 3, }); //訪問視窗中設定的參數 stdout.println(win.parameters["test"]); }); //修改當前視窗的狀態 $(#state1).on("click", function() { //全屏 view.state = View.WINDOW_FULL_SCREEN; }); $(#state2).on("click", function() { //預設 view.state = View.WINDOW_SHOWN; }); $(#state3).on("click", function() { //關閉 view.close(); }); //view的事件響應 //視圖視窗大小發生改變時 view.on("sizing", function(sizingParams) { view.dump("大小改變", sizingParams.x, sizingParams.y, sizingParams.width, sizingParams.height); }); //視圖視窗移動時 view.on("moving", function(movingParams) { view.dump("移動改變", movingParams.x, movingParams.y, movingParams.width, movingParams.height); }); //視圖視窗狀態改變時 view.on("statechange", function() { view.dump(view.state); }); //要求方法類似jquery中的ajax $(#req).on("click", function() { view.request({ //請求類型(#get, #post, #put, #delete) type: #get, //請求url url: "http://www.163.com", //協議(#basic, #multipart, #json) protocol: #basic, params: { "test": "test" }, //成功回呼函數 success: function(data,status) { $(#reqRet).html = data; }, //失敗回呼函數 error: function(err,status) { } }); }); };</script></body></html>
demo9.go代碼如下:
package main;import ("github.com/sciter-sdk/go-sciter/window""github.com/sciter-sdk/go-sciter""log""os""strings""path/filepath""io/ioutil""fmt")func defFunc(w *window.Window) {//註冊dump函數方便在tis指令碼中列印資料w.DefineFunction("dump", func(args ...*sciter.Value) *sciter.Value {for _, v := range args {fmt.Print(v.String() + " ");}fmt.Println();return sciter.NullValue();});//處理檔案移動w.DefineFunction("moveFile", func(args ...*sciter.Value) *sciter.Value {file := args[0].String();folder := args[1].String();//去掉路徑左邊的file://file = strings.TrimLeft(file, "file://");//擷取檔案名稱fileName := filepath.Base(file);//讀取檔案資料data, err := ioutil.ReadFile(file);if err != nil {return sciter.NewValue(err.Error());}//寫入檔案資料err = ioutil.WriteFile(filepath.Join(folder, fileName), data, os.ModePerm);if err == nil {//刪除原檔案os.Remove(file);return sciter.NewValue("移動成功");} else {return sciter.NewValue("移動失敗");}});}func main() {w, err := window.New(sciter.DefaultWindowCreateFlag, sciter.DefaultRect);if err != nil {log.Fatal(err);}w.LoadFile("demo9.html");w.SetTitle("view對象學習");defFunc(w);w.Show();w.Run();}
顯示結果如下:
go語言使用go-sciter建立案頭應用(七) view對象常用方法,檔案選擇,視窗彈出,請求