在说窗口隐藏之前,把两个东西了解一下:
postMessage()把消息发送到消息队列;直接返回
sendMessage()不进入消息队列,而直接调用消息过程;需要消息被处理之后返回;
MFC 中PreTranslateMessage是GetMessage(...)函数的下一级操作,即GetMessage(...)从消息队列中获
取消息后,交由PreTranslateMessage()处理,若其返回FALSE则再交给TranslateMessage和
DispatchMessage处理(进入WindowProc); 在消息有TranslateMessage之后,会调用一个
OnSysCommand(UINT nID, LPARAM lParam),这个函数主要是截获控制命令的。第一参数的前缀为:SC_;注意:nID参数的低四位被Windows内部使用。当应用程
序测试nID的值时,它必须用位与操作符AND将值0xFFF0与nID的值组合在一起以获得正确的结果。
下面进入正题:
主要用到下面函数和结构体:
Shell_NotifyIcon和NOTIFYICONDATA(有兴趣的可以再百度一下,这里就不copy了)
我把它封装成为一个类:NotifyIcon
下载地址:http://download.csdn.net/user/liu563582815
NotifyIcon.h
// NotifyIcon.h: interface for the NotifyIcon class. // ////////////////////////////////////////////////////////////////////// #if !defined(AFX_NOTIFYICON1_H__72C21498_5672_48B5_8C9C_81D1D5BC8338__INCLUDED_) #define AFX_NOTIFYICON1_H__72C21498_5672_48B5_8C9C_81D1D5BC8338__INCLUDED_ #if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000 class NotifyIcon { public: /* @param wnd:对话框指针 @param iconID:图标ID,用于标示托盘图标和显示托盘图标 @param messageID:消息ID,用于把托盘产生的消息,发送给对应窗口 */ void Init(CWnd *wnd,int iconID,int messageID); void ShowWindow(); void HideWindow(int StringID); NotifyIcon(); virtual ~NotifyIcon(); private: NOTIFYICONDATA nd; CWnd *wnd; }; #endif // !defined(AFX_NOTIFYICON1_H__72C21498_5672_48B5_8C9C_81D1D5BC8338__INCLUDED_) ///*******用时注意********/// ///*******在对话框初始函数中调用Init,否则对话框还没有句柄,将出现问题********/// ///*******需要自定义消息和函数,并修改系统消息函数,拦截消息********/// /* void CNotifyIconDlg::OnShowTask(WPARAM wParam,LPARAM lParam)//自定义消息函数,在afxmsg里面声明函数,并加入消息映射 { if (wParam!=IDR_MAINFRAME) { return; } else{ switch(lParam) { case WM_RBUTTONUP: break; case WM_LBUTTONDBLCLK: ni.ShowWindow(); break; } } } void CNotifyIconDlg::OnSysCommand(UINT nID, LPARAM lParam)//系统函数,加入第一个语句,用于拦截系统消息 { if ((nID & 0xFFF0) == SC_MINIMIZE) { ni.HideWindow(IDS_ABOUTBOX); return; } if ((nID & 0xFFF0) == IDM_ABOUTBOX) { CAboutDlg dlgAbout; dlgAbout.DoModal(); } else { CDialog::OnSysCommand(nID, lParam); } } */
NotifyIcon.cpp
// NotifyIcon1.cpp: implementation of the NotifyIcon class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "NotifyIcon.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// NotifyIcon::NotifyIcon() { } NotifyIcon::~NotifyIcon() { } void NotifyIcon::HideWindow(int StringID) { nd.hWnd=wnd->m_hWnd; LoadString(AfxGetInstanceHandle(),StringID,nd.szTip,sizeof(nd.szTip)); Shell_NotifyIcon(NIM_ADD,&nd); wnd->ShowWindow(SW_HIDE); } void NotifyIcon::ShowWindow() { Shell_NotifyIcon(NIM_DELETE,&nd); //在托盘区删除图标 wnd->ShowWindow(SW_SHOW); } void NotifyIcon::Init(CWnd *wnd,int iconID,int messageID) { this->wnd=wnd; nd.cbSize=sizeof(NOTIFYICONDATA); nd.uID=iconID; nd.uFlags=NIF_ICON|NIF_MESSAGE|NIF_TIP; nd.uCallbackMessage=messageID; nd.hIcon=LoadIcon(AfxGetInstanceHandle(),MAKEINTRESOURCE(nd.uID)); }