/
main.go
67 lines (64 loc) · 1.45 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
"io/ioutil"
"os"
)
func main() {
var outTE *walk.TextEdit
mw := new(MyMainWindow)
MainWindow{
Title: "简易加密文件",
AssignTo: &mw.MainWindow,
MinSize: Size{300, 140},
Layout: VBox{},
Children: []Widget{
HSplitter{
Children: []Widget{
TextEdit{AssignTo: &outTE, ReadOnly: true},
},
},
PushButton{
Text: "选择文件求反",
OnClicked: func() {
dlg := new(walk.FileDialog)
dlg.Title = "选择文件"
if ok, err := dlg.ShowOpen(mw); err != nil {
outTE.SetText(err.Error())
} else if !ok {
outTE.SetText("未选择文件")
} else {
fileName := dlg.FilePath
file, err := os.Open(fileName)
defer file.Close()
if err != nil {
outTE.SetText("未找到待处理文件")
}
//读取文件内容
plain, _ := ioutil.ReadAll(file)
//求反
for i, b := range plain {
plain[i] = ^b
}
//写入文件
if fileName[len(fileName)-1:] != "+" {
fileName += "+"
} else {
fileName = fileName[:len(fileName)-1]
}
err = ioutil.WriteFile(fileName, plain, 0777)
if err != nil {
outTE.SetText(dlg.FilePath + " 保存转换后文件失败!")
} else {
outTE.SetText(dlg.FilePath + " 文件已转换!")
}
}
},
},
},
}.Run()
}
type MyMainWindow struct {
*walk.MainWindow
}