16,548
社区成员




#include "stdafx.h"
#include <iostream>
using namespace std;
void term_func() {
cout << "term_func was called by terminate." << endl;
exit( -1 );
}
int _tmain(int argc, _TCHAR* argv[])
{
try
{
set_terminate( term_func );
throw "Out of memory!"; // No catch handler for this exception
}
catch( int )
{
cout << "Integer exception raised." << endl;
}
return 0;
}
//handle to the Tray Notification window
HWND hwndTrayNotificationWnd;
BOOL CALLBACK EnumChildProc(
HWND hwnd,
LPARAM lParam
)
{
TCHAR szWindowText[255];
::GetWindowText(hwnd,szWindowText,255);
if(0 == _tcsicmp(szWindowText,_T("Notification Area")))
{
//found the "Notification Area" window.
//This is on Windows XP
hwndTrayNotificationWnd = hwnd;
return FALSE;
}
return TRUE;
}
void RefreshSysTrayIcons()
{
//clear the handle
hwndTrayNotificationWnd = NULL;
//locate the "Shell_TrayWnd" first
HWND hwnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
//now, locate the "Notification Area"
EnumChildWindows(hwnd,EnumChildProc,0);
//if we did not find "Notification area", fall back to "TrayNotifyWnd" This is
//for Win98
if(NULL == hwndTrayNotificationWnd)
{
hwndTrayNotificationWnd = ::FindWindowEx(hwnd,NULL,_T("TrayNotifyWnd"),NULL);
}
//the icons are nSkipPixels apart. We first try to reach the
//center of the top-left icon, from there on , adding
//nSkipPixels x axis-wise and y axis-wise shall move mouse
//to the center of the next icon
int nSkipPixels = GetSystemMetrics(SM_CXSMICON);
if(hwndTrayNotificationWnd)
{
//found it.. now, simulate mouse moves in the notification area to
//cause a refresh
//first cache the current cursor position.
//Since we are moving the mouse, we have to
//restore it back to where it was.
POINT point;
GetCursorPos(&point);
INPUT input;
input.type = INPUT_MOUSE;
input.mi.dwExtraInfo = 0;
input.mi.mouseData = 0;
input.mi.time = 0;
input.mi.dwFlags = (MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE);
int nCXFullScreen = GetSystemMetrics(SM_CXSCREEN);
int nCYFullScreen = GetSystemMetrics(SM_CYSCREEN);
RECT oRect;
//moving mouse over dead tray icons will result in icons being
//shuffled around again. Now, this could lead to some icons again
//getting skipped. Hence, apply retry here.
//loop NUM_RETRIES times
int nNumRetries = 1;//tweak this for your needs
while(nNumRetries)
{
::GetWindowRect(hwndTrayNotificationWnd,&oRect);
//now send a series of mouse move messages across the
//tray window. This is done because we don't know
//in which position the lingering tray icon actually is
//move to the center of the first icon
oRect.top += nSkipPixels/2;
oRect.left += nSkipPixels/2;
int nXPos;
for(;oRect.top < oRect.bottom; oRect.top += nSkipPixels)
{
//normalize
input.mi.dy = (oRect.top*65535)/nCYFullScreen;
for(nXPos = oRect.left;nXPos < oRect.right; nXPos += nSkipPixels)
{
//normalize
input.mi.dx = (nXPos*65535)/nCXFullScreen;
SendInput(1,&input,sizeof(INPUT));
Sleep(1);
}
}
nNumRetries--;
}
//restore the cursor now
input.mi.dx = (point.x*65535)/nCXFullScreen;
input.mi.dy = (point.y*65535)/nCYFullScreen;
SendInput(1,&input,sizeof(INPUT));
}
}