github.com/lxn/walk 写打开文件

使用 github.com/lxn/walk 写一个类似记事本打开文件的操作


===========================================================
editor.go
===========================================================

package main

import (
    "log"

    "editor/Abouter" //关于按钮相关
    "editor/Opener" //打开按钮相关

    "github.com/lxn/walk"
    . "github.com/lxn/walk/declarative"
)

func main() {

    if _,err:= (MainWindow{
        AssignTo: &Opener.Mw.MainWindow,
        Title: "NoteBook",
        MenuItems: []MenuItem{
            Menu{
                Text: "&File",
                Items: []MenuItem{
                    Action{

                        Text: "&NewFile",
                    },
                    Action{
                        AssignTo: &openAction,
                        Text: "&Open",
                        //Image: "/img/open.png",
                        OnTriggered: Opener.Mw.OpenAction_Triggered,
                    },
                    Separator{},
                    Action{
                        Text: "Exit",
                        OnTriggered: func() { Opener.Mw.Close() },
                    },
                },
            },
            Menu{
                Text: "&Help",
                Items: []MenuItem{
                    Action{
                        Text: "About",
                        OnTriggered: Abouter.AboutAction_Triggered,
                    },
                },
            },
        },
        MinSize: Size{320, 240},
        Size: Size{800, 600},
        Layout: VBox{MarginsZero: true},
        Children: []Widget{
            HSplitter{
                Children: []Widget{
                    TextEdit{
                        AssignTo: &Opener.Te,
                        HScroll: true,
                        VScroll: true,
                    },
                },
            },
        },
    }.Run()); err != nil {
        log.Fatal(err)
    }
}


===========================================================
Opener.go
===========================================================

package Opener

import (
    "encoding/csv"
    "log"
    "os"

    "github.com/axgle/mahonia"
    "github.com/golang/glog"
    "github.com/lxn/walk"
)

var Te*walk.TextEdit
var Mw=new(MyMainWindow)

type MyMainWindow struct {
    *walk.MainWindow
    prevFilePath string
}

func (Mw *MyMainWindow)OpenAction_Triggered() {

    err := Te.SetText("")// 以便于清空文本框原有的内容
    if err !=nil {
        glog.Error("SetText() clear content :", err)
    }

    if err:= Mw.OpenFile(); err!=nil {
        log.Print(err)
    }

    //max := te.MaxLength()
    //fmt.Println("max = ", max)

}

func GetCSV(csvPathstring) [][]string {
    file, err:= os.Open(csvPath)
    if err !=nil {
        glog.Error("Error when open file in GetCSV():", err)
        return nil
    }
    defer file.Close()
    decoder := mahonia.NewDecoder("gbk")// 把原来ANSI格式的文本文件里的字符,用gbk进行解码。
    //r := csv.NewReader(file)
    r := csv.NewReader(decoder.NewReader(file))// 这样,最终返回的字符串就是utf-8了。(go只认utf8)
    res, err:= r.ReadAll()
    if err !=nil {
        glog.Error("Error when read csv in GetCSV():", err)
        return nil
    }
    return res
}

func (Mw *MyMainWindow)OpenFile()error {
    dlg :=new(walk.FileDialog)

    //var te *walk.TextEdit

    dlg.FilePath = Mw.prevFilePath
    dlg.Filter = "txt Files (*.txt)|*.txt"
    dlg.Title = "Select an file"

    if ok,err:= dlg.ShowOpen(Mw); err!=nil {
        return err
    } else if!ok {
        return nil
    }
    Mw.prevFilePath = dlg.FilePath
    content :=GetCSV(Mw.prevFilePath)

    for i,_:= range content {
        //te.AppendText(content[i][0])
        AppendChangeText(content[i][0])
    }

    return nil
}

func AppendChangeText(valuestring) {
    start, end:= Te.TextSelection()
    len := Te.TextLength()
    Te.SetTextSelection(len, len)
    Te.ReplaceSelectedText(value+"\n",true)
    Te.SetTextSelection(start, end)
}


=============================================================

Abouter.go

=============================================================

package Abouter

import (
    "editor/Opener"

    "github.com/lxn/walk"
)

func AboutAction_Triggered() {
    walk.MsgBox(Opener.Mw, "About", "Walk Image Viewer Example", walk.MsgBoxIconInformation)
}



 

执行效果图:





  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值