首页产品库评测行情新闻|手机数码笔记本台式机DIY硬件数字家庭数码相机办公外设|软件下载游戏开发|社区

更多

数码相机
MP4
LCD
机箱
音箱

天极网 > 软件频道 > 设计在线 > 网页UI设计 >控制和操纵Win95TaskBar(2)

控制和操纵Win95TaskBar(2)

2000-08-15 00:00作者:-出处:-责任编辑:

二、CTrayIcon类的实现)

这一节介绍如何用一个MFC实现CTrayIcon类,来抽象一些常用的操作,以更方便的利用Task Bar。

1.TRAYICON.H

#ifndef _TRAYICON_H

#define _TRAYICON_H

////////////////

// CTrayIcon manages an icon in the Windows 95 system tray.

//

class CTrayIcon : public CCmdTarget {

protected:

DECLARE_DYNAMIC(CTrayIcon)

NOTIFYICONDATA m_nid; // struct for Shell_NotifyIcon args

public:

CTrayIcon(UINT uID);

~CTrayIcon();

// Call this to receive tray notifications

void SetNotificationWnd(CWnd* pNotifyWnd, UINT uCbMsg);

// SetIcon functions. To remove icon, call SetIcon(0)

//

BOOL SetIcon(UINT uID); // main variant you want to use

BOOL SetIcon(HICON hicon, LPCSTR lpTip);

BOOL SetIcon(LPCTSTR lpResName, LPCSTR lpTip)

{ return SetIcon(lpResName ?

AfxGetApp()->LoadIcon(lpResName) : NULL, lpTip); }

BOOL SetStandardIcon(LPCTSTR lpszIconName, LPCSTR lpTip)

{ return SetIcon(::LoadIcon(NULL, lpszIconName), lpTip); }

virtual LRESULT OnTrayNotification(WPARAM uID, LPARAM lEvent);

};

#endif

2.TRAYICON.CPP

#include "stdafx.h"

#include "trayicon.h"

#include

// for AfxLoadString

IMPLEMENT_DYNAMIC(CTrayIcon, CCmdTarget)

CTrayIcon::CTrayIcon(UINT uID)

{

// Initialize NOTIFYICONDATA

memset(&m_nid, 0 , sizeof(m_nid));

m_nid.cbSize = sizeof(m_nid);

m_nid.uID = uID; // never changes after construction

// Use resource string as tip if there is one

AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip));

}

CTrayIcon::~CTrayIcon()

{

SetIcon(0); // remove icon from system tray

}

/ //////////////

// Set notification window. It must created already.

//

void CTrayIcon::SetNotificationWnd(CWnd* pNotifyWnd, UINT uCbMsg)

{

// If the following assert fails, youre probably

// calling me before you created your window. Oops.

ASSERT(pNotifyWnd= =NULL ||

∶:IsWindow(pNotifyWnd->GetSafeHwnd()));

m_nid.hWnd = pNotifyWnd->GetSafeHwnd();

ASSERT(uCbMsg= =0 || uCbMsg>=WM_USER);

m_nid.uCallbackMessage = uCbMsg;

}

//////////////////

// This is the main variant for setting the icon.

// Sets both the icon and tooltip from resource ID

// To remove the icon, call SetIcon(0)

//

BOOL CTrayIcon::SetIcon(UINT uID)

{

HICON hicon=NULL;

if (uID) {

AfxLoadString(uID, m_nid.szTip, sizeof(m_nid.szTip));

hicon = AfxGetApp()->LoadIcon(uID);

}

return SetIcon(hicon, NULL);

}

//////////////////

// Common SetIcon for all overloads.

//

BOOL CTrayIcon::SetIcon(HICON hicon, LPCSTR lpTip)

{

UINT msg;

m_nid.uFlags = 0;

// Set the icon

if (hicon) {

// Add or replace icon in system tray

msg = m_nid.hIcon ? NIM_MODIFY : NIM_ADD;

m_nid.hIcon = hicon;

m_nid.uFlags |= NIF_ICON;

} else { // remove icon from tray

if (m_nid.hIcon= =NULL)

return TRUE; // already deleted

msg = NIM_DELETE;

}

// Use the tip, if any

if (lpTip)

strncpy(m_nid.szTip, lpTip, sizeof(m_nid.szTip));

if (m_nid.szTip[0])

m_nid.uFlags |= NIF_TIP;

// Use callback if any

if (m_nid.uCallbackMessage && m_nid.hWnd)

m_nid.uFlags |= NIF_MESSAGE;

// Do it

BOOL bRet = Shell_NotifyIcon(msg, &m_nid);

if (msg= =NIM_DELETE || !bRet)

m_nid.hIcon = NULL; // failed

return bRet;

}

/////////////////

// Default event handler handles right-menu and doubleclick.

// Call this function from your own notification handler.

//

LRESULT CTrayIcon::OnTrayNotification(WPARAM wID, LPARAM lEvent)

{

if (wID!=m_nid.uID ||

(lEvent!=WM_RBUTTONUP && lEvent!=WM_LBUTTONDBLCLK))

return 0;

// If theres a resource menu with the same ID as the icon, use it

//as the right-button popup menu. CTrayIcon will interprets the

//first item in the menu as the default command for WM_LBUTTONDBLCLK

//

CMenu menu;

if (!menu.LoadMenu(m_nid.uID))

return 0;

CMenu* pSubMenu = menu.GetSubMenu(0);

if (!pSubMenu)

return 0;

if (lEvent= =WM_RBUTTONUP) {

// Make first menu item the default (bold font)

::SetMenuDefaultItem(pSubMenu->m_hMenu, 0, TRUE);

// Display the menu at the current mouse location. Theres a

//"bug" (Microsoft calls it a feature) in Windows 95 that requires

//calling SetForegroundWindow. To find out more, search for Q135788 in

//MSDN.

//

CPoint mouse;

GetCursorPos(&mouse);

::SetForegroundWindow(m_nid.hWnd);

::TrackPopupMenu(pSubMenu->m_hMenu, 0, mouse.x, mouse.y, 0,

m_nid.hWnd, NULL);

} else // double click: execute first menu item

::SendMessage(m_nid.hWnd, WM_COMMAND,

pSubMenu->GetMenuItemID(0), 0);

return 1; // handled

}

关注此文的读者还看过:

返回软件频道首页

软件频道最新更新

热点推荐

IT嘉年华

编辑推荐

热门
推荐

网友关注

软件
资料
游戏

文章排行

本周
本月
最新更新
天极服务| 关于我们| About us| 网站律师| 电子杂志| RSS订阅| 友情合作| 加入我们| 网站地图| MSN/QQ上看天极
TMG
Copyright (C) 1999-2009 Yesky.com, All Rights Reserved
版权所有 天极网络
商务联系、网站内容、合作建议:010-82657868 在线提交意见反馈
渝ICP证B2-20030003号  通用网址:天极网
天极传媒:天极网|比特网|IT专家网|IT商网|52PK游戏网|IT分众
经营性网站备案信息 网警备案 中国网站排名