#define ID_TASKBARICON 100
#define WM_ICONNOTIFY (WM_USER+101)
在 類中申明
virtual LRESULT DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) ;
在 CPP中 :
BOOL CALLBACK EnumProc( HWND hWnd, LPARAM lParam)
{
static const TCHAR szAfxOldWndProc[] = _T("AfxOldWndProc423"); // Visual C++ 6.0
//check for property and unsubclass if necessary
WNDPROC oldWndProc = (WNDPROC)::GetProp(hWnd, szAfxOldWndProc);
if (oldWndProc!=NULL)
{
SetWindowLong(hWnd, GWL_WNDPROC, (DWord)oldWndProc);
RemoveProp(hWnd, szAfxOldWndProc);
}
return TRUE;
}
LRESULT CLandunDlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
static UINT const WM_TASKBARCREATED = RegisterWindowMessage( _T("TaskbarCreated") );
if( WM_TASKBARCREATED == message ) // Explorer.exe重新啟動
{
Shell_NotifyIcon(NIM_ADD, &m_Nid);
return TRUE;
}
// TODO: Add your specialized code here and/or call the base class
switch( message )
{
case WM_ENDSESSION:
{
DWord dwProcessId;
DWord dwThreadId= GetWindowThreadProcessId(m_hWnd,&dwProcessId);
EnumThreadWindows(dwThreadId, EnumProc,(LPARAM) dwThreadId);
}
return TRUE;
case WM_SYSCOMMAND:
switch( wParam )
{
// 隱藏
case SC_MINIMIZE:
case SC_CLOSE:
ShowWindow(SW_HIDE);
return TRUE;
}
break;
case WM_ICONNOTIFY:
switch( lParam )
{
case WM_LBUTTONDBLCLK: // 恢復顯示
if( IsWindowVisible() )
{
ShowWindow(SW_HIDE);
}
else
{
ShowWindow(SW_RESTORE);
SetForegroundWindow();
}
return TRUE;
case WM_RBUTTONUP: // 顯示菜單
POINT point;
HMENU hMenu, hSubMenu;
GetCursorPos(&point);
hMenu = LoadMenu(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MENU1));
hSubMenu = GetSubMenu(hMenu, 0);
SetForegroundWindow();
SetMenuDefaultItem(hSubMenu, ID_ICON_SHOW, FALSE);
TrackPopupMenu(hSubMenu,TPM_LEFTBUTTON|TPM_RIGHTBUTTON|TPM_LEFTALIGN,point.x, point.y, 0, this->GetSafeHwnd(), NULL);
PostMessage(WM_NULL,0,0);
DestroyMenu(hMenu);
return TRUE;
}
return TRUE;
}
return CDialog::DefWindowProc(message, wParam, lParam);
}
加個 Menu 資源
IDR_MENU1
|_ ID_SHOW
|_ ID_EXIT
// 生成個 ID_SHOW 事件
void CMyDlg::OnShow()
{
// TODO: Add your command handler code here
WindowProc(WM_ICONNOTIFY,0,WM_LBUTTONDBLCLK);
}
// 生成個 ID_EXIT 事件
void CMyDlg::OnExit()
{
// TODO: Add your command handler code here
CDialog::OnClose();
}