Sign in to follow this  

Hiding and showing a shell notify icon [SOLVED]

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
The usage is described right there in the quote:

Quote:
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.


dwStateMask does not change the icon's state; it controls which flag bits in dwState are considered relevant by the API.


Are you familiar with the way bitmasks work?

Share this post


Link to post
Share on other sites
I don't know how but it works now!
The final code is below :

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

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


I found the code at this address :
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21400676.html

So ApochPiQ, it has nothing to do with bit-mask trick. I know, what you said was what it has to be, but it actually isn't that way. Thank you for you help.
I still don't understand why dwState is set to 0, and mask is set to hidden state to *show* the icon. It is a mystery.

Share this post


Link to post
Share on other sites
Actually it did have to do with bitmasking. You didnt read the documentation -

The value of dwStateMask that you specify is which bits you care about from dwState. When you set dwStateMask to NIS_HIDDEN you are saying "Set the state of the icon to be (dwState & NIS_HIDDEN)". The bits associated with NIS_HIDDEN can be thought of as the hidden-ness of the icon. When these bits are 1 then the icon is hidden, when they are 0 the icon is visible. When dwState is 0 then the bits associated with NIS_HIDDEN are 0 and when dwState is NIS_HIDDEN then the bits associated with NIS_HIDDEN are 1.

Share this post


Link to post
Share on other sites
My dumb head finally got it.

dwState is the state of icon, and dwStateMask is what we care about.
I confused so much because I thought NIS_SHAREDICON and NIS_HIDDEN were opposite things and were pointing to same bit. (If something is shared it is not hidded, my logic). It's not a mystery anymore.

I'm ending with one last question...
So what is NIS_SHAREDICON for?

Thank you.

Share this post


Link to post
Share on other sites

This topic is 4097 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this