trayicon.cpp

 // TrayIcon.cpp : implementation file
//
// This code is Copyright (c) 1999 - 2001 by SDMSoft (Damjan Miholjevich).  All rights under
// copyright are reserved, except as set forth below.
//
// 1. You may use or modifie this code for private purpose. 
//  
//
// 2. If you wish to modifie this code or use it for public purpose
//    conntact me at cppcoder@hotmail.com
//
#include "stdafx.h"
#include "TrayIcon.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CTrayIcon
const char* kpcTrayNotificationMsg = "MY_MSG";
CTrayIcon::CTrayIcon(CWnd* pParent, HICON hIcon, char* txtToolTip, UINT uID)
{
nTrayNotificationMsg=RegisterWindowMessage(kpcTrayNotificationMsg);
Create(pParent,hIcon,txtToolTip,uID);
}

CTrayIcon::~CTrayIcon()
{
iconData.uFlags =0;
Shell_NotifyIcon(NIM_DELETE, &iconData);
DestroyWindow();
}


BEGIN_MESSAGE_MAP(CTrayIcon, CWnd)
//{{AFX_MSG_MAP(CTrayIcon)

//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CTrayIcon message handlers

//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

BOOL CTrayIcon::Create(CWnd* pParent, HICON hIcon, char *txtToolTip,UINT uID)
{

m_pParent=pParent;

CWnd::CreateEx(0, AfxRegisterWndClass(0), _T(""), WS_POPUP, 0,0,10,10, NULL, 0);

iconData.cbSize=sizeof(NOTIFYICONDATA);
iconData.hIcon =hIcon;
iconData.hWnd =this->GetSafeHwnd();
strcpy(iconData.szTip,txtToolTip);
iconData.uCallbackMessage =nTrayNotificationMsg;
iconData.uFlags =NIF_ICON|NIF_TIP|NIF_MESSAGE;
iconData.uID=uID;

Shell_NotifyIcon(NIM_ADD,&iconData);


return TRUE;
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

LRESULT CTrayIcon::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
switch(LOWORD(lParam))
{
case WM_RBUTTONUP://ce kliknemo z desno miskino tipko (if clicked with right mouse button)
{
POINT point;
CMenu menu;
CMenu *submenu;
menu.LoadMenu(iconData.uID );
submenu = menu.GetSubMenu(0);
GetCursorPos(&point);
submenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,
point.x, point.y,m_pParent,NULL);

m_pParent->PostMessage(WM_NULL, 0, 0);
menu.DestroyMenu();
break;
}
case WM_LBUTTONDBLCLK://ce dvokliknemo (if doubleclikcked)
m_pParent->PostMessage(WM_SYSCOMMAND, SC_RESTORE);
}
return CWnd::WindowProc(message, wParam, lParam);
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Project Homepage: