科技行者

行者学院 转型私董会 科技行者专题报道 网红大战科技行者

知识库

知识库 安全导航

至顶网软件频道基础软件也谈制作系统托盘程序

也谈制作系统托盘程序

  • 扫一扫
    分享文章到微信

  • 扫一扫
    关注官方公众号
    至顶头条

看到贵网站刊登的文章《制作系统托盘程序》,颇有一番感触

作者:bull77 来源:yesky 2007年11月16日

关键字:

  • 评论
  • 分享微博
  • 分享邮件
2. 使用本类的步骤如下:

  第一步:在VC变成环境下,建立一个工程,基于对话框或者是单文档(或者是多文档)你随便,所有的选项都取默认值即可。

  第二步:在mainfrm.h中定义变量CSystemTray m_TrayIcon;并添加函数声明:

   afx_msg LRESULT OnTrayNotification(WPARAM wParam, LPARAM lParam);

   afx_msg void OnSysCommand(UINT nID, LPARAM lParam);

  第三步:在StaAFX.h中定义消息:

   #define WM_ICON_NOTIFY WM_USER + 1

  第四步:自定义菜单IDR_POPUPMENU,其中最少包括一项:ID:ID_VIEW_MAIN_WINDOW,Caption为"显示主窗口",并为该项在类CmainFrame中添加消息映射函数,COMMAND和UPDATE_COMMAND_UI。

  第五步:在Mainframe.cpp中添加以下内容:

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领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。

    重磅专题
    往期文章
    最新文章