컴퓨터 이야기
[WinAPI] Tray Icon 등록하기
CatchMeIfYouCan
2006. 6. 29. 06:29
옛날부터 궁금했던거라..
잠깐 시간나서 자료 찾아보고..
간단하게 테스트 프로그램 하나 만들었다..
시간되면 MFC로도 하나 만들어볼까 싶었는데..
그것까진 무리;;ㅎㅎㅎ뭐..어짜피 MFC에서 API쓰면 되니까-_-;; 상관있겠나.ㅋㅋ
대충 실행결과는 단순하게-_-..요정도?ㅋ
걍 오른쪽 클릭하면..메뉴 뜨고.........메뉴 선택하면 실행되고..
버그 투성이 프로그램이다..ㅎㅎㅎㅎ
담에 언젠가 써먹을때가 있겠지?ㅋㅎㅎㅎ
없음 말고-_-;;;;;;;;;;;;;;;
// tray_test.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "shellapi.h"
#define MAX_LOADSTRING 100
#define TRAY_NOTIFY (WM_APP + 100)
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TRAY_TEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TRAY_TEST);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TRAY_TEST);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_TRAY_TEST;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
// 만약 시작시 창이 보이지 안도록 하고 싶다면
// ShowWindow(hWnd, SW_HIDE);
// 로 설정한다.
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
NOTIFYICONDATA nid;
HMENU hMenu, hPopupMenu;
POINT pt;
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CREATE:
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 0;
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
// 트레이아이콘의 이벤트를 전송받을 메시지 타입정의
nid.uCallbackMessage = TRAY_NOTIFY;
nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_TRAY_TEST));
strcpy(nid.szTip,"트레이 아이콘 예제");
// 윈도우 쉘 관리자에게 nid의 정보를 가지는 트레이 아이콘을 등록함을 알린다.
Shell_NotifyIcon(NIM_ADD, &nid);
return 0;
case TRAY_NOTIFY:
switch(lParam){
case WM_RBUTTONDOWN:
// 트레이 아이콘을 우클릭시 나타나는 메뉴를 생성하고 보여준다.
// 메뉴 로드하기
hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_TRAY_ICON));
// hMenu의 서브 메뉴들 중에서 첫번째 서브메뉴를 hPopupMenu로 등록
hPopupMenu = GetSubMenu(hMenu, 0);
// 마우스 위치 받아오기
GetCursorPos(&pt);
// 위에서 받아온 마우스 위치에 메뉴를 생성한다.
// 참고
// BOOL TrackPopupMenu( HMENU hMenu,
// UINT uFlags,
// int x,
// int y,
// int nReserved,
// HWND hWnd,
// HWND prcRect
//);
// 앞뒤로 SetForegroundWindow가 붙는 이유는 가끔 본일이 있을지 모르지만
// 메뉴가 나타나고 사라지지 않고 남아있는 경우를 제거하기 위한 방법이다.
// 이는 윈도우의 공식적인 버그-_-이므로 이 방법을 꼭 사용하여야한다. - 가남사 Win API참조
SetForegroundWindow(hWnd);
TrackPopupMenu(hPopupMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
SetForegroundWindow(hWnd);
// 메뉴 삭제
DestroyMenu(hPopupMenu);
DestroyMenu(hMenu);
break;
case WM_LBUTTONDOWN:
break;
}
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
// 트레이의 팝업메뉴의 메뉴들을 여기서 처리하여 준다.
case ID_MENU_EXIT:
DestroyWindow(hWnd);
break;
case ID_MENU_INTRO:
MessageBox(NULL, "인트로 박스 생성 부분","af",MB_OK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_CLOSE:
break;
case WM_DESTROY:
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.uID = 0;
// 윈도우의 트레이관리자에 nid의 트레이아이콘을 삭제하라는
// 메시지를 전송한다.
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
소스파일 :
잠깐 시간나서 자료 찾아보고..
간단하게 테스트 프로그램 하나 만들었다..
시간되면 MFC로도 하나 만들어볼까 싶었는데..
그것까진 무리;;ㅎㅎㅎ뭐..어짜피 MFC에서 API쓰면 되니까-_-;; 상관있겠나.ㅋㅋ
걍 오른쪽 클릭하면..메뉴 뜨고.........메뉴 선택하면 실행되고..
버그 투성이 프로그램이다..ㅎㅎㅎㅎ
담에 언젠가 써먹을때가 있겠지?ㅋㅎㅎㅎ
없음 말고-_-;;;;;;;;;;;;;;;
// tray_test.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "resource.h"
#include "shellapi.h"
#define MAX_LOADSTRING 100
#define TRAY_NOTIFY (WM_APP + 100)
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_TRAY_TEST, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_TRAY_TEST);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_TRAY_TEST);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_TRAY_TEST;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance; // Store instance handle in our global variable
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
// 만약 시작시 창이 보이지 안도록 하고 싶다면
// ShowWindow(hWnd, SW_HIDE);
// 로 설정한다.
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
NOTIFYICONDATA nid;
HMENU hMenu, hPopupMenu;
POINT pt;
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
switch (message)
{
case WM_CREATE:
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 0;
nid.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
// 트레이아이콘의 이벤트를 전송받을 메시지 타입정의
nid.uCallbackMessage = TRAY_NOTIFY;
nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_TRAY_TEST));
strcpy(nid.szTip,"트레이 아이콘 예제");
// 윈도우 쉘 관리자에게 nid의 정보를 가지는 트레이 아이콘을 등록함을 알린다.
Shell_NotifyIcon(NIM_ADD, &nid);
return 0;
case TRAY_NOTIFY:
switch(lParam){
case WM_RBUTTONDOWN:
// 트레이 아이콘을 우클릭시 나타나는 메뉴를 생성하고 보여준다.
// 메뉴 로드하기
hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_TRAY_ICON));
// hMenu의 서브 메뉴들 중에서 첫번째 서브메뉴를 hPopupMenu로 등록
hPopupMenu = GetSubMenu(hMenu, 0);
// 마우스 위치 받아오기
GetCursorPos(&pt);
// 위에서 받아온 마우스 위치에 메뉴를 생성한다.
// 참고
// BOOL TrackPopupMenu( HMENU hMenu,
// UINT uFlags,
// int x,
// int y,
// int nReserved,
// HWND hWnd,
// HWND prcRect
//);
// 앞뒤로 SetForegroundWindow가 붙는 이유는 가끔 본일이 있을지 모르지만
// 메뉴가 나타나고 사라지지 않고 남아있는 경우를 제거하기 위한 방법이다.
// 이는 윈도우의 공식적인 버그-_-이므로 이 방법을 꼭 사용하여야한다. - 가남사 Win API참조
SetForegroundWindow(hWnd);
TrackPopupMenu(hPopupMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_RIGHTBUTTON, pt.x, pt.y, 0, hWnd, NULL);
SetForegroundWindow(hWnd);
// 메뉴 삭제
DestroyMenu(hPopupMenu);
DestroyMenu(hMenu);
break;
case WM_LBUTTONDOWN:
break;
}
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
// 트레이의 팝업메뉴의 메뉴들을 여기서 처리하여 준다.
case ID_MENU_EXIT:
DestroyWindow(hWnd);
break;
case ID_MENU_INTRO:
MessageBox(NULL, "인트로 박스 생성 부분","af",MB_OK);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_CLOSE:
break;
case WM_DESTROY:
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.uID = 0;
// 윈도우의 트레이관리자에 nid의 트레이아이콘을 삭제하라는
// 메시지를 전송한다.
Shell_NotifyIcon(NIM_DELETE, &nid);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}
소스파일 :