Hiding and showing a shell notify icon [SOLVED]

Started by
14 comments, last by hkBattousai 17 years, 6 months ago
Hello, I am working on a system tray icon class for Windows XP, with DevCPP. It all seems to be working. But I got stuck at Hide() and Show() member functions. I have no idea what code to write for them. Any help will be appreciated.

class CTrayIcon
{
	private:
	
	public:
	NOTIFYICONDATA	Nid;
	CTrayIcon();
	void Create(void);
	void Show(void);
	void Hide(void);
	void Modify(void);
	void Kill(void);
	
};

CTrayIcon::CTrayIcon()
{
	Nid.cbSize			= (DWORD) sizeof(NOTIFYICONDATA);
	Nid.uFlags			= (UINT) NIF_ICON | NIF_MESSAGE | NIF_TIP | NIF_STATE | NIF_INFO | NIF_GUID;
	Nid.dwState			= (DWORD) NIS_SHAREDICON | NIS_HIDDEN;
	//Nid.dwStateMask		= (DWORD) NIS_SHAREDICON | NIS_HIDDEN;
	Nid.uTimeout		= (UINT) 10;
	//Nid.uVersion		= (UINT) NOTIFYICON_VERSION;
	Nid.dwInfoFlags		= (DWORD) NIIF_INFO;	// NIIF_ERROR, NIIF_NON, NIIF_WARNING, NIIF_ICON_MASK, NIIF_NOSOUND
	//Nid.guidItem		= (GUID) NULL;			// Version 6.0. Reserved	
}

void CTrayIcon::Create(void)
{
	if(!Shell_NotifyIcon(NIM_ADD, &Nid)) DebugTest("Couldn't create the icon.");
}

void CTrayIcon::Show(void)
{
	
	//if(!Shell_NotifyIcon(NIM_MODIFY, &Nid)) DebugTest("Couldn't show the icon.");
}

void CTrayIcon::Hide(void)
{
	
	//if(!Shell_NotifyIcon(NIM_MODIFY, &Nid)) DebugTest("Couldn't hide the icon.");
}

void CTrayIcon::Modify(void)
{
	if(!Shell_NotifyIcon(NIM_MODIFY, &Nid)) DebugTest("Couldn't modify the icon.");
}

void CTrayIcon::Kill(void)
{
	if(!Shell_NotifyIcon(NIM_DELETE, &Nid)) DebugTest("Couldn't kill the icon.");
}
[Edited by - Battousai on September 28, 2006 3:39:45 PM]
Advertisement
Just i forgot to write. DebugTest() is a function that is using MessageBox() to inform user about runtime errors.
I just wrote a function like this.

void SysTray(bool enable){    enable ? Shell_NotifyIcon(NIM_ADD, &m_sysTray) : Shell_NotifyIcon(NIM_DELETE, &m_sysTray); }

I don't know if this is how you do it really, but I just ADD it to show it and then DELETE it to hide it.
If you insist on saying "DUH", try to make a post that makes a little more sense
Isn't there any other way without deleting it?
No - adding and deleting are the only known operations. There's no such thing as a tray icon that is added but not displayed.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Then what dwState and dwStateMask fields are for?
When I assign ANY value to dwStateMask, icon creation fails. Have any idea about that?
OK, I stand corrected - there is in fact a hidden state. However, it is only available in certain versions of the API (not much of a limitation - anything more recent than Win2000 should have it).

See The Manual for full documentation on the NOTIFYICONDATA structure.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

Quote:dwState
Version 5.0. State of the icon. There are two flags that can be set independently.

NIS_HIDDEN
The icon is hidden.
NIS_SHAREDICON
The icon is shared.

dwStateMask
Version 5.0. A value that specifies which bits of the state member are retrieved or modified. For example, setting this member to NIS_HIDDEN causes only the item's hidden state to be retrieved.


Now Hide() works but Show() doesn't. New code is below :
void CTrayIcon::Show(void){	Nid.dwStateMask = NIS_SHAREDICON;	if(!Shell_NotifyIcon(NIM_MODIFY, &Nid)) DebugTest("Couldn't show the icon.");}void CTrayIcon::Hide(void){	Nid.dwStateMask = NIS_HIDDEN;	if(!Shell_NotifyIcon(NIM_MODIFY, &Nid)) DebugTest("Couldn't hide the icon.");}


By the way, about version, I've already defined :
#define	_WIN32_IE	0x0600


[Edited by - Battousai on September 27, 2006 3:51:22 AM]
I think you need to reread the documentation, carefully. What is the difference between dwState and dwStateMask?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

The only thing I found in MSDN is this :
Quote:dwState
Version 5.0. State of the icon. There are two flags that can be set independently.

NIS_HIDDEN
The icon is hidden.
NIS_SHAREDICON
The icon is shared.

dwStateMask
Version 5.0. A value that specifies which bits of the state member are retrieved or modified. For example, setting this member to NIS_HIDDEN causes only the item's hidden state to be retrieved.
It doesn't make clear usage of dwStateMask.

Unfortunately, the documentation doesn't touch on this detail. I searched the web with google but found nothing.

This topic is closed to new replies.

Advertisement