Introduction
I write applications for my site Settlers.net and also to make things easier for myself at home. In one of my apps, I have wanted to include a system tray icon. Only problem being, the sys tray icon samples people wrote were confusing and didn't do what I want. I'm young and not exactly pro at programming yet. This tray icon code is different simply because upon starting the program, the icon is placed into the system tray. When you click hide, the whole program is minimized to tray.
Right, now down the code.
Let's do this!
Okay, on the main dialog you must make a button for the hiding command. Name this button IDC_HIDEAPP
.
Add files traynot.cpp and traynot.h to your project.
Paste this into yourDlg.cpp:
void CYourDlg::OnHide()
{
m_pTray = new CTrayNot (this,WM_TRAY_NOTIFY,
NULL,theApp.m_pIconList);
m_pTray->SetState(IDR_MAINFRAME);
m_bHidden = TRUE;
}
void CYourDlg::OnUnHide()
{
ShowWindow (SW_RESTORE) ;
m_bHidden = FALSE ;
if (m_pTray)
{
delete m_pTray ;
m_pTray = NULL ;
}
}
LONG CYourDlg::OnTrayNotify ( WPARAM wParam, LPARAM lParam )
{
switch (lParam)
{
case WM_RBUTTONDOWN:
{
CMenu menu ;
VERIFY(menu.LoadMenu(IDR_TRAY));
CMenu* pPopup = menu.GetSubMenu (0) ;
ASSERT(pPopup != NULL);
POINT pt ;
GetCursorPos (&pt) ;
SetForegroundWindow();
pPopup->TrackPopupMenu(TPM_LEFTALIGN |
TPM_RIGHTBUTTON,pt.x, pt.y, AfxGetMainWnd());
break ;
}
case WM_LBUTTONDBLCLK:
if (m_bHidden)
ShowWindow (SW_RESTORE);
break ;
}
return (0) ;
}
void CYourDlg::OnDestroy()
{
CDialog::OnDestroy();
if (m_pTray)
{
delete m_pTray ;
m_pTray = NULL ;
}
}
void CYourDlg::OnTrayRestore()
{
if (m_bHidden)
ShowWindow (SW_RESTORE) ;
m_bHidden = FALSE ;
}
void CYourDlg::OnHideapp()
{
theApp.HideApplication();
}
Add these to yourDlgs
message map:
ON_MESSAGE(WM_TRAY_NOTIFY, OnTrayNotify)
ON_COMMAND(ID_TRAY_RESTORE, OnTrayRestore)
ON_BN_CLICKED(IDC_HIDEAPP, OnHideapp)
Add #include "TrayNot.h"
to yourDlg.h. Add:
CTrayNot* m_pTray;
BOOL m_bHidden;
to your public call in yourDlg.h before yourDlgs
standard constructor.
Add the following to your dialog's afx
message maps.
afx_msg LONG OnTrayNotify ( WPARAM wParam, LPARAM lParam ) ;
afx_msg void OnTrayRestore();
afx_msg void OnHideapp();
afx_msg void OnHide();
afx_msg void OnUnHide();
afx_msg void OnDestroy();
In yourapp.cpp (Not yourdlg.cpp) add this just after #endif
:
m_pIconList[0] = LoadIcon (MAKEINTRESOURCE(IDR_MAINFRAME));
m_pIconList[1] = LoadIcon (MAKEINTRESOURCE(IDR_MAINFRAME));
m_pIconList[2] = LoadIcon (MAKEINTRESOURCE(IDR_MAINFRAME));
In the yourapp.h (not yourdlg.h) add this to the public callback:
HICON m_pIconList[3];
After the last }
down the bottom of this file, add this:
extern CYourApp theApp;
Open stdafx.h and add #define WM_TRAY_NOTIFY WM_APP+1000
after AfxCmn.h's include
.
Now just add a menu using the resource add and name it IDR_TRAY
. Add a restore command (ID_TRAY_RESTORE
) and then you're done....
You may find that when you quit, the icon doesn't disappear. Use the destroy
function and add delete m_pTray ;
to it... Or you could just download my demo project and rip all the code from that?
Anyways, I hope this helped some of you. Vote for this, so I get a T-Shirt haha!