Prev: optimization
Next: Lost Mail
From: dushkin on
Hi all.

I have two questions regarding a very simple tray application. The
application does nothing. It only appears in the system tray. I want
to add a floating menu to it with a "close" menu item that will close
the process. The application is an MFC dialog based application. I
work with VS2008.

My questions are:

1. I added a message handler to the "close" menu item. The handler is
called only after I first r-clicking the app icon, select the "close"
item and again r-clicking the app icon in the tray. Why?

2. Then, I put SendMessage(WM_CLOSE) in the handler. The application
was not closed. Why?

The code is as follows:

//-----------------------------------------------------------------
cpp file
//-----------------------------------------------------------------

BEGIN_MESSAGE_MAP(CVPGAgentDlg, CDialog)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_MESSAGE(MYWM_NOTIFYICON,onTrayNotify)
//}}AFX_MSG_MAP
ON_WM_DESTROY()
ON_WM_CLOSE()
ON_COMMAND(ID_AGENTMENU_CLOSE, OnAgentClose)
END_MESSAGE_MAP()

void CVPGAgentDlg::SetSysTrayIcon(void)
{
HICON m_hicon = ::LoadIcon(AfxGetInstanceHandle(),
MAKEINTRESOURCE(IDI_SYSTRAYICON));

CString sTip(_T("PA Agent"));

m_pnId.cbSize = sizeof(NOTIFYICONDATA);
m_pnId.hWnd = this->GetSafeHwnd();
m_pnId.uID = IDI_SYSTRAYICON;
//ICON RESOURCE ID
m_pnId.uFlags = NIF_MESSAGE|NIF_ICON;
m_pnId.uCallbackMessage = MYWM_NOTIFYICON;
m_pnId.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
m_pnId.hIcon = m_hicon;
//ICON RESOURCE ID
lstrcpyn(m_pnId.szTip, (LPCTSTR)sTip, sizeof(m_pnId.szTip));

DWORD dwMessage = NIM_ADD;
Shell_NotifyIcon(dwMessage, &m_pnId);

m_TrayMenu.LoadMenu(IDR_MENU1);
}

LRESULT CVPGAgentDlg::onTrayNotify(WPARAM wParam,LPARAM lParam)
{
UINT uMsg = (UINT) lParam;
switch (uMsg )
{
case WM_LBUTTONDBLCLK:
this->ShowWindow(SW_SHOW);
break;
case WM_RBUTTONUP:
CPoint pt;
GetCursorPos(&pt);

SetForegroundWindow();
m_TrayMenu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTALIGN|
TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this);
PostMessage(WM_NULL, 0, 0);

break;
}
return TRUE;
}

void CVPGAgentDlg::OnAgentClose()
{
AfxMessageBox("a");
//SendMessage(WM_CLOSE);
}

void CVPGAgentDlg::OnDestroy()
{
Shell_NotifyIcon(NIM_DELETE,&m_pnId);
CDialog::OnDestroy();

// TODO: Add your message handler code here
}


//-----------------------------------------------------------------
h file:
//-----------------------------------------------------------------
#define MYWM_NOTIFYICON (WM_USER+1)

CMenu m_TrayMenu;
NOTIFYICONDATA m_pnId;

void SetSysTrayIcon(void);

afx_msg LRESULT onTrayNotify(WPARAM, LPARAM);
afx_msg void OnAgentClose();
afx_msg void OnClose();
afx_msg void OnDestroy();




THANKS!


From: dushkin on
And please... I know that there is and AfxMessageBox in the function
and SendMessage is commented... this is not the reason the app is not
closed. It is only part of my tests
:-)
From: Tom Serface on
When I added this functionality to a recent program I used this class that
PJ wrote:

http://www.naughter.com/ntray.html

It is very easy to add and does all the things you are asking for...
Basically, you can use the equivalent of a right click menu and the messages
are handled by the mainframe window just like any other messages that you'd
typically do from menus or the toolbar.

Tom

"dushkin" <taltene(a)gmail.com> wrote in message
news:078a3b75-73a1-42b2-b265-031daa2239db(a)f8g2000yqn.googlegroups.com...
> Hi all.
>
> I have two questions regarding a very simple tray application. The
> application does nothing. It only appears in the system tray. I want
> to add a floating menu to it with a "close" menu item that will close
> the process. The application is an MFC dialog based application. I
> work with VS2008.
>
> My questions are:
>
> 1. I added a message handler to the "close" menu item. The handler is
> called only after I first r-clicking the app icon, select the "close"
> item and again r-clicking the app icon in the tray. Why?
>
> 2. Then, I put SendMessage(WM_CLOSE) in the handler. The application
> was not closed. Why?
>
> The code is as follows:
>
> //-----------------------------------------------------------------
> cpp file
> //-----------------------------------------------------------------
>
> BEGIN_MESSAGE_MAP(CVPGAgentDlg, CDialog)
> ON_WM_SYSCOMMAND()
> ON_WM_PAINT()
> ON_WM_QUERYDRAGICON()
> ON_MESSAGE(MYWM_NOTIFYICON,onTrayNotify)
> //}}AFX_MSG_MAP
> ON_WM_DESTROY()
> ON_WM_CLOSE()
> ON_COMMAND(ID_AGENTMENU_CLOSE, OnAgentClose)
> END_MESSAGE_MAP()
>
> void CVPGAgentDlg::SetSysTrayIcon(void)
> {
> HICON m_hicon = ::LoadIcon(AfxGetInstanceHandle(),
> MAKEINTRESOURCE(IDI_SYSTRAYICON));
>
> CString sTip(_T("PA Agent"));
>
> m_pnId.cbSize = sizeof(NOTIFYICONDATA);
> m_pnId.hWnd = this->GetSafeHwnd();
> m_pnId.uID = IDI_SYSTRAYICON;
> //ICON RESOURCE ID
> m_pnId.uFlags = NIF_MESSAGE|NIF_ICON;
> m_pnId.uCallbackMessage = MYWM_NOTIFYICON;
> m_pnId.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
> m_pnId.hIcon = m_hicon;
> //ICON RESOURCE ID
> lstrcpyn(m_pnId.szTip, (LPCTSTR)sTip, sizeof(m_pnId.szTip));
>
> DWORD dwMessage = NIM_ADD;
> Shell_NotifyIcon(dwMessage, &m_pnId);
>
> m_TrayMenu.LoadMenu(IDR_MENU1);
> }
>
> LRESULT CVPGAgentDlg::onTrayNotify(WPARAM wParam,LPARAM lParam)
> {
> UINT uMsg = (UINT) lParam;
> switch (uMsg )
> {
> case WM_LBUTTONDBLCLK:
> this->ShowWindow(SW_SHOW);
> break;
> case WM_RBUTTONUP:
> CPoint pt;
> GetCursorPos(&pt);
>
> SetForegroundWindow();
> m_TrayMenu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTALIGN|
> TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this);
> PostMessage(WM_NULL, 0, 0);
>
> break;
> }
> return TRUE;
> }
>
> void CVPGAgentDlg::OnAgentClose()
> {
> AfxMessageBox("a");
> //SendMessage(WM_CLOSE);
> }
>
> void CVPGAgentDlg::OnDestroy()
> {
> Shell_NotifyIcon(NIM_DELETE,&m_pnId);
> CDialog::OnDestroy();
>
> // TODO: Add your message handler code here
> }
>
>
> //-----------------------------------------------------------------
> h file:
> //-----------------------------------------------------------------
> #define MYWM_NOTIFYICON (WM_USER+1)
>
> CMenu m_TrayMenu;
> NOTIFYICONDATA m_pnId;
>
> void SetSysTrayIcon(void);
>
> afx_msg LRESULT onTrayNotify(WPARAM, LPARAM);
> afx_msg void OnAgentClose();
> afx_msg void OnClose();
> afx_msg void OnDestroy();
>
>
>
>
> THANKS!
>
>
From: dushkin on
On Mar 17, 2:41 pm, "Tom Serface" <t...(a)camaswood.com> wrote:
> When I added this functionality to a recent program I used this class that
>
> PJ wrote:
>
> http://www.naughter.com/ntray.html
>
> It is very easy to add and does all the things you are asking for...
> Basically, you can use the equivalent of a right click menu and the messages
> are handled by the mainframe window just like any other messages that you'd
> typically do from menus or the toolbar.
>
> Tom
>
> "dushkin" <talt...(a)gmail.com> wrote in message
>
> news:078a3b75-73a1-42b2-b265-031daa2239db(a)f8g2000yqn.googlegroups.com...
>
> > Hi all.
>
> > I have two questions regarding a very simple tray application. The
> > application does nothing. It only appears in the system tray. I want
> > to add a floating menu to it with a "close" menu item that will close
> > the process. The application is an MFC dialog based application. I
> > work with VS2008.
>
> > My questions are:
>
> > 1. I added a message handler to the "close" menu item. The handler is
> > called only after I first r-clicking the app icon, select the "close"
> > item and again r-clicking the app icon in the tray. Why?
>
> > 2. Then, I put SendMessage(WM_CLOSE) in the handler. The application
> > was not closed. Why?
>
> > The code is as follows:
>
> > //-----------------------------------------------------------------
> > cpp file
> > //-----------------------------------------------------------------
>
> > BEGIN_MESSAGE_MAP(CVPGAgentDlg, CDialog)
> > ON_WM_SYSCOMMAND()
> > ON_WM_PAINT()
> > ON_WM_QUERYDRAGICON()
> > ON_MESSAGE(MYWM_NOTIFYICON,onTrayNotify)
> > //}}AFX_MSG_MAP
> > ON_WM_DESTROY()
> > ON_WM_CLOSE()
> > ON_COMMAND(ID_AGENTMENU_CLOSE, OnAgentClose)
> > END_MESSAGE_MAP()
>
> > void CVPGAgentDlg::SetSysTrayIcon(void)
> > {
> > HICON m_hicon = ::LoadIcon(AfxGetInstanceHandle(),
> > MAKEINTRESOURCE(IDI_SYSTRAYICON));
>
> > CString sTip(_T("PA Agent"));
>
> > m_pnId.cbSize = sizeof(NOTIFYICONDATA);
> > m_pnId.hWnd = this->GetSafeHwnd();
> > m_pnId.uID = IDI_SYSTRAYICON;
> > //ICON RESOURCE ID
> > m_pnId.uFlags = NIF_MESSAGE|NIF_ICON;
> > m_pnId.uCallbackMessage = MYWM_NOTIFYICON;
> > m_pnId.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;
> > m_pnId.hIcon = m_hicon;
> > //ICON RESOURCE ID
> > lstrcpyn(m_pnId.szTip, (LPCTSTR)sTip, sizeof(m_pnId.szTip));
>
> > DWORD dwMessage = NIM_ADD;
> > Shell_NotifyIcon(dwMessage, &m_pnId);
>
> > m_TrayMenu.LoadMenu(IDR_MENU1);
> > }
>
> > LRESULT CVPGAgentDlg::onTrayNotify(WPARAM wParam,LPARAM lParam)
> > {
> > UINT uMsg = (UINT) lParam;
> > switch (uMsg )
> > {
> > case WM_LBUTTONDBLCLK:
> > this->ShowWindow(SW_SHOW);
> > break;
> > case WM_RBUTTONUP:
> > CPoint pt;
> > GetCursorPos(&pt);
>
> > SetForegroundWindow();
> >    m_TrayMenu.GetSubMenu(0)->TrackPopupMenu(TPM_RIGHTALIGN|
> > TPM_LEFTBUTTON|TPM_RIGHTBUTTON,pt.x,pt.y,this);
> > PostMessage(WM_NULL, 0, 0);
>
> > break;
> > }
> > return TRUE;
> > }
>
> > void CVPGAgentDlg::OnAgentClose()
> > {
> > AfxMessageBox("a");
> > //SendMessage(WM_CLOSE);
> > }
>
> > void CVPGAgentDlg::OnDestroy()
> > {
> > Shell_NotifyIcon(NIM_DELETE,&m_pnId);
> > CDialog::OnDestroy();
>
> > // TODO: Add your message handler code here
> > }
>
> > //-----------------------------------------------------------------
> > h file:
> > //-----------------------------------------------------------------
> > #define MYWM_NOTIFYICON (WM_USER+1)
>
> > CMenu m_TrayMenu;
> > NOTIFYICONDATA m_pnId;
>
> > void SetSysTrayIcon(void);
>
> > afx_msg LRESULT onTrayNotify(WPARAM, LPARAM);
> > afx_msg void OnAgentClose();
> > afx_msg void OnClose();
> > afx_msg void OnDestroy();
>
> > THANKS!

Seems to be great! I will check it. Thanks!
Did you really maintained this package 11 years?!?!
:-)
From: Tom Serface on
PJ has had this for a while, but the technology hasn't changed much in the
last years so it still works really well. Worth a look anyway.

Tm

"dushkin" <taltene(a)gmail.com> wrote in message
news:81012ed3-aa95-4c27-b2ac-846c742f8324(a)g11g2000yqe.googlegroups.com...

>>
> Seems to be great! I will check it. Thanks!
> Did you really maintained this package 11 years?!?!
> :-)

 |  Next  |  Last
Pages: 1 2
Prev: optimization
Next: Lost Mail