#include "stdafx.h" #include "TrayTest.h" #include "about.h"#include "MainFrm.h"#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif///////////////////////////////////////////////////////////////////////////// // CMyAppBEGIN_MESSAGE_MAP(CMyApp, CWinApp) //{{AFX_MSG_MAP(CMyApp) ON_COMMAND(ID_APP_ABOUT, OnAppAbout) // NOTE - the ClassWizard will add and remove mapping macros here. // DO NOT EDIT what you see in these blocks of generated code! //}}AFX_MSG_MAP END_MESSAGE_MAP()///////////////////////////////////////////////////////////////////////////// // CMyApp constructionCMyApp::CMyApp() { // TODO: add construction code here, // Place all significant initialization in InitInstance }///////////////////////////////////////////////////////////////////////////// // The one and only CMyApp objectCMyApp theApp;///////////////////////////////////////////////////////////////////////////// // CMyApp initializationBOOL CMyApp::InitInstance() { // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need.#ifdef _AFXDLL Enable3dControls(); // Call this when using MFC in a shared DLL #else Enable3dControlsStatic(); // Call this when linking to MFC statically #endif // Change the registry key under which our settings are stored. // TODO: You should modify this string to be something appropriate // such as the name of your company or organization. SetRegistryKey(_T("Local AppWizard-Generated Applications")); // To create the main window, this code creates a new frame window // object and then sets it as the application's main window object. CMainFrame* pFrame = new CMainFrame; m_pMainWnd = pFrame; // create and load the frame with its resources pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL); // The one and only window has been initialized, so show and update it. pFrame->ShowWindow(SW_HIDE); pFrame->UpdateWindow(); OnAppAbout(); return TRUE; }///////////////////////////////////////////////////////////////////////////// // CMyApp message handlers // App command to run the About dialog void CMyApp::OnAppAbout() { CAboutDlg().DoModal(); }///////////////////////////////////////////////////////////////////////////// // CMyApp message handlers
#include "TrayTest.h"
#include "about.h"#include "MainFrm.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/////////////////////////////////////////////////////////////////////////////
// CMyAppBEGIN_MESSAGE_MAP(CMyApp, CWinApp)
//{{AFX_MSG_MAP(CMyApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code!
//}}AFX_MSG_MAP
END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
// CMyApp constructionCMyApp::CMyApp()
{
// TODO: add construction code here,
// Place all significant initialization in InitInstance
}/////////////////////////////////////////////////////////////////////////////
// The one and only CMyApp objectCMyApp theApp;/////////////////////////////////////////////////////////////////////////////
// CMyApp initializationBOOL CMyApp::InitInstance()
{
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif // Change the registry key under which our settings are stored.
// TODO: You should modify this string to be something appropriate
// such as the name of your company or organization.
SetRegistryKey(_T("Local AppWizard-Generated Applications"));
// To create the main window, this code creates a new frame window
// object and then sets it as the application's main window object. CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame; // create and load the frame with its resources pFrame->LoadFrame(IDR_MAINFRAME,
WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL,
NULL); // The one and only window has been initialized, so show and update it.
pFrame->ShowWindow(SW_HIDE);
pFrame->UpdateWindow();
OnAppAbout(); return TRUE;
}/////////////////////////////////////////////////////////////////////////////
// CMyApp message handlers
// App command to run the About dialog
void CMyApp::OnAppAbout()
{
CAboutDlg().DoModal();
}/////////////////////////////////////////////////////////////////////////////
// CMyApp message handlers
typedef struct _NOTIFYICONDATA {
DWORD cbSize;
HWND hWnd;
UINT uID;
UINT uFlags;
UINT uCallbackMessage;
HICON hIcon;
TCHAR szTip[64];
DWORD dwState; //Version 5.0
DWORD dwStateMask; //Version 5.0
TCHAR szInfo[256]; //Version 5.0
UINT uTimeout; //Version 5.0
TCHAR szInfoTitle[64]; //Version 5.0
DWORD dwInfoFlags; //Version 5.0
} NOTIFYICONDATA, *PNOTIFYICONDATA; 为了要在系统托盘中显示图标,用NIM_ADD标志调用Shell_NotifyIcon函数。
#define ID_TASKBARICON 100
#define WM_ICONNOTIFY (WM_USER + 101)NOTIFYICONDATA nid;// 初始化系统托盘图标
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = ID_TASKBARICON;
nid.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
nid.uCallbackMessage = WM_ICONNOTIFY;
nid.hIcon = LoadImage(hInst, MAKEINTRESOURCE(IDI_TRAY1), IMAGE_ICON, 16, 16, 0);
strcpy(nid.szTip, "My Tooltip Text");
Shell_NotifyIcon(NIM_ADD, &nid);cbSize成员是结构的大小(使用它主要是为了支持将来这个结构大小增加)。
hWnd是窗口句柄。当图标发生某事件时(如单击、双击等),Windows将向窗口发送uCallbackMessage成员指定的消息。uID成员指定与图标关联的ID。它不是很重要,除非你需要显示并跟踪几个图标。
uFlag成员告诉Windows应该读取哪个成员。当添加一个图标时,应该包含这个结构的大多数成员。当更新图标时,如只是需要改变图标时,你只要设置相应
的标志就可以了。hIcon成员是你想显示的图标。
最后,szTip成员是提示文本。设置好这些结构成员后,调用Shell_NotifyIcon函数。当与图标关联的事件发生时,Windows将发送uCallbackMessage成员指定的消息。IParam包含发送的消息。当获得WM_LBUTTONDBLCLK消息时显示主窗口
或者启动主程序。当获得WM_RBUTTONUP消息时显示菜单。注意:如果在系统托盘中单击鼠标右键,有时会有一个弹出式(上下文菜单)菜单显示/消失的怪现象,详细信息击解决办法请参阅微软知识库文章Q135788,也可以参考下列代码加以解决。
switch(nMsg) {
case WM_ICONNOTIFY:
switch(lParam) {
case WM_LBUTTONDBLCLK:
// Load main window here
break;
case WM_RBUTTONUP:
{
POINT point;
HMENU hMenu, hSubMenu;
// Get mouse position
GetCursorPos(&point);
// Popup context menu
hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_MYMENU));
hSubMenu = GetSubMenu(hMenu, 0);
SetMenuDefaultItem(hSubMenu, IDM_DEFAULTCMD, FALSE);
SetForegroundWindow(hMainDlg); // Per KB Article Q135788
TrackPopupMenu(hSubMenu,
TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_LEFTALIGN,
point.x, point.y, 0, hWnd, NULL);
PostMessage(hMainDlg, WM_NULL, 0, 0); // Per KB Article Q135788
DestroyMenu(hMenu);
}
break;
default:
return FALSE;
}
}不论什么时候,你都可以用 NIM_MODIFY 调用 Shell_NotifyIcon。程序终止之前,用 NIM_DELETE 调用 Shell_NotifyIcon从托盘中清除图标。Shell_NotifyIcon(NIM_DELETE, &nid);