Go GUI---lxn/walk 自带demo学习---16.notifyicon通知图标

 简单说明:通知图标demo,原文都有注释,很好理解

需要一个walk.MainWindow 或一个walk.Dialog用于他们的消息循环。但是本例中让MainWindow 不可见。

创建通知图标,并确保在退出时将其清除

// Create the notify icon and make sure we clean it up on exit.
	ni, err := walk.NewNotifyIcon(mw)
	if err != nil {
		log.Fatal(err)
	}
	defer ni.Dispose()

设置图标和工具提示文本

// Set the icon and a tool tip text.
	if err := ni.SetIcon(icon); err != nil {
		log.Fatal(err)
	}
	if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
		log.Fatal(err)
	}

 当按下鼠标左键时,打开气球提示

// When the left mouse button is pressed, bring up our balloon.
	ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
		if button != walk.LeftButton {
			return
		}

		if err := ni.ShowCustom(
			"Walk NotifyIcon Example",
			"There are multiple ShowX methods sporting different icons.",
			icon); err != nil {

			log.Fatal(err)
		}
	})

退出

	// We put an exit action into the context menu.
	exitAction := walk.NewAction()
	if err := exitAction.SetText("E&xit"); err != nil {
		log.Fatal(err)
	}
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
		log.Fatal(err)
	}

通知图标最初是隐藏的,因此我们必须使其可见

	// The notify icon is hidden initially, so we have to make it visible.
	if err := ni.SetVisible(true); err != nil {
		log.Fatal(err)
	}

	// Now that the icon is visible, we can bring up an info balloon.
	if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
		log.Fatal(err)
	}

notifyicon.go

// Copyright 2011 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
	"log"
)

import (
	"github.com/lxn/walk"
)

func main() {
	// We need either a walk.MainWindow or a walk.Dialog for their message loop.
	// We will not make it visible in this example, though.
	mw, err := walk.NewMainWindow()
	if err != nil {
		log.Fatal(err)
	}

	// We load our icon from a file.
	icon, err := walk.Resources.Icon("../img/stop.ico")
	if err != nil {
		log.Fatal(err)
	}

	// Create the notify icon and make sure we clean it up on exit.
	ni, err := walk.NewNotifyIcon(mw)
	if err != nil {
		log.Fatal(err)
	}
	defer ni.Dispose()

	// Set the icon and a tool tip text.
	if err := ni.SetIcon(icon); err != nil {
		log.Fatal(err)
	}
	if err := ni.SetToolTip("Click for info or use the context menu to exit."); err != nil {
		log.Fatal(err)
	}

	// When the left mouse button is pressed, bring up our balloon.
	ni.MouseDown().Attach(func(x, y int, button walk.MouseButton) {
		if button != walk.LeftButton {
			return
		}

		if err := ni.ShowCustom(
			"Walk NotifyIcon Example",
			"There are multiple ShowX methods sporting different icons.",
			icon); err != nil {

			log.Fatal(err)
		}
	})

	// We put an exit action into the context menu.
	exitAction := walk.NewAction()
	if err := exitAction.SetText("E&xit"); err != nil {
		log.Fatal(err)
	}
	exitAction.Triggered().Attach(func() { walk.App().Exit(0) })
	if err := ni.ContextMenu().Actions().Add(exitAction); err != nil {
		log.Fatal(err)
	}

	// The notify icon is hidden initially, so we have to make it visible.
	if err := ni.SetVisible(true); err != nil {
		log.Fatal(err)
	}

	// Now that the icon is visible, we can bring up an info balloon.
	if err := ni.ShowInfo("Walk NotifyIcon Example", "Click the icon to show again."); err != nil {
		log.Fatal(err)
	}

	// Run the message loop.
	mw.Run()
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Walk库中,可以使用`TabPage`和`TabWidget`控件来创建和管理选项卡。下面是一个简单的示例代码,演示如何初始化包含两个选项卡的`TabWidget`: ```go package main import ( "github.com/lxn/walk" . "github.com/lxn/walk/declarative" ) func main() { // 创建一个新的主窗口 mainWindow := new(MyMainWindow) // 创建一个新的TabWidget tabWidget := new(walk.TabWidget) // 创建第一个选项卡 tabPage1 := walk.NewTabPage() tabPage1.SetTitle("Tab Page 1") tabPage1.SetLayout(walk.NewVBoxLayout()) // 添加一些控件到第一个选项卡中 tabPage1.Layout().Add(walk.NewLabel(mainWindow)) tabPage1.Layout().Add(walk.NewPushButton(mainWindow)) // 创建第二个选项卡 tabPage2 := walk.NewTabPage() tabPage2.SetTitle("Tab Page 2") tabPage2.SetLayout(walk.NewVBoxLayout()) // 添加一些控件到第二个选项卡中 tabPage2.Layout().Add(walk.NewLabel(mainWindow)) tabPage2.Layout().Add(walk.NewCheckBox(mainWindow)) // 将两个选项卡添加到TabWidget中 tabWidget.Pages().Add(tabPage1) tabWidget.Pages().Add(tabPage2) // 创建一个新的主布局,并将TabWidget添加到其中 mainLayout := VBox{ MarginsZero: true, Children: []Widget{ tabWidget, }, } // 创建主窗口的声明式描述 mainWindow.Desc = &MainWindow{ Title: "My App", MinSize: Size{600, 400}, Layout: mainLayout, AssignTo: &mainWindow.MainWindow, } // 运行应用程序 mainWindow.Run() } // 定义一个新的主窗口类型 type MyMainWindow struct { *walk.MainWindow } ``` 在这个示例中,我们创建了一个`TabWidget`,并向其中添加了两个选项卡。每个选项卡都包含一些基本控件,例如`Label`,`PushButton`和`CheckBox`,以演示如何将控件添加到选项卡中。最后,我们将`TabWidget`添加到一个主布局中,并将其分配给主窗口的`Desc`属性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值