扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:bull77 来源:yesky 2007年11月16日
关键字:
1. BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) ON_COMMAND(ID_VIEW_MAIN_WINDOW, OnViewMainWindow) ON_UPDATE_COMMAND_UI(ID_VIEW_MAIN_WINDOW, OnUpdateViewMainWindow) ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification) ON_WM_SYSCOMMAND() //}}AFX_MSG_MAP END_MESSAGE_MAP() 2. void CMainFrame::OnViewMainWindow() { if(IsWindowVisible()) { ShowWindow(SW_SHOWMINIMIZED); ShowWindow(SW_HIDE); m_TrayIcon.SetIcon(IDI_ICON1); } else { ShowWindow(SW_SHOW); ShowWindow(SW_RESTORE); m_TrayIcon.SetIcon(IDI_ICON2); } } void CMainFrame::OnUpdateViewMainWindow(CCmdUI* pCmdUI) { pCmdUI->SetCheck(IsWindowVisible()); } 3. int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CFrameWnd::OnCreate(lpCreateStruct) == -1) return -1; //创建托盘图标 if (!m_TrayIcon.Create(this, WM_ICON_NOTIFY,"大屏实时显示程序", NULL, IDR_POPUPMENU)) return -1; m_TrayIcon.SetIcon(IDI_ICON1); SetMenu(NULL); return 0; } 4. LRESULT CMainFrame::OnTrayNotification(WPARAM wParam, LPARAM lParam) { if (wParam != IDR_POPUPMENU) return 0L; CMenu menu, *pSubMenu; if (LOWORD(lParam) == WM_RBUTTONUP) { CPoint pos; GetCursorPos(&pos); if (!menu.LoadMenu(IDR_POPUPMENU)) return 0; if (!(pSubMenu=menu.GetSubMenu(0))) return 0; ::SetMenuDefaultItem(pSubMenu->m_hMenu, 3, TRUE); SetForegroundWindow(); pSubMenu->TrackPopupMenu(TPM_RIGHTALIGN | TPM_BOTTOMALIGN | TPM_RIGHTBUTTON, pos.x, pos.y, this); menu.DestroyMenu(); } else if (LOWORD(lParam) == WM_LBUTTONDBLCLK) { if (!menu.LoadMenu(IDR_POPUPMENU)) return 0; if (!(pSubMenu = menu.GetSubMenu(0))) return 0; SetForegroundWindow(); //激活第2个菜单项 SendMessage(WM_COMMAND, pSubMenu->GetMenuItemID(1), 0); menu.DestroyMenu(); } return 0; } void CMainFrame::OnSysCommand(UINT nID, LPARAM lParam) { if(nID==SC_MINIMIZE) { ShowWindow(SW_SHOWMINIMIZED); ShowWindow(SW_HIDE); } else CFrameWnd::OnSysCommand(nID, lParam); } 3. 附录:类的实现代码 CsystemTray的头文件 #ifndef _INCLUDED_SYSTEMTRAY_H_ #define _INCLUDED_SYSTEMTRAY_H_ ///////////////////////////////////////////////////////////////////////////// // CSystemTray window class CSystemTray : public CObject { // Construction/destruction public: CSystemTray(); CSystemTray(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID); virtual ~CSystemTray(); // Operations public: CFrameWnd * m_pFrame; BOOL Enabled() { return m_bEnabled; } BOOL Visible() { return !m_bHidden; } //Create the tray icon Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szTip, HICON icon, UINT uID); //Change or retrieve the Tooltip text BOOL SetTooltipText(LPCTSTR pszTooltipText); BOOL SetTooltipText(UINT nID); CString GetTooltipText() const; //Change or retrieve the icon displayed BOOL SetIcon(HICON hIcon); BOOL SetIcon(LPCTSTR lpIconName); BOOL SetIcon(UINT nIDResource); BOOL SetStandardIcon(LPCTSTR lpIconName); BOOL SetStandardIcon(UINT nIDResource); HICON GetIcon() const; void HideIcon(); void ShowIcon(); void RemoveIcon(); void MoveToRight(); //Change or retrieve the window to send notification messages to BOOL SetNotificationWnd(CWnd* pNotifyWnd); CWnd* GetNotificationWnd() const; //Default handler for tray notification message // Overrides // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CSystemTray) //}}AFX_VIRTUAL // Implementation protected: BOOL m_bEnabled; // does O/S support tray icon? BOOL m_bHidden; // Has the icon been hidden? NOTIFYICONDATA m_tnd; DECLARE_DYNAMIC(CSystemTray) }; #endif /////////////////////////////////////////////////// CsystemTray的实现文件 #include "stdafx.h" #include "SystemTray.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif IMPLEMENT_DYNAMIC(CSystemTray, CObject) ///////////////////////////////////////////////// // CSystemTray construction/creation/destruction CSystemTray::CSystemTray() { memset(&m_tnd, 0, sizeof(m_tnd)); m_bEnabled = FALSE; m_bHidden = FALSE; } CSystemTray::CSystemTray(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID) { Create(pWnd, uCallbackMessage, szToolTip, icon, uID); m_bHidden = FALSE; } BOOL CSystemTray::Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID) { // this is only for Windows 95 (or higher) VERIFY(m_bEnabled = ( GetVersion() & 0xff ) >= 4); if (!m_bEnabled) return FALSE; //Make sure Notification window is valid VERIFY(m_bEnabled = (pWnd && ::IsWindow(pWnd->GetSafeHwnd()))); if (!m_bEnabled) return FALSE; //Make sure we avoid conflict with other messages ASSERT(uCallbackMessage >= WM_USER); //Tray only supports tooltip text up to 64 characters ASSERT(_tcslen(szToolTip) <= 64); // load up the NOTIFYICONDATA structure m_tnd.cbSize = sizeof(NOTIFYICONDATA); m_tnd.hWnd = pWnd->GetSafeHwnd(); m_tnd.uID = uID; m_tnd.hIcon = icon; m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; m_tnd.uCallbackMessage = uCallbackMessage; strcpy (m_tnd.szTip, szToolTip); // Set the tray icon m_pFrame = (CFrameWnd*)pWnd; VERIFY(m_bEnabled = Shell_NotifyIcon(NIM_ADD, &m_tnd)); return m_bEnabled; } CSystemTray::~CSystemTray() { RemoveIcon(); } ///////////////////////////////////////////// // CSystemTray icon manipulation void CSystemTray::MoveToRight() { HideIcon(); ShowIcon(); } void CSystemTray::RemoveIcon() { if (!m_bEnabled) return; m_tnd.uFlags = 0; Shell_NotifyIcon(NIM_DELETE, &m_tnd); m_bEnabled = FALSE; } void CSystemTray::HideIcon() { if (m_bEnabled && !m_bHidden) { m_tnd.uFlags = NIF_ICON; Shell_NotifyIcon (NIM_DELETE, &m_tnd); m_bHidden = TRUE; } } void CSystemTray::ShowIcon() { if (m_bEnabled && m_bHidden) { m_tnd.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP; Shell_NotifyIcon(NIM_ADD, &m_tnd); m_bHidden = FALSE; } } BOOL CSystemTray::SetIcon(HICON hIcon) { if (!m_bEnabled) return FALSE; m_tnd.uFlags = NIF_ICON; m_tnd.hIcon = hIcon; return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); } BOOL CSystemTray::SetIcon(LPCTSTR lpszIconName) { HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName); return SetIcon(hIcon); } BOOL CSystemTray::SetIcon(UINT nIDResource) { HICON hIcon = AfxGetApp()->LoadIcon(nIDResource); return SetIcon(hIcon); } BOOL CSystemTray::SetStandardIcon(LPCTSTR lpIconName) { HICON hIcon = LoadIcon(NULL, lpIconName); return SetIcon(hIcon); } BOOL CSystemTray::SetStandardIcon(UINT nIDResource) { HICON hIcon = ::LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(nIDResource)); return SetIcon(hIcon); } HICON CSystemTray::GetIcon() const { HICON hIcon = NULL; if (m_bEnabled) hIcon = m_tnd.hIcon; return hIcon; } ////////////////////////////////////////////////// // CSystemTray tooltip text manipulation BOOL CSystemTray::SetTooltipText(LPCTSTR pszTip) { if (!m_bEnabled) return FALSE; m_tnd.uFlags = NIF_TIP; _tcscpy(m_tnd.szTip, pszTip); return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); } BOOL CSystemTray::SetTooltipText(UINT nID) { CString strText; VERIFY(strText.LoadString(nID)); return SetTooltipText(strText); } CString CSystemTray::GetTooltipText() const { CString strText; if (m_bEnabled) strText = m_tnd.szTip; return strText; } //////////////////////////////////////////////// // CSystemTray notification window stuff BOOL CSystemTray::SetNotificationWnd(CWnd* pWnd) { if (!m_bEnabled) return FALSE; //Make sure Notification window is valid ASSERT(pWnd && ::IsWindow(pWnd->GetSafeHwnd())); m_tnd.hWnd = pWnd->GetSafeHwnd(); m_tnd.uFlags = 0; return Shell_NotifyIcon(NIM_MODIFY, &m_tnd); } CWnd* CSystemTray::GetNotificationWnd() const { return CWnd::FromHandle(m_tnd.hWnd); } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者