Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Complete FTP Server

, 30 May 2005
This article presents a fully functional implementation of a FTP server.
/****************************************************************/
/*																*/
/*  SystemTray.cpp												*/
/*																*/
/*  Implementation of the CSystemTray class						*/
/*																*/
/*  Copyright Pablo Software Solutions 2005						*/
/*	http://www.pablosoftwaresolutions.com						*/
/*																*/
/*  Last updated: May 28, 2005									*/
/*																*/
/****************************************************************/

#include "stdafx.h"
#include "SystemTray.h"

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

IMPLEMENT_DYNAMIC(CSystemTray, CWnd)


/********************************************************************/
/*																	*/
/* Function name : CSystemTray										*/
/* Description   : Constructor										*/
/*																	*/
/********************************************************************/
CSystemTray::CSystemTray()
{
    ZeroMemory(&m_NotifyIconData, sizeof(m_NotifyIconData));
}


/********************************************************************/
/*																	*/
/* Function name : ~CSystemTray										*/
/* Description   : Destructor										*/
/*																	*/
/********************************************************************/
CSystemTray::~CSystemTray()
{
	// remove icon from system tray
    m_NotifyIconData.uFlags = 0;
    Shell_NotifyIcon(NIM_DELETE, &m_NotifyIconData);

	// destroy dummy window
    DestroyWindow();
}


BEGIN_MESSAGE_MAP(CSystemTray, CWnd)
	//{{AFX_MSG_MAP(CSystemTray)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/********************************************************************/
/*																	*/
/* Function name : Create											*/
/* Description   : Create systemtray icon							*/
/*																	*/
/********************************************************************/
BOOL CSystemTray::Create(UINT uCallbackMessage, LPCTSTR lpszToolTip, HICON hIcon, UINT uID)
{
	// create dummy window
    if (!CWnd::CreateEx(0, AfxRegisterWndClass(0), "CSystemTray Dummy Window", WS_POPUP, 0,0,0,0, NULL, 0))
		return FALSE;

    // setup NOTIFYICONDATA
    m_NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
    m_NotifyIconData.hWnd   = m_hWnd;
    m_NotifyIconData.uID    = uID;
    m_NotifyIconData.hIcon  = hIcon;
    m_NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
    m_NotifyIconData.uCallbackMessage = uCallbackMessage;
    lstrcpy(m_NotifyIconData.szTip, lpszToolTip);

    // add tray icon
    return Shell_NotifyIcon(NIM_ADD, &m_NotifyIconData);
}


/********************************************************************/
/*																	*/
/* Function name : SetIcon											*/
/* Description   : Modify icon in the system tray					*/
/*																	*/
/********************************************************************/
BOOL CSystemTray::SetIcon(UINT nIDResource)
{
    HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
    m_NotifyIconData.uFlags = NIF_ICON;
    m_NotifyIconData.hIcon = hIcon;
    return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}


/********************************************************************/
/*																	*/
/* Function name : SetNotificationWnd								*/
/* Description   : Modify notification window						*/
/*																	*/
/********************************************************************/
BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd)
{
    m_NotifyIconData.hWnd = pWnd->GetSafeHwnd();
    m_NotifyIconData.uFlags = 0;
    return Shell_NotifyIcon(NIM_MODIFY, &m_NotifyIconData);
}


/********************************************************************/
/*																	*/
/* Function name : OnTrayNotification								*/
/* Description   : Handle notification messages						*/
/*																	*/
/********************************************************************/
LRESULT CSystemTray::OnTrayNotification(UINT wParam, LONG lParam) 
{
    // is it for us ?
    if (wParam != m_NotifyIconData.uID)
        return 0L;

    CWnd *pMainWnd = AfxGetMainWnd();

    switch(LOWORD(lParam))
	{
		case WM_RBUTTONUP:
		{    
			// right button click brings up a context menu
			CMenu menu, *pSubMenu;
			if (!menu.LoadMenu(m_NotifyIconData.uID)) 
				return 0;
			
			pSubMenu = menu.GetSubMenu(0);

			if (pSubMenu == NULL)
				return 0;

			CPoint pos;
			GetCursorPos(&pos);

			// display popup menu
			pSubMenu->TrackPopupMenu(0, pos.x, pos.y, pMainWnd, NULL);

			menu.DestroyMenu();
			break;
		} 
    
		case WM_LBUTTONDBLCLK:
		{
			// double click brings up the main dialog
			pMainWnd->ShowWindow(SW_SHOW);
			pMainWnd->SetForegroundWindow();
			break;
		}
		default:
			break;
	}
    return 1;
}


/********************************************************************/
/*																	*/
/* Function name : WindowProc										*/
/* Description   : Dispatch messages through the window�s message	*/
/*				   map												*/
/*																	*/
/********************************************************************/
LRESULT CSystemTray::WindowProc(UINT uMsg, WPARAM wParam, LPARAM lParam) 
{
    if (uMsg == m_NotifyIconData.uCallbackMessage)
	{
        return OnTrayNotification(wParam, lParam);
	}
	return CWnd::WindowProc(uMsg, wParam, lParam);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Pablo van der Meer
Web Developer
Netherlands Netherlands
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141216.1 | Last Updated 30 May 2005
Article Copyright 2002 by Pablo van der Meer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid