codice:
void DoEventsLoop(){
MSG message;
while(PeekMessage(&message, NULL, 0, 0, PM_REMOVE)){
DispatchMessage(&message);
}
}
BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lParam){
if (IsWindowVisible(hwnd) && (hwnd!=shellTray) && IsWindow(hwnd) && (hwnd!=deskhWnd) && (hwnd!=foreWin)){
SetWindowLong(hwnd, GWL_EXSTYLE,GetWindowLong(hwnd, GWL_EXSTYLE) | WS_EX_LAYERED);
m_pSetLayeredWindowAttributes(hwnd, 0, 100, LWA_ALPHA);
}
DoEventsLoop();
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam){
switch(uMsg){
case WM_COMMAND:
if (lParam == WM_LBUTTONUP){
DestroyWindow(hwnd);
PostQuitMessage(0);
}
break;
default:
break;
}
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}
void CreateInvisibleWindow(){
wcx.cbSize = sizeof(wcx);
wcx.lpfnWndProc = WndProc;
wcx.hInstance = myInst;
wcx.hIcon = LoadIcon(myInst, IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL, IDC_ARROW);
wcx.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wcx.lpszClassName = "myWin";
RegisterClassEx(&wcx);
hidWin=CreateWindowEx(0, "myWin", "glassWin",
WS_OVERLAPPEDWINDOW&~WS_MAXIMIZEBOX&~WS_THICKFRAME,
0, 0, 278, 178, 0,0, myInst,0);
ShowWindow(hidWin,SW_HIDE);
UpdateWindow(hidWin);
}
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
myInst = hInstance;
CreateInvisibleWindow();
ShowTrayIcon(hidWin);
HINSTANCE hUser32 = LoadLibrary("user32.dll");
m_pSetLayeredWindowAttributes = (lpfnSetLayeredWindowAttributes)GetProcAddress(hUser32, "SetLayeredWindowAttributes");
FreeLibrary(hUser32);
deskhWnd = GetDesktopWindow();
shellTray = FindWindow("Shell_TrayWnd","");
while(1){
foreWin = GetForegroundWindow();
EnumWindows(MyEnumWindowsProc, 0);
DoEventsLoop();
SetWindowLong(foreWin, GWL_EXSTYLE,
GetWindowLong(foreWin, GWL_EXSTYLE) | WS_EX_LAYERED);
m_pSetLayeredWindowAttributes(foreWin, 0, 255, LWA_ALPHA);
}
return TRUE;
}
In parole povere (e come prevedibile), il ciclo while(1){ } mi "uccide" il processore, portandolo anche al 50% delle propria possibilità.