Example #1
0
// NewNotifyIcon creates and returns a new NotifyIcon.
//
// The NotifyIcon is initially not visible.
func NewNotifyIcon() (*NotifyIcon, error) {
	// Create the message-only window for the NotifyIcon.
	hWnd := win.CreateWindowEx(
		0,
		syscall.StringToUTF16Ptr(notifyIconWindowClass),
		nil,
		0,
		0,
		0,
		0,
		0,
		win.HWND_MESSAGE,
		0,
		0,
		nil)
	if hWnd == 0 {
		return nil, lastError("CreateWindowEx")
	}

	// Add our notify icon to the status area and make sure it is hidden.
	nid := win.NOTIFYICONDATA{
		HWnd:             hWnd,
		UFlags:           win.NIF_MESSAGE | win.NIF_STATE,
		DwState:          win.NIS_HIDDEN,
		DwStateMask:      win.NIS_HIDDEN,
		UCallbackMessage: notifyIconMessageId,
	}
	nid.CbSize = uint32(unsafe.Sizeof(nid))

	if !win.Shell_NotifyIcon(win.NIM_ADD, &nid) {
		return nil, newError("Shell_NotifyIcon")
	}

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

	if !win.Shell_NotifyIcon(win.NIM_SETVERSION, &nid) {
		return nil, newError("Shell_NotifyIcon")
	}

	// Create and initialize the NotifyIcon already.
	menu, err := NewMenu()
	if err != nil {
		return nil, err
	}

	ni := &NotifyIcon{
		id:          nid.UID,
		hWnd:        hWnd,
		contextMenu: menu,
	}

	// Set our *NotifyIcon as user data for the message window.
	win.SetWindowLongPtr(hWnd, win.GWLP_USERDATA, uintptr(unsafe.Pointer(ni)))

	return ni, nil
}
Example #2
0
func (ni *NotifyIcon) showMessage(title, info string, iconType uint32) error {
	nid := ni.notifyIconData()
	nid.UFlags = win.NIF_INFO
	nid.DwInfoFlags = iconType
	copy(nid.SzInfoTitle[:], syscall.StringToUTF16(title))
	copy(nid.SzInfo[:], syscall.StringToUTF16(info))

	if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
		return newError("Shell_NotifyIcon")
	}

	return nil
}
Example #3
0
// SetToolTip sets the tool tip text of the NotifyIcon.
func (ni *NotifyIcon) SetToolTip(toolTip string) error {
	if toolTip == ni.toolTip {
		return nil
	}

	nid := ni.notifyIconData()
	nid.UFlags = win.NIF_TIP
	copy(nid.SzTip[:], syscall.StringToUTF16(toolTip))

	if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
		return newError("Shell_NotifyIcon")
	}

	ni.toolTip = toolTip

	return nil
}
Example #4
0
// Dispose releases the operating system resources associated with the
// NotifyIcon.
//
// The associated Icon is not disposed of.
func (ni *NotifyIcon) Dispose() error {
	if ni.hWnd == 0 {
		return nil
	}

	nid := ni.notifyIconData()

	if !win.Shell_NotifyIcon(win.NIM_DELETE, nid) {
		return newError("Shell_NotifyIcon")
	}

	if !win.DestroyWindow(ni.hWnd) {
		return lastError("DestroyWindow")
	}
	ni.hWnd = 0

	return nil
}
Example #5
0
// SetVisible sets if the NotifyIcon is visible.
func (ni *NotifyIcon) SetVisible(visible bool) error {
	if visible == ni.visible {
		return nil
	}

	nid := ni.notifyIconData()
	nid.UFlags = win.NIF_STATE
	nid.DwStateMask = win.NIS_HIDDEN
	if !visible {
		nid.DwState = win.NIS_HIDDEN
	}

	if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
		return newError("Shell_NotifyIcon")
	}

	ni.visible = visible

	return nil
}
Example #6
0
// SetIcon sets the Icon of the NotifyIcon.
func (ni *NotifyIcon) SetIcon(icon *Icon) error {
	if icon == ni.icon {
		return nil
	}

	nid := ni.notifyIconData()
	nid.UFlags = win.NIF_ICON
	if icon == nil {
		nid.HIcon = 0
	} else {
		nid.HIcon = icon.hIcon
	}

	if !win.Shell_NotifyIcon(win.NIM_MODIFY, nid) {
		return newError("Shell_NotifyIcon")
	}

	ni.icon = icon

	return nil
}