#define ID_TASKBARICON 100
#define WM_ICONNOTIFY (WM_USER+101)
#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;
}
{
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;
}
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);
}
{
// 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();
}
{
// TODO: Add your command handler code here
CDialog::OnClose();
}