一个Window托盘类 好用

来源:互联网 发布:淘宝店铺无线端在哪里 编辑:程序博客网 时间:2024/05/21 10:58

// 头文件

 

 

#pragma once

 

const int TITLE_LEN         = 63;

const int INFO_LEN     = 255;

#define MSG_TRAY_NOTIFYICON WM_USER + 100

 

class CHYTray

{

public:

     CHYTray(void);

     ~CHYTray(void);

 

     bool Create(const HINSTANCE hInstance, const UINT uIconID, const char* pTip, HWND hWndNotity = 0);

     bool Release();

 

     bool SetIcon(UINT uID);

     bool SetIcon();

 

     bool ShowBalloonTip(const char* szInfo, const char* szInfoTitle, const UINT uTimeout, const DWORD dwInfoFlags = NIIF_INFO);

private:

     HINSTANCE          m_hInstance;       // the hInstance of application.

     NOTIFYICONDATAA        *m_pNotifyIconData;    // the system needs to process taskbar status area messages.

 

     HWND               m_hWnd;               

     HWND               m_hWndNotify;

     bool               m_bMyWnd;          // 记录是否自己创建窗口

     static LRESULT CALLBACK TrayNotifyPro(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

};

 

 

 

// cpp文件

 

 

#include "StdAfx.h"

#include "HYTray.h"

#include <ShellAPI.h>

 

// 这个是为了在explorer崩毁后, 再次创建使用的

const UINT WM_TASKBARCREATED = ::RegisterWindowMessage(_T("TaskbarCreated"));

 

CHYTray::CHYTray(void)

     : m_hWnd(0)

     , m_pNotifyIconData(0)

     , m_hWndNotify(0)

     , m_bMyWnd(false)

{}

 

CHYTray::~CHYTray(void)

{

     Release();

}

 

// 创建托盘, 托盘通过消息通知客户处理

bool CHYTray::Create(const HINSTANCE hInstance, const UINT uIconID, const char* pTip, HWND hWndNotity/* = 0 */)

{

     if(m_pNotifyIconData)

     {

         return false;

     }

 

     //

     if((::GetVersion() & 0xFF) < 4)

     {

         return false;

     }

 

     UINT msg;

     m_hInstance            = hInstance;

     char chWindowClass[10];

     strcpy(chWindowClass, "HYTrayWnd");

     // registers a window class for tray

     WNDCLASSEXA wcex;

     memset(&wcex, 0, sizeof(WNDCLASSEX));

     wcex.cbSize         = sizeof(WNDCLASSEX);

     wcex.lpfnWndProc   = (WNDPROC)(CHYTray::TrayNotifyPro);

     wcex.hInstance         = m_hInstance;

     wcex.lpszClassName = chWindowClass;

     ::RegisterClassExA(&wcex);

 

     // creates the window

     if((m_hWnd = ::CreateWindowA(chWindowClass, pTip, WS_OVERLAPPED, 0, 0, 0, 0, NULL, NULL, m_hInstance, NULL)) == 0)

     {

         return false;

     }

     // 存储对象地址

     ::SetWindowLongPtrA(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);

     m_hWndNotify = hWndNotity;

 

     m_pNotifyIconData = new NOTIFYICONDATAA;

     memset(m_pNotifyIconData, 0, sizeof(NOTIFYICONDATAA));

 

     m_pNotifyIconData->cbSize            = sizeof(NOTIFYICONDATAA);

     m_pNotifyIconData->hWnd              = m_hWnd;

     m_pNotifyIconData->uID               = uIconID;

     m_pNotifyIconData->hIcon             = LoadIcon(hInstance, MAKEINTRESOURCE(uIconID));

     m_pNotifyIconData->uFlags            = NIF_MESSAGE | NIF_ICON | NIF_TIP;

     m_pNotifyIconData->uCallbackMessage  = MSG_TRAY_NOTIFYICON;

     strncpy(m_pNotifyIconData->szTip, pTip, TITLE_LEN);

 

     return ::Shell_NotifyIconA(NIM_ADD, m_pNotifyIconData);

}

 

bool CHYTray::Release()

{

     if(m_pNotifyIconData)

     {

         m_pNotifyIconData->uFlags = 0;

         ::Shell_NotifyIconA(NIM_DELETE, m_pNotifyIconData);

         delete m_pNotifyIconData;

         m_pNotifyIconData = 0;

         m_hWndNotify = 0;

         if(m_bMyWnd)

         {

              m_bMyWnd = false;

              ::DestroyWindow(m_hWnd);

              m_hWnd = 0;

         }

     }

     return true;

}

 

bool CHYTray::SetIcon(UINT uID)

{

     if(m_pNotifyIconData == 0)

     {

         return false;

     }

 

     m_pNotifyIconData->hIcon    = LoadIcon(m_hInstance, MAKEINTRESOURCE(uID));

     m_pNotifyIconData->uFlags   |= NIF_ICON;

     m_pNotifyIconData->uFlags   =~ NIF_INFO;

     return ::Shell_NotifyIconA(NIM_MODIFY, m_pNotifyIconData);

}

 

bool CHYTray::SetIcon()

{

     if(m_pNotifyIconData == 0)

     {

         return false;

     }

     return ::Shell_NotifyIconA(NIM_MODIFY, m_pNotifyIconData);

}

 

// 设置气球显示

// 气球提示

bool CHYTray::ShowBalloonTip(const char* szInfo, const char* szInfoTitle, const UINT uTimeout, const DWORD dwInfoFlags/* = NIIF_INFO*/)

{

     if(m_pNotifyIconData == 0)

     {

         return false;

     }

 

     /*

     #define NIIF_INFO       0x00000001

     #define NIIF_WARNING    0x00000002

     #define NIIF_ERROR      0x00000003

     */

     m_pNotifyIconData->uFlags        |= NIF_INFO;

     m_pNotifyIconData->uTimeout      = uTimeout;

     m_pNotifyIconData->dwInfoFlags   = dwInfoFlags;

     strncpy(m_pNotifyIconData->szInfoTitle, szInfoTitle, TITLE_LEN);

     strncpy(m_pNotifyIconData->szInfo, szInfo, INFO_LEN);

     return ::Shell_NotifyIconA(NIM_MODIFY, m_pNotifyIconData);

}

 

// the processes messages.

LRESULT CHYTray::TrayNotifyPro(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)

{

     // explorer崩毁是调用

     if(WM_TASKBARCREATED == message)

     {

         CHYTray* pHYTray = (CHYTray *)::GetWindowLongPtrA(hWnd, GWL_USERDATA);

         if(pHYTray)

         {

              pHYTray->SetIcon();

              return S_OK;

         }

     }

     else if(MSG_TRAY_NOTIFYICON == message)

     {

         CHYTray* pHYTray = (CHYTray *)::GetWindowLongPtrA(hWnd, GWL_USERDATA);

         if(pHYTray && ::SendMessageA(pHYTray->m_hWndNotify, message, wParam, lParam) == 0)

         {

              return S_OK;

         }

     }

     return DefWindowProc(hWnd, message, wParam, lParam);

}

 

 

// 使用

 

     // TODO: 在此添加额外的初始化代码

     m_hyTray.Create(theApp.m_hInstance, IDR_MAINFRAME, "444");

     m_hyTray1.Create(theApp.m_hInstance, IDR_MAINFRAME, "555", m_hWnd);

 

void CTryTrayDlg::OnBnClickedButton1()

{

     static int i = 0;

     switch(i++ % 2)

     {

     case 0:

         m_hyTray.SetIcon(IDR_MAINFRAME);

         break;

     case 1:

         m_hyTray.SetIcon(0);

         break;

     default:

         m_hyTray.SetIcon();

         break;

     }

}

 

 

 

LRESULT CTryTrayDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)

{

     // TODO: 在此添加专用代码和/或调用基类

     // 响应在托盘图标上的单击

     if(MSG_TRAY_NOTIFYICON == message)

     {

         if ((wParam == IDR_MAINFRAME)&&(lParam == WM_LBUTTONDOWN))

         {

              ::MessageBoxA(m_hWnd, "左键", "左键", MB_OK);

              return 0;

         }

         else if((wParam == IDR_MAINFRAME)&&(lParam == WM_RBUTTONDOWN))

         {

              ::MessageBoxA(m_hWnd, "右键", "右键", MB_OK);

              return 0;

         }

         return 1;

     }

 

     return CDialog::WindowProc(message, wParam, lParam);

}

 

void CTryTrayDlg::OnBnClickedButton2()

{

     m_hyTray.ShowBalloonTip("中国人真厉害", "中国人", 3);

}

 

 

 完整工程

http://download.csdn.net/source/2867522

 

原创粉丝点击