73

Golang Walk 托盘菜单

 5 years ago
source link: https://studygolang.com/articles/19210?amp%3Butm_medium=referral
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

最近在用Golang开发一个客户端。客户端打开之后,会打开系统浏览器然后弹出一个网页。

目前已经写的差不多了,但是有个问题疑惑着我,Golang有没有类似C# contextMenu的东西呢,我在 github.com 上看到了 walk .

一个非常不错的golang gui开发框架,这个实现了类似于C# ContextMenu的东西

下面是一个非常简单的小示例:

package main
import (
  "github.com/lxn/walk"
  "log"
)
func GuiInit()  {
  mw, err := walk.NewMainWindow()
  if err != nil { 
    log.Fatal(err)
  }
//托盘图标文件
  icon, err := walk.Resources.Icon("./icon.ico")
  if err != nil {
    log.Fatal(err)
  }
  ni, err := walk.NewNotifyIcon(mw)
  if err != nil {
    log.Fatal(err)
  }
  defer ni.Dispose()
  if err := ni.SetIcon(icon); err != nil {
    log.Fatal(err)
  }
  if err := ni.SetToolTip("鼠标在icon上悬浮的信息."); err != nil {
    log.Fatal(err)
  }
  ni.MouseDown().Attach(func(x, yint, button walk.MouseButton) { 
    if button != walk.LeftButton {
      return
    }
    if err := ni.ShowCustom("Walk 任务栏通知标题","walk 任务栏通知内容"); err != nil {
      og.Fatal(err)
    }
  })
  exitAction := walk.NewAction()
  if err := exitAction.SetText("右键icon的菜单按钮"); err != nil {
    log.Fatal(err)
  }
//Exit 实现的功能
  exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
  if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
    log.Fatal(err)
  }
  if err := ni.SetVisible(true); err != nil {
    log.Fatal(err)
  }
  if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
    log.Fatal(err)
  }
  mw.Run()
}

如上例所示,这就完成了一个非常简单的任务托盘栏的程序

改天有时间,我会深入研究这个功能怎么实现的,谢谢观看文章


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK