任务栏更新问题(高手请进)!!
小弟求救请问怎么样实现任务栏标更新(.net中)
比如动态显示内存使用量 (用一个图表显示在任务栏中,图标上显示内存的是使用量)
你搜索 系统托盘 就有例子了。
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(); } |
[此贴子已经被作者于2005-10-30 13:53:17编辑过]