15 мая 2023 года "Исходники.РУ" отмечают своё 23-летие!
Поздравляем всех причастных и неравнодушных с этим событием!
И огромное спасибо всем, кто был и остаётся с нами все эти годы!

Главная Форум Журнал Wiki DRKB Discuz!ML Помощь проекту


Shell_NotifyIcon

DeVincci -- devincci@ix.netcom.com
Saturday, December 28, 1996

Environment:   MSVC++ 4.0,  Windows 95

I have a problem with this call to the Shell_NotifyIcon API function...

I pass the hWnd to the function in the NOTIFYICONDATA structure.
If I give the handle to a CStatic control with an icon in it, the Icon
stays in the taskbar.
If I give the hWnd to the my Dialog, the icon disappears from the tray when
I put my mouse
cursor on the icon in the taskbar tray.
The CStatic control has the "Notify" option checked.

I created an Appwizard Dialog App and put the call to Shell_NotifyIcon in
the OnInitDialog() function.  

Anyways, for the callback message in the NOTIFYICONDATA structure, where do
I capture the message that I specify.

Clicking the mouse on the Taskbar icon DOES NOT trigger the WindowProc or
DefWindowProc functions......
If someone has some sample code implementing a taskbar icon in an AppWizard
created App, I would be very thankful for some sample code..

P.S.  The sample that comes with VC++ does NOT use an AppWizard created
App...it uses a standard WinMain function approach...

Here's my code:

	NOTIFYICONDATA tnd;


	myPntr= (CStatic *)GetDlgItem(IDC_STATIC1);
	myPntr->SetIcon(ExtractIcon(AfxGetInstanceHandle(),"state11.ico",0));
	
	tnd.cbSize		= sizeof(NOTIFYICONDATA);
	tnd.hWnd		= myPntr->m_hWnd;
	tnd.uID			= 53;
	tnd.uFlags		= NIF_ICON | NIF_MESSAGE | NIF_TIP;
	tnd.uCallbackMessage	= WM_LBUTTONDOWN;
	tnd.hIcon		= myPntr->GetIcon();
	strcpy(tnd.szTip, "Hello") ;

	Shell_NotifyIcon(NIM_ADD, &tnd);


Thanks for ANY help!!!!!!!!!!




Mike Morel -- mmorel@mushroomsoft.com
Sunday, December 29, 1996

This sample is an App-Wizard app, but not dialog-based.  The hWnd that you 
use must be the one which will receive the notifications (mouse move, 
click, etc.).  So you want the dialog's hWnd.  In the sample below, it's 
the main frame window.  This sample is from the December issue of MFC For 
Yourself.  The class is CTrayFrame, derived from CFrameWnd.  Using the 
CTrayFrame class for your main window, your app automatically adds and 
handles a tray icon:

void CTrayFrame::SetTrayIcon(UINT idIcon)
{
	m_hTrayIcon = AfxGetApp()->LoadIcon(m_idIcon);
	... error checking...

	NOTIFYICONDATA nid;
	nid.cbSize = sizeof(nid);
	nid.hWnd = m_hWnd;
	nid.uID = 1;
	nid.uFlags = NIF_ICON | NIF_MESSAGE;
	nid.uCallbackMessage = TRAY_MESSAGE;
	nid.hIcon = m_hTrayIcon;

	if (! Shell_NotifyIcon(NIM_ADD, &nid))
	{
		TRACE("Shell_NotifyIcon Failed!\n");
		return;
	}
	m_bIconAdded = TRUE;
	return;
}

To handle the messages:
BEGIN_MESSAGE_MAP(CTrayFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CTrayFrame)
	//}}AFX_MSG_MAP
	ON_MESSAGE(TRAY_MESSAGE, OnTrayMessage)
END_MESSAGE_MAP()

LRESULT CTrayFrame::OnTrayMessage(WPARAM wParam, LPARAM lParam)
{
	UINT msg = (UINT)lParam;
	switch (msg)
	{
	case WM_LBUTTONDOWN:
	...etc...

One other note - if you use a dialog-based app, you'll probably have 
trouble with the enabling / disabling of menu items of "right-click" popup 
menus.  This is because the normal command UI update mechanism will work 
with TrackPopupMenu only if the owner window is derived from CFrameWnd, not 
CDialog.  So I used a frame window, and modified its attributes so that it 
cannot be resized, so that it looks like a dialog, but it really isn't.

Hope this helps.  You can see more about the CTrayFrame class at our web 
site.

Mike Morel
mmorel@mushroomsoft.com
Mushroom Software
Home of MFC For Yourself
http://www.mushroomsoft.com



----------
From: 	DeVincci[SMTP:devincci@ix.netcom.com]
Sent: 	Saturday, December 28, 1996 1:08 PM
To: 	Send2MFC-L
Subject: 	Shell_NotifyIcon

Environment:   MSVC++ 4.0,  Windows 95

I have a problem with this call to the Shell_NotifyIcon API function...

I pass the hWnd to the function in the NOTIFYICONDATA structure.
If I give the handle to a CStatic control with an icon in it, the Icon
stays in the taskbar.
If I give the hWnd to the my Dialog, the icon disappears from the tray when
I put my mouse
cursor on the icon in the taskbar tray.
The CStatic control has the "Notify" option checked.

I created an Appwizard Dialog App and put the call to Shell_NotifyIcon in
the OnInitDialog() function.

Anyways, for the callback message in the NOTIFYICONDATA structure, where do
I capture the message that I specify.

Clicking the mouse on the Taskbar icon DOES NOT trigger the WindowProc or
DefWindowProc functions......
If someone has some sample code implementing a taskbar icon in an AppWizard
created App, I would be very thankful for some sample code..

P.S.  The sample that comes with VC++ does NOT use an AppWizard created
App...it uses a standard WinMain function approach...

Here's my code:

	NOTIFYICONDATA tnd;


	myPntr= (CStatic *)GetDlgItem(IDC_STATIC1);
	myPntr->SetIcon(ExtractIcon(AfxGetInstanceHandle(),"state11.ico",0));
	
	tnd.cbSize		= sizeof(NOTIFYICONDATA);
	tnd.hWnd		= myPntr->m_hWnd;
	tnd.uID			= 53;
	tnd.uFlags		= NIF_ICON | NIF_MESSAGE | NIF_TIP;
	tnd.uCallbackMessage	= WM_LBUTTONDOWN;
	tnd.hIcon		= myPntr->GetIcon();
	strcpy(tnd.szTip, "Hello") ;

	Shell_NotifyIcon(NIM_ADD, &tnd);


Thanks for ANY help!!!!!!!!!!









Dana M. Epp -- eppdm@uniserve.com
Sunday, December 29, 1996

I have a solution that works for me, however, I do not know if it is the
defacto standard. Anyways, if I had to approach this .. I would do it like
this.. you can register a callback with the Tray for messages. Most people
would want to deal with more than one message.. so set up one callback for
all of them. I would approach it like this:


	// Do this after your defines in you implimentation file
        UINT MSG_TASKBARCALLBACK =
::RegisterWindowMessage("TaskbarMessage"); // Register the Callback Message


        // In you Message Map.. add this.. 
	ON_REGISTERED_MESSAGE( MSG_TASKBARCALLBACK, OnTaskbarNotify )
        // don't forget to add this to your class:
        LRESULT OnTaskbarNotify( WPARAM wParam, LPARAM lParam ); 

Now by simply setting the MSG_TASKBARCALLBACK as the callback.. you have
full control over what is going on.

	NOTIFYICONDATA tnd;

	myPntr= (CStatic *)GetDlgItem(IDC_STATIC1);
	myPntr->SetIcon(ExtractIcon(AfxGetInstanceHandle(),"state11.ico",0));
		
	tnd.cbSize		= sizeof(NOTIFYICONDATA);
	tnd.hWnd		= myPntr->m_hWnd;
	tnd.uID			= 53;
	tnd.uFlags		= NIF_MESSAGE | NIF_ICON | NIF_TIP;
	tnd.uCallbackMessage    = MSG_TASKBARCALLBACK;
	tnd.hIcon		= myPntr->GetIcon();
	strcpy(tnd.szTip, "Hello") ;

	::Shell_NotifyIcon(NIM_ADD, &tnd);
	
LRESULT CMainFrame::OnTaskbarNotify( WPARAM, LPARAM lParam ) 
{

	switch (lParam)
	{
		case WM_LBUTTONDOWN:
			// You got your Message Callback
			break;
                case WM_RBUTTONDOWN:
                        break; // etc etc
	}
	
	return 0;
}


I know this example shows it in a CMainFrame for clarity.. but you can
pretty well put this where you want. The point is.. the callback message
will hold in the lParam the message for the tray icon... in this case a
WM_LBUTTONDOWN or WM_RBUTTONDOWN. Good luck.

At 01:08 PM 12/28/96 -0500, you wrote:
>Environment:   MSVC++ 4.0,  Windows 95
>
>I have a problem with this call to the Shell_NotifyIcon API function...
>
>I pass the hWnd to the function in the NOTIFYICONDATA structure.
>If I give the handle to a CStatic control with an icon in it, the Icon
>stays in the taskbar.
>If I give the hWnd to the my Dialog, the icon disappears from the tray when
>I put my mouse
>cursor on the icon in the taskbar tray.
>The CStatic control has the "Notify" option checked.
>
>I created an Appwizard Dialog App and put the call to Shell_NotifyIcon in
>the OnInitDialog() function.  
>
>Anyways, for the callback message in the NOTIFYICONDATA structure, where do
>I capture the message that I specify.
>
>Clicking the mouse on the Taskbar icon DOES NOT trigger the WindowProc or
>DefWindowProc functions......
>If someone has some sample code implementing a taskbar icon in an AppWizard
>created App, I would be very thankful for some sample code..
>
>P.S.  The sample that comes with VC++ does NOT use an AppWizard created
>App...it uses a standard WinMain function approach...
>
>Here's my code:
>
>	NOTIFYICONDATA tnd;
>
>
>	myPntr= (CStatic *)GetDlgItem(IDC_STATIC1);
>	myPntr->SetIcon(ExtractIcon(AfxGetInstanceHandle(),"state11.ico",0));
>	
>	tnd.cbSize		= sizeof(NOTIFYICONDATA);
>	tnd.hWnd		= myPntr->m_hWnd;
>	tnd.uID			= 53;
>	tnd.uFlags		= NIF_ICON | NIF_MESSAGE | NIF_TIP;
>	tnd.uCallbackMessage	= WM_LBUTTONDOWN;
>	tnd.hIcon		= myPntr->GetIcon();
>	strcpy(tnd.szTip, "Hello") ;
>
>	Shell_NotifyIcon(NIM_ADD, &tnd);
>
>
>Thanks for ANY help!!!!!!!!!!
>
>
PC'ing you,                     
Dana M. Epp          
eppdm@uniserve.com
http://bedrock.cyberhq.com/dana

     "How can one work with the technology of today, using yesterdays
      software and methods, and still be on the leading edge tomorrow?
      Why settle for less... I won't! "




Dan Kirby -- dkirby@accessone.com
Sunday, December 29, 1996

Hi, you don't need to use RegisterWindowMessage(). Use WM_APP+x. See
knowleddgebase article Q86835 for more information about using WM_APP. 

Also, on a related topic, article Q139408 contains additional information
about how to add drag and drop features to the icon in the status area
which you may find useful.

--dan


----------
> From: Dana M. Epp 
> To: mfc-l@netcom.com
> Cc: devincci@ix.netcom.com
> Subject: Re: Shell_NotifyIcon
> Date: Sunday, December 29, 1996 11:41 AM
> 
> I have a solution that works for me, however, I do not know if it is the
> defacto standard. Anyways, if I had to approach this .. I would do it
like
> this.. you can register a callback with the Tray for messages. Most
people
> would want to deal with more than one message.. so set up one callback
for
> all of them. I would approach it like this:
> 
> 
> 	// Do this after your defines in you implimentation file
>         UINT MSG_TASKBARCALLBACK =
> ::RegisterWindowMessage("TaskbarMessage"); // Register the Callback
Message
> 
> 
>         // In you Message Map.. add this.. 
> 	ON_REGISTERED_MESSAGE( MSG_TASKBARCALLBACK, OnTaskbarNotify )
>         // don't forget to add this to your class:
>         LRESULT OnTaskbarNotify( WPARAM wParam, LPARAM lParam ); 
> 
> Now by simply setting the MSG_TASKBARCALLBACK as the callback.. you have
> full control over what is going on.
> 
> 	NOTIFYICONDATA tnd;
> 
> 	myPntr= (CStatic *)GetDlgItem(IDC_STATIC1);
> 	myPntr->SetIcon(ExtractIcon(AfxGetInstanceHandle(),"state11.ico",0));
> 		
> 	tnd.cbSize		= sizeof(NOTIFYICONDATA);
> 	tnd.hWnd		= myPntr->m_hWnd;
> 	tnd.uID			= 53;
> 	tnd.uFlags		= NIF_MESSAGE | NIF_ICON | NIF_TIP;
> 	tnd.uCallbackMessage    = MSG_TASKBARCALLBACK;
> 	tnd.hIcon		= myPntr->GetIcon();
> 	strcpy(tnd.szTip, "Hello") ;
> 
> 	::Shell_NotifyIcon(NIM_ADD, &tnd);
> 	
> LRESULT CMainFrame::OnTaskbarNotify( WPARAM, LPARAM lParam ) 
> {
> 
> 	switch (lParam)
> 	{
> 		case WM_LBUTTONDOWN:
> 			// You got your Message Callback
> 			break;
>                 case WM_RBUTTONDOWN:
>                         break; // etc etc
> 	}
> 	
> 	return 0;
> }
> 
> 
> I know this example shows it in a CMainFrame for clarity.. but you can
> pretty well put this where you want. The point is.. the callback message
> will hold in the lParam the message for the tray icon... in this case a
> WM_LBUTTONDOWN or WM_RBUTTONDOWN. Good luck.
> 
> At 01:08 PM 12/28/96 -0500, you wrote:
> >Environment:   MSVC++ 4.0,  Windows 95
> >
> >I have a problem with this call to the Shell_NotifyIcon API function...
> >
> >I pass the hWnd to the function in the NOTIFYICONDATA structure.
> >If I give the handle to a CStatic control with an icon in it, the Icon
> >stays in the taskbar.
> >If I give the hWnd to the my Dialog, the icon disappears from the tray
when
> >I put my mouse
> >cursor on the icon in the taskbar tray.
> >The CStatic control has the "Notify" option checked.
> >
> >I created an Appwizard Dialog App and put the call to Shell_NotifyIcon
in
> >the OnInitDialog() function.  
> >
> >Anyways, for the callback message in the NOTIFYICONDATA structure, where
do
> >I capture the message that I specify.
> >
> >Clicking the mouse on the Taskbar icon DOES NOT trigger the WindowProc
or
> >DefWindowProc functions......
> >If someone has some sample code implementing a taskbar icon in an
AppWizard
> >created App, I would be very thankful for some sample code..
> >
> >P.S.  The sample that comes with VC++ does NOT use an AppWizard created
> >App...it uses a standard WinMain function approach...
> >
> >Here's my code:
> >
> >	NOTIFYICONDATA tnd;
> >
> >
> >	myPntr= (CStatic *)GetDlgItem(IDC_STATIC1);
> >	myPntr->SetIcon(ExtractIcon(AfxGetInstanceHandle(),"state11.ico",0));
> >	
> >	tnd.cbSize		= sizeof(NOTIFYICONDATA);
> >	tnd.hWnd		= myPntr->m_hWnd;
> >	tnd.uID			= 53;
> >	tnd.uFlags		= NIF_ICON | NIF_MESSAGE | NIF_TIP;
> >	tnd.uCallbackMessage	= WM_LBUTTONDOWN;
> >	tnd.hIcon		= myPntr->GetIcon();
> >	strcpy(tnd.szTip, "Hello") ;
> >
> >	Shell_NotifyIcon(NIM_ADD, &tnd);
> >
> >
> >Thanks for ANY help!!!!!!!!!!
> >
> >
> PC'ing you,                     
> Dana M. Epp          
> eppdm@uniserve.com
> http://bedrock.cyberhq.com/dana
> 
>      "How can one work with the technology of today, using yesterdays
>       software and methods, and still be on the leading edge tomorrow?
>       Why settle for less... I won't! "
> 



Mario Contestabile -- Mario_Contestabile.UOS__MTL@UOSMTL2.universal.com
Monday, December 30, 1996

>I have a problem with this call to the Shell_NotifyIcon API function...

Here's how I do it and it works fine:

NOTIFYICONDATA tnib;
memset(&tnib, 0, sizeof(NOTIFYICONDATA));
tnib.cbSize           = sizeof(NOTIFYICONDATA); 
tnib.uFlags           = NIF_ICON | NIF_TIP;
tnib.uID    = njobid;
CString      tip;
HICON icon = LoadIcon(IDI_XXX);
tnib.hIcon = icon;
tip.LoadString(IDS_TIP);   // 64 chars max
_tcsncpy(tnib.szTip, tip, 62);
Shell_NotifyIcon(NIM_ADD, &tnib)

mcontest@universal.com





| Вернуться в корень Архива |