随笔-59  评论-36  文章-0  trackbacks-0
在学习实现托盘图标的过程中,自己尝试着写了个托盘类。
当然了,不用问也知道,这个类是很弱的。

头文件:
//////////////////////////////////////////////////////////////////////////
//    
//    文件说明 : TrayIcon类声明
//    
//    简要说明 : 为方便自己使用,同时以达到练手为目的,于是简单的封装(姑且用"封装")了一个托盘类.
//            使用该类时,可以通过使用重载函数Create来进行内部的设置,随后调用LoadTrayIcon或
//            UnLoadTrayIcon函数来进行在系统托盘中的图标显示和隐藏.
//
//    zhaoyg : 2010.1.23
//
//////////////////////////////////////////////////////////////////////////


#pragma once

class TrayIcon
{
public:
    TrayIcon(
void);
    
~TrayIcon(void);
    typedef 
void (*CALLBACKTrayIconNotify)(LPVOID thisclass ,WPARAM wparam ,LPARAM lparam);
    
void        Create(LPVOID callerthis , const HWND &hwnd , UINT iconID , UINT CallBackMessage, 
                        CALLBACKTrayIconNotify pcallback, LPCTSTR ptips , HICON hicon , 
                        UINT flags 
= NIF_MESSAGE|NIF_ICON|NIF_TIP);
    
void        Create(LPVOID callerthis , const HWND &hwnd , UINT iconID);
    
void        OnTrayNotification(WPARAM wparam ,LPARAM lparam);

    
bool        LoadTrayIcon();
    
bool        UnLoadTrayIcon();

    
void        SetIcon(const HICON &hicon);
    
void        SetTip(const LPCTSTR tipstr);
    
void        SetBalloonTip(UINT timeout , DWORD infoflags , const LPCTSTR infotitle , const LPCTSTR info);
    
void        SetCallBackMessage(UINT CallBackMessage , CALLBACKTrayIconNotify pcallback);

    
// change the existing icon status
    bool        ChangeIcon(const HICON &hicon);
    
bool        ChangeTip(const LPCTSTR tipstr);
    
bool        ChangeBalloonTip(UINT timeout , DWORD infoflags , const LPCTSTR infotitle , const LPCTSTR info);

    
bool        visible();
    
bool        enable();

protected:
    NOTIFYICONDATA            m_iconinfo;
    CALLBACKTrayIconNotify    m_pcallback;
    LPVOID                    m_thisclass;
    
bool                    m_enable;
    
bool                    m_ishide;
};



实现:
//////////////////////////////////////////////////////////////////////////
//    
//    文件说明 : TrayIcon类实现
//    
//    简要说明 : 托盘类
//
//    zhaoyg : 2010.1.23
//
//////////////////////////////////////////////////////////////////////////


#include 
"StdAfx.h"
#include 
"TrayIcon.h"

TrayIcon::TrayIcon(
void)
{
    memset(
&m_iconinfo,0,sizeof(NOTIFYICONDATA)) ;
    m_thisclass 
= NULL;
    m_pcallback 
= NULL;
    m_enable 
= false;
    m_ishide 
= false;
}

TrayIcon::
~TrayIcon(void)
{
}

//////////////////////////////////////////////////////////////////////////
//  函数 Create(LPVOID callerthis , const HWND &hwnd , UINT iconID , UINT CallBackMessage, CALLBACKTrayIconNotify pcallback, LPCTSTR ptips , HICON hicon , UINT flags);
//
//    功能 : NOTIFYICONDATA 
//
//    参数说明:
//      callerthis        : 包含TrayIcon对象的类对象的this指针,该指针将传入参数pcallback指向的回调函数中
//    hwnd                : 包含TrayIcon对象的类的句柄
//    iconID            : 托盘对象ID
//      CallBackMessage    : 回调消息.系统将向hwnd所表示的窗口发送该消息.该消息与一个自定义的消息处理函数关联
//      pcallback            : 回调函数指针.指向的函数即为CallBackMessage消息的响应函数
//      ptips                : 提示字符串.当鼠标放置于托盘上的图标时显示该字符串
//      hicon                : 需要显示的图标句柄
//      flags                : 设置托盘的相关属性.详见MSDN中的NOTIFYICONDATA结构体
//////////////////////////////////////////////////////////////////////////
void TrayIcon::Create( LPVOID callerthis , const HWND &hwnd , UINT iconID , UINT CallBackMessage, 
                      CALLBACKTrayIconNotify pcallback, LPCTSTR ptips , HICON hicon ,
                      UINT flags)
{
    m_iconinfo.cbSize 
= sizeof(NOTIFYICONDATA);
    m_iconinfo.hWnd 
= hwnd;
    m_iconinfo.uID 
= iconID;
    m_iconinfo.hIcon 
= hicon;
    m_iconinfo.uFlags 
= flags;
    wcscpy_s (m_iconinfo.szTip,ptips);
    m_iconinfo.uCallbackMessage 
= CallBackMessage;
    m_pcallback 
=pcallback;
    m_thisclass 
= callerthis;
    m_enable
= true;
}

void TrayIcon::Create( LPVOID callerthis , const HWND &hwnd ,UINT iconID)
{
    m_iconinfo.cbSize 
= sizeof(NOTIFYICONDATA);
    m_iconinfo.hWnd 
= hwnd;
    m_thisclass 
= callerthis;
    m_iconinfo.uID 
= iconID;
    m_enable
= true;
}

void TrayIcon::OnTrayNotification( WPARAM wparam ,LPARAM lparam )
{
    ASSERT(m_pcallback 
!= NULL && m_thisclass != NULL);
    
return m_pcallback(m_thisclass,wparam , lparam);
}

bool TrayIcon::LoadTrayIcon()
{
    
if (m_enable && !m_ishide)
    {
        m_ishide 
= Shell_NotifyIcon(NIM_ADD , &m_iconinfo);
        
return m_ishide;
    }

    
return false;
}

bool TrayIcon::UnLoadTrayIcon()
{
    
if (m_enable && m_ishide)
    {
        
bool tmp = Shell_NotifyIcon(NIM_DELETE , &m_iconinfo);
        m_ishide 
= !tmp;
        m_iconinfo;
        
return tmp;
    }

    
return false;
}

void TrayIcon::SetIcon( const HICON &hicon )
{
    
if (m_enable)
    {
        m_iconinfo.uFlags 
|= NIF_ICON;
        m_iconinfo.hIcon 
= hicon;
    }
}

void TrayIcon::SetTip( const LPCTSTR tipstr )
{
    ASSERT(wcslen(tipstr) 
< 64);

    m_iconinfo.uFlags 
|= NIF_TIP;
    wcscpy_s (m_iconinfo.szTip , tipstr);
}

void TrayIcon::SetBalloonTip( UINT timeout , DWORD infoflags , const LPCTSTR infotitle , const LPCTSTR info )
{
    m_iconinfo.uFlags 
|= NIF_INFO;
    m_iconinfo.uTimeout 
= timeout;
    m_iconinfo.dwInfoFlags 
= infoflags;
    wcscpy_s (m_iconinfo.szInfo,info);
    wcscpy_s (m_iconinfo.szInfoTitle,infotitle);
}

void TrayIcon::SetCallBackMessage( UINT CallBackMessage , CALLBACKTrayIconNotify pcallback )
{
    m_iconinfo.uFlags 
|= NIF_MESSAGE;
    m_iconinfo.uCallbackMessage 
= CallBackMessage;
    m_pcallback 
= pcallback;
}


bool TrayIcon::ChangeIcon( const HICON &hicon )
{
    m_iconinfo.uFlags 
|= NIF_ICON;
    m_iconinfo.hIcon 
= hicon;

    
return Shell_NotifyIcon(NIM_MODIFY , &m_iconinfo);
}

bool TrayIcon::ChangeTip( const LPCTSTR tipstr )
{
    ASSERT(wcslen(tipstr) 
< 64);
    m_iconinfo.uFlags 
|= NIF_TIP;
    wcscpy_s(m_iconinfo.szTip , tipstr);

    
return Shell_NotifyIcon(NIM_MODIFY , &m_iconinfo);
}
bool TrayIcon::ChangeBalloonTip( UINT timeout , DWORD infoflags , const LPCTSTR infotitle , const LPCTSTR info )
{
    ASSERT(wcslen(infotitle)
< 63 && wcslen(info)< 255);

    m_iconinfo.uFlags 
|= NIF_INFO;
    m_iconinfo.uTimeout 
= timeout;
    m_iconinfo.dwInfoFlags 
= infoflags;
    wcscpy_s(m_iconinfo.szInfoTitle , infotitle);
    wcscpy_s(m_iconinfo.szInfo , info);
    
int i =Shell_NotifyIcon(NIM_MODIFY , &m_iconinfo);
    
return i;
}

bool TrayIcon::visible()
{
    
return !m_ishide;
}
bool TrayIcon::enable()
{
    
return m_enable;
}
posted on 2010-02-01 18:10 zhaoyg 阅读(382) 评论(0)  编辑 收藏 引用 所属分类: 小代码MFC学习笔记

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理