글
Technical Article/펌 2005. 7. 14. 10:39트레이(tray)에 넣어보자
트레이(tray)에 넣어보자
작성자 : 이근호
작성이 : 2005.07.14 (최초작성일 2002년 2월)
1. CTrayIcon클래스를 생성.
CTrayIcon.h
// TrayIcon.h: interface for the CTrayIcon class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_TRAYICON_H__24E119DA_5ACC_44B5_A130_CA747C2359A5__INCLUDED_)
#define AFX_TRAYICON_H__24E119DA_5ACC_44B5_A130_CA747C2359A5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CTrayIcon
{
private:
NOTIFYICONDATA m_tnd;
public:
CTrayIcon();
virtual ~CTrayIcon();
BOOL Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID);
BOOL SetIcon(HICON hIcon);
BOOL SetIcon(LPCTSTR lpszIconName);
BOOL SetIcon(UINT nIDResource);
void RemoveIcon();
};
#endif // !defined(AFX_TRAYICON_H__24E119DA_5ACC_44B5_A130_CA747C2359A5__INCLUDED_)
CTrayIcon.cpp
// TrayIcon.cpp: implementation of the CTrayIcon class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TrayIcon.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTrayIcon::CTrayIcon()
{
}
CTrayIcon::~CTrayIcon()
{
}
BOOL CTrayIcon::Create(CWnd* pWnd, UINT uCallbackMessage,
LPCTSTR szToolTip, HICON icon, UINT uID)
{
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);
return Shell_NotifyIcon(NIM_ADD, &m_tnd);
}
BOOL CTrayIcon::SetIcon(HICON hIcon)
{
m_tnd.uFlags = NIF_ICON;
m_tnd.hIcon = hIcon;
return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}
BOOL CTrayIcon::SetIcon(LPCTSTR lpszIconName)
{
HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName);
return SetIcon(hIcon);
}
BOOL CTrayIcon::SetIcon(UINT nIDResource)
{
HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
return SetIcon(hIcon);
}
void CTrayIcon::RemoveIcon()
{
m_tnd.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE, &m_tnd);
}
2. #include "TrayIcon.h"
다이얼로그 기반일때 XXXDlg.h에 삽입
SDI기반일때 MainFrm.h에 삽입
3. 멤버변수,함수 추가
CTrayIcon m_TrayIcon;
LRESULT OnTrayNotification(WPARAM wParam, LPARAM lParam);
기반에 따라 상동.
4. OnCreate() 오버라이딩
if ( !m_TrayIcon.Create(this, WM_ICON_NOTIFY, _T("Vin's Messenger"),NULL, IDR_MAINFRAME))
return -1;
m_TrayIcon.SetIcon(IDR_MAINFRAME);
5. DestroyWindow() 오버라이딩
m_TrayIcon.RemoveIcon();
6. 트레이 아이콘 이벤트 추가
cpp파일에서
#define WM_ICON_NOTIFY WM_USER+10
삽입
7. 이벤트 메세지 맵 추가
BEGIN_MESSAGE_MAP(COnDlg, CDialog)
//{{AFX_MSG_MAP(COnDlg)
ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
8. 리소스에 메뉴 추가
여기서 임의의 아이디 IDR_MAINFRAME의 첫번째 메뉴에 해당.
LRESULT COnDlg::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
CMenu menu, *pSubMenu;
int nEvent = LOWORD(lParam);
switch ( nEvent )
{
case WM_RBUTTONUP:
{
if(!menu.LoadMenu(IDR_MENU1)) return 0;
if(!(pSubMenu = menu.GetSubMenu(0))) return 0;
CPoint pos;
GetCursorPos(&pos);
SetForegroundWindow();
pSubMenu->TrackPopupMenu(TPM_RIGHTALIGN, pos.x, pos.y, this);
menu.DestroyMenu();
break;
}
case WM_LBUTTONDBLCLK:
{
AfxGetMainWnd()->ShowWindow(SW_HIDE);
// SendMessage(WM_COMMAND, ID_APP_ABOUT);
break;
}
}
return 1;
}
8. 해당메뉴 이벤트 오버라이딩
이제 추가해줄거 추가해주면 끝~
작성자 : 이근호
작성이 : 2005.07.14 (최초작성일 2002년 2월)
1. CTrayIcon클래스를 생성.
CTrayIcon.h
// TrayIcon.h: interface for the CTrayIcon class.
//
//////////////////////////////////////////////////////////////////////
#if !defined(AFX_TRAYICON_H__24E119DA_5ACC_44B5_A130_CA747C2359A5__INCLUDED_)
#define AFX_TRAYICON_H__24E119DA_5ACC_44B5_A130_CA747C2359A5__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
class CTrayIcon
{
private:
NOTIFYICONDATA m_tnd;
public:
CTrayIcon();
virtual ~CTrayIcon();
BOOL Create(CWnd* pWnd, UINT uCallbackMessage, LPCTSTR szToolTip, HICON icon, UINT uID);
BOOL SetIcon(HICON hIcon);
BOOL SetIcon(LPCTSTR lpszIconName);
BOOL SetIcon(UINT nIDResource);
void RemoveIcon();
};
#endif // !defined(AFX_TRAYICON_H__24E119DA_5ACC_44B5_A130_CA747C2359A5__INCLUDED_)
CTrayIcon.cpp
// TrayIcon.cpp: implementation of the CTrayIcon class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "TrayIcon.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CTrayIcon::CTrayIcon()
{
}
CTrayIcon::~CTrayIcon()
{
}
BOOL CTrayIcon::Create(CWnd* pWnd, UINT uCallbackMessage,
LPCTSTR szToolTip, HICON icon, UINT uID)
{
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);
return Shell_NotifyIcon(NIM_ADD, &m_tnd);
}
BOOL CTrayIcon::SetIcon(HICON hIcon)
{
m_tnd.uFlags = NIF_ICON;
m_tnd.hIcon = hIcon;
return Shell_NotifyIcon(NIM_MODIFY, &m_tnd);
}
BOOL CTrayIcon::SetIcon(LPCTSTR lpszIconName)
{
HICON hIcon = AfxGetApp()->LoadIcon(lpszIconName);
return SetIcon(hIcon);
}
BOOL CTrayIcon::SetIcon(UINT nIDResource)
{
HICON hIcon = AfxGetApp()->LoadIcon(nIDResource);
return SetIcon(hIcon);
}
void CTrayIcon::RemoveIcon()
{
m_tnd.uFlags = 0;
Shell_NotifyIcon(NIM_DELETE, &m_tnd);
}
2. #include "TrayIcon.h"
다이얼로그 기반일때 XXXDlg.h에 삽입
SDI기반일때 MainFrm.h에 삽입
3. 멤버변수,함수 추가
CTrayIcon m_TrayIcon;
LRESULT OnTrayNotification(WPARAM wParam, LPARAM lParam);
기반에 따라 상동.
4. OnCreate() 오버라이딩
if ( !m_TrayIcon.Create(this, WM_ICON_NOTIFY, _T("Vin's Messenger"),NULL, IDR_MAINFRAME))
return -1;
m_TrayIcon.SetIcon(IDR_MAINFRAME);
5. DestroyWindow() 오버라이딩
m_TrayIcon.RemoveIcon();
6. 트레이 아이콘 이벤트 추가
cpp파일에서
#define WM_ICON_NOTIFY WM_USER+10
삽입
7. 이벤트 메세지 맵 추가
BEGIN_MESSAGE_MAP(COnDlg, CDialog)
//{{AFX_MSG_MAP(COnDlg)
ON_MESSAGE(WM_ICON_NOTIFY, OnTrayNotification)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
8. 리소스에 메뉴 추가
여기서 임의의 아이디 IDR_MAINFRAME의 첫번째 메뉴에 해당.
LRESULT COnDlg::OnTrayNotification(WPARAM wParam, LPARAM lParam)
{
CMenu menu, *pSubMenu;
int nEvent = LOWORD(lParam);
switch ( nEvent )
{
case WM_RBUTTONUP:
{
if(!menu.LoadMenu(IDR_MENU1)) return 0;
if(!(pSubMenu = menu.GetSubMenu(0))) return 0;
CPoint pos;
GetCursorPos(&pos);
SetForegroundWindow();
pSubMenu->TrackPopupMenu(TPM_RIGHTALIGN, pos.x, pos.y, this);
menu.DestroyMenu();
break;
}
case WM_LBUTTONDBLCLK:
{
AfxGetMainWnd()->ShowWindow(SW_HIDE);
// SendMessage(WM_COMMAND, ID_APP_ABOUT);
break;
}
}
return 1;
}
8. 해당메뉴 이벤트 오버라이딩
이제 추가해줄거 추가해주면 끝~
RECENT COMMENT