M
Kommt sofort :).
hookDLL.cpp
//---------------------------------------------------------------------------
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------
extern "C" __declspec(dllexport) void SetHook(HWND);
extern "C" __declspec(dllexport) void RemoveHook(void);
LRESULT CALLBACK BrowserHookProc(int nCode, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK BrowserMenuHookProc(int nCode, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
HHOOK ghHook = NULL;
HHOOK ghHook2 = NULL;
HINSTANCE ghInst;
//---------------------------------------------------------------------------
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved)
{
ghInst = hinst;
return 1;
}
//---------------------------------------------------------------------------
void SetHook(HWND hWnd)
{
if(!ghHook)
{
ghHook = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)BrowserHookProc, ghInst, 0);
if(!ghHook)
{
MessageBox(NULL,"Hook kann nicht erstellt werden", "FEHLER", MB_OK | MB_ICONERROR);
}
}
else
{
MessageBox(NULL, "Hook ist bereits erstellt", "Browser Window Hook", MB_OK);
}
if(!ghHook2)
{
ghHook2 = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)BrowserMenuHookProc, ghInst, 0);
if(!ghHook2)
{
MessageBox(NULL,"Hook2 kann nicht erstellt werden", "FEHLER", MB_OK | MB_ICONERROR);
}
}
else
{
MessageBox(NULL, "Hook2 ist bereits erstellt", "Browser Window Hook", MB_OK);
}
}
//---------------------------------------------------------------------------
void RemoveHook(void)
{
UnhookWindowsHookEx(ghHook);
UnhookWindowsHookEx(ghHook2);
}
//---------------------------------------------------------------------------
LRESULT CALLBACK BrowserHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode == HC_ACTION)
{
CWPSTRUCT *cwp = (CWPSTRUCT*)lParam;
HWND hWndIE = FindWindow("IEFrame", NULL);
HWND hWndMozilla = FindWindow("MozillaWindowClass", NULL);
if((cwp->message == WM_CREATE) && ((cwp->hwnd == hWndIE) || cwp->hwnd == (hWndMozilla)))
{
HMENU hMenuIE = GetSystemMenu(cwp->hwnd, FALSE);
HMENU ddMenu = CreateMenu();
AppendMenu(ddMenu, MF_STRING, 101, "800 x 600");
AppendMenu(ddMenu, MF_STRING, 102, "1024 x 768");
AppendMenu(ddMenu, MF_STRING, 103, "1200 X 960");
AppendMenu(ddMenu, MF_STRING, 104, "1600 x 1200");
AppendMenu(ddMenu, MF_STRING, 105, "ganzer Bildschirm");
AppendMenu(hMenuIE, MF_SEPARATOR, 0, 0);
AppendMenu(hMenuIE, MF_POPUP, (UINT)ddMenu, "Größe anpassen");
}
}
return CallNextHookEx(ghHook, nCode, wParam, lParam);
}
//---------------------------------------------------------------------------
LRESULT CALLBACK BrowserMenuHookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if(nCode == HC_ACTION)
{
MSG *msg = (MSG*)lParam;
if(msg->message == WM_SYSCOMMAND)
{
RECT wRect;
GetWindowRect(msg->hwnd, &wRect);
switch(LOWORD(msg->wParam))
{
case 101: SetWindowPos(msg->hwnd, HWND_TOP, wRect.left, wRect.top, 800, 600, SWP_SHOWWINDOW);
break;
case 102: SetWindowPos(msg->hwnd, HWND_TOP, wRect.left, wRect.top, 1024, 768, SWP_SHOWWINDOW);
break;
case 103: SetWindowPos(msg->hwnd, HWND_TOP, wRect.left, wRect.top, 1200, 960, SWP_SHOWWINDOW);
break;
case 104: SetWindowPos(msg->hwnd, HWND_TOP, wRect.left, wRect.top, 1600, 1200, SWP_SHOWWINDOW);
break;
case 105: ShowWindow(msg->hwnd, SW_MAXIMIZE);
break;
}
}
}
return CallNextHookEx(ghHook, nCode, wParam, lParam);
}
Diese DLL wird von meinem Hauptprogramm dynamisch geladen und beim Erstellen des Hauptprogrammes werden die Hooks gesetzt. Beim Beenden des Programms werden diese dann wieder entfern. Es können natürlich ohne weiteres noch andere Browser oder Programme hinzugefügt werden.
Was mir noch etwas schwer im Magen liegt ist, das die MSDN über Hooks sagt, das diese recht viel Resourcen schlucken. Aber ich glaube die Lösung mit den Hooks ist noch wesentlich performanter als die mit dem Timer.