Skip to content

Commit

Permalink
Added SetBalloonIcon to NotifyIcon for support of custom balloon icon…
Browse files Browse the repository at this point in the history
…s. Compatibility of NotifyIcon is changed to Vista if on Vista or higher to support custom balloon icon. Works also with Windows compatibility mode.
  • Loading branch information
Angelo Steinbach committed Oct 31, 2016
1 parent 765f165 commit f34c686
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions notifyicon.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

import (
"github.com/lxn/win"
"errors"
)

const notifyIconWindowClass = `\o/ Walk_NotifyIcon_Class \o/`
Expand Down Expand Up @@ -78,6 +79,8 @@ type NotifyIcon struct {
hWnd win.HWND
contextMenu *Menu
icon *Icon
uVersion uint32
balloonIcon *Icon
toolTip string
visible bool
mouseDownPublisher MouseEventPublisher
Expand Down Expand Up @@ -120,8 +123,14 @@ func NewNotifyIcon() (*NotifyIcon, error) {
return nil, newError("Shell_NotifyIcon")
}

// We want XP-compatible message behavior.
nid.UVersion = win.NOTIFYICON_VERSION

// We want XP-compatible message behavior or vista+ when available.
uVersion := win.NOTIFYICON_VERSION_XP
if win.LOBYTE(win.LOWORD(win.GetVersion())) >= 6 {
uVersion = win.NOTIFYICON_VERSION_VISTA
}

nid.UVersion = uVersion

if !win.Shell_NotifyIcon(win.NIM_SETVERSION, &nid) {
return nil, newError("Shell_NotifyIcon")
Expand All @@ -137,6 +146,7 @@ func NewNotifyIcon() (*NotifyIcon, error) {
id: nid.UID,
hWnd: hWnd,
contextMenu: menu,
uVersion: uVersion,
}

// Set our *NotifyIcon as user data for the message window.
Expand Down Expand Up @@ -260,6 +270,28 @@ func (ni *NotifyIcon) SetIcon(icon *Icon) error {
return nil
}

// SetBalloonIcon sets the Icon of the custom message.
func (ni *NotifyIcon) SetBalloonIcon(icon *Icon) error {
if ni.uVersion < win.NOTIFYICON_VERSION_VISTA{
return errors.New("Custom balloon icon is only supported on Windows Vista and higher!")
}

if icon == ni.balloonIcon {
return nil
}

nid := ni.notifyIconData()
if icon == nil {
nid.Set_hBalloonIcon(0)
} else {
nid.Set_hBalloonIcon(icon.hIcon)
}

ni.balloonIcon = icon

return nil
}

// ToolTip returns the tool tip text of the NotifyIcon.
func (ni *NotifyIcon) ToolTip() string {
return ni.toolTip
Expand Down

0 comments on commit f34c686

Please sign in to comment.