Browse Source

实现菜单栏应用

master
VIVIMAN 3 years ago
parent
commit
090e4c56ef
  1. 35
      golang_learn/data_func/go_to_listen/demo01/main.go
  2. 53
      golang_learn/data_func/go_to_listen/demo02/main.go
  3. 5
      golang_learn/data_func/go_to_tray/demo03/build.bat
  4. 225
      golang_learn/data_func/go_to_tray/demo03/main.go
  5. BIN
      golang_learn/data_func/go_to_tray/demo03/main.ico
  6. 22
      golang_learn/data_func/go_to_tray/demo03/main.manifest
  7. BIN
      golang_learn/data_func/go_to_tray/demo03/main.syso
  8. 15
      golang_learn/data_func/go_to_tray/demo04/main.go

35
golang_learn/data_func/go_to_listen/demo01/main.go

@ -0,0 +1,35 @@
package main
import (
"bufio"
"fmt"
"io"
"log"
"net"
)
func handleConnection(conn net.Conn) {
br := bufio.NewReader(conn)
for {
data, err := br.ReadString('\n')
if err == io.EOF {
break
}
fmt.Printf("%s", data)
fmt.Fprintf(conn, "OK\n")
}
conn.Close()
}
func main() {
ln, err := net.Listen("tcp", ":54022")
if err != nil {
panic(err)
}
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal("get client connection error: ", err)
}
go handleConnection(conn)
}
}

53
golang_learn/data_func/go_to_listen/demo02/main.go

@ -0,0 +1,53 @@
package main
import (
"fmt"
"net"
"os"
"time"
)
// 获取IP和端口
func getIpPort() []string {
// 根据接收参数个数,定义动态数组,
ipPorts := make([]string, len(os.Args)-1)
i := 0
for index, value := range os.Args {
//排除脚本名称
if index == 0 {
continue
}
//写入数组
ipPorts[i] = value
i++
}
return ipPorts
}
// 检测端口
func checkPorts(ipPorts []string) {
now := time.Now().Format("2006-01-02 15:04:05")
for _, ipPort := range ipPorts {
// 检测端口
conn, err := net.DialTimeout("tcp", ipPort, 3*time.Second)
if err != nil {
fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!")
} else {
if conn != nil {
fmt.Println("["+now+"]", ipPort, "端口已开启(success)!")
conn.Close()
} else {
fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!")
}
}
}
}
func main() {
ret := getIpPort()
if len(ret) == 0 {
ret = []string{":35017", ":54022"}
}
checkPorts(ret)
}

5
golang_learn/data_func/go_to_tray/demo03/build.bat

@ -0,0 +1,5 @@
set GO111MODULE=on
set GOARCH=386
rsrc -manifest main.manifest -ico main.ico -o main.syso
go generate
go build -ldflags="-s -w -H=windowsgui" -o 꽉데으-頓契솰왠.exe

225
golang_learn/data_func/go_to_tray/demo03/main.go

@ -0,0 +1,225 @@
package main
import (
"log"
"net"
"os/exec"
"runtime"
"syscall"
"time"
"github.com/braintree/manners"
"github.com/lxn/walk"
)
type MyWindow struct {
*walk.MainWindow
ni *walk.NotifyIcon
}
func NewMyWindow() *MyWindow {
mw := new(MyWindow)
var err error
mw.MainWindow, err = walk.NewMainWindow()
checkError(err)
return mw
}
func (mw *MyWindow) AddNotifyIcon() {
var err error
mw.ni, err = walk.NewNotifyIcon(mw)
checkError(err)
_ = mw.ni.SetVisible(true)
icon, err := walk.NewIconFromResourceId(3)
checkError(err)
_ = mw.SetIcon(icon)
_ = mw.ni.SetIcon(icon)
// 数据库 服务
mw.addDbMenu()
// 应用程序 服务
mw.addCxMenu()
// 其他快捷键
mw.addOther()
}
func init() {
}
func main() {
mw := NewMyWindow()
mw.AddNotifyIcon()
mw.Run()
}
func checkError(err error) {
if err != nil {
log.Fatal(err)
}
}
// open opens the specified URL in the default browser of the user.
func OpenUrl(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
c := exec.Command(cmd, args...)
c.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}
return c.Start()
}
// addOther 增加其他按钮操作
func (mw *MyWindow) addOther() {
mw.addAction(nil, "打开应用").Triggered().Attach(func() {
_ = OpenUrl("http://localhost:54022/qggwy")
})
helpMenu := mw.addMenu("帮助")
mw.addAction(helpMenu, "升级").Triggered().Attach(func() {
walk.MsgBox(mw, "即将打开网页", "点击《确认》进行下载安装组工软件!", walk.MsgBoxIconInformation)
_ = OpenUrl("https://www.12371.cn/zgrjxz/gwywx/")
})
mw.addAction(helpMenu, "关于").Triggered().Attach(func() {
walk.MsgBox(mw, "关于", "辅助使用公务员系统服务", walk.MsgBoxIconInformation)
})
mw.addAction(nil, "退出").Triggered().Attach(func() {
mw.ni.Dispose()
mw.Dispose()
walk.App().Exit(0)
})
}
// addDbMenu 增加数据库服务按钮操作
func (mw *MyWindow) addDbMenu() {
dbMenu := mw.addMenu("数据服务")
startDbAction := mw.addAction(dbMenu, "启动")
stopDbAction := mw.addAction(dbMenu, "停止")
_ = stopDbAction.SetEnabled(false)
startDbAction.Triggered().Attach(func() {
ok := manners.Close()
if !ok {
setEnableAndCheck(startDbAction, stopDbAction, true, false)
mw.msgbox("启动", "启动失败!", walk.MsgBoxIconError)
} else {
setEnableAndCheck(startDbAction, stopDbAction, false, true)
mw.msgbox("启动", "启动成功.", walk.MsgBoxIconInformation)
}
})
stopDbAction.Triggered().Attach(func() {
ok := manners.Close()
if !ok {
setEnableAndCheck(startDbAction, stopDbAction, false, true)
mw.msgbox("停止", "停止失败!", walk.MsgBoxIconError)
} else {
setEnableAndCheck(startDbAction, stopDbAction, true, false)
mw.msgbox("停止", "停止成功.", walk.MsgBoxIconInformation)
}
})
go AddListen(startDbAction, stopDbAction, ":35017", 3*time.Second)
}
// addCxMenu 增加程序服务按钮操作
func (mw *MyWindow) addCxMenu() {
cxMenu := mw.addMenu("应用服务")
startCxAction := mw.addAction(cxMenu, "启动")
stopCxAction := mw.addAction(cxMenu, "停止")
_ = stopCxAction.SetEnabled(false)
startCxAction.Triggered().Attach(func() {
ok := manners.Close()
if !ok {
setEnableAndCheck(startCxAction, stopCxAction, true, false)
mw.msgbox("启动", "启动失败!", walk.MsgBoxIconError)
} else {
setEnableAndCheck(startCxAction, stopCxAction, false, true)
mw.msgbox("启动", "启动成功.", walk.MsgBoxIconInformation)
}
})
stopCxAction.Triggered().Attach(func() {
ok := manners.Close()
if ok {
setEnableAndCheck(startCxAction, stopCxAction, false, true)
mw.msgbox("停止", "停止失败!", walk.MsgBoxIconError)
} else {
setEnableAndCheck(startCxAction, stopCxAction, true, false)
mw.msgbox("停止", "停止成功.", walk.MsgBoxIconInformation)
}
})
go AddListen(startCxAction, stopCxAction, ":54022", 3*time.Second)
}
// AddListen 增加服务监听,控制按钮状态
func AddListen(startAction *walk.Action, stopAction *walk.Action, address string, timeout time.Duration) {
conn, err := net.DialTimeout("tcp", address, timeout)
if err != nil {
// fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!")
setEnableAndCheck(startAction, stopAction, false, false)
} else {
if conn != nil {
// fmt.Println("["+now+"]", ipPort, "端口已开启(success)!")
setEnableAndCheck(startAction, stopAction, false, true)
_ = conn.Close()
} else {
// fmt.Println("["+now+"]", ipPort, "端口未开启(fail)!")
setEnableAndCheck(startAction, stopAction, true, false)
}
}
}
// setEnableAndCheck 设置按钮状态
func setEnableAndCheck(startAction, stopAction *walk.Action, startBool, stopBool bool) {
_ = startAction.SetEnabled(startBool)
_ = stopAction.SetEnabled(stopBool)
_ = startAction.SetChecked(!startBool)
_ = stopAction.SetChecked(!stopBool)
}
// addMenu 增加分类按钮操作
func (mw *MyWindow) addMenu(name string) *walk.Menu {
helpMenu, err := walk.NewMenu()
checkError(err)
help, err := mw.ni.ContextMenu().Actions().AddMenu(helpMenu)
checkError(err)
_ = help.SetText(name)
return helpMenu
}
// addAction 增加分类子按钮操作
func (mw *MyWindow) addAction(menu *walk.Menu, name string) *walk.Action {
action := walk.NewAction()
_ = action.SetText(name)
if menu != nil {
_ = menu.Actions().Add(action)
} else {
_ = mw.ni.ContextMenu().Actions().Add(action)
}
return action
}
// msgbox 提示框
func (mw *MyWindow) msgbox(title, message string, style walk.MsgBoxStyle) {
_ = mw.ni.ShowInfo(title, message)
walk.MsgBox(mw, title, message, style)
}

BIN
golang_learn/data_func/go_to_tray/demo03/main.ico

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.3 KiB

22
golang_learn/data_func/go_to_tray/demo03/main.manifest

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="*" name="SomeFunkyNameHere" type="win32"/>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="*" publicKeyToken="6595b64144ccf1df" language="*"/>
</dependentAssembly>
</dependency>
<application xmlns="urn:schemas-microsoft-com:asm.v3">
<windowsSettings>
<dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness>
<dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">True</dpiAware>
</windowsSettings>
</application>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

BIN
golang_learn/data_func/go_to_tray/demo03/main.syso

Binary file not shown.

15
golang_learn/data_func/go_to_tray/demo04/main.go

@ -0,0 +1,15 @@
package main
import (
"fmt"
"github.com/braintree/manners"
"net/http"
)
func main() {
err := manners.ListenAndServe(":54022", http.DefaultServeMux)
if err == nil {
fmt.Println("哼")
}
fmt.Println("哈哈")
}
Loading…
Cancel
Save