unit TestSizeHook;
interface
procedure StartProportionalSizing;
stdcall;
forward;
procedure StopProportionalSizing;
stdcall;
forward;
implementation
uses Windows, Forms, Dialogs, Messages, SysUtils, Math, Classes, Controls,
Types;
var
glbSizeHook: cardinal = 0;
glbRatio: single;
glbHookRunning: boolean = FALSE;
glbWindowRect: TRect;
function Hook(code: Integer; W: wParam; L: lParam): LResult;
stdcall;
type
pCWPStruct = ^CWPSTRUCT;
var
currentCWP: CWPSTRUCT;
windowRect: TRect;
PWindowRect: PRect;
newSize: integer;
begin
if (code >= HC_ACTION)
then begin
currentCWP := pCWPStruct(L)^;
case currentCWP.
message of
WM_ENTERSIZEMOVE:
begin
if GetWindowRect(currentCWP.hwnd, windowRect)
then
glbRatio := (windowRect.Right - windowRect.Left) / (windowRect.Bottom - windowRect.Top);
end;
WM_SIZING:
begin
if not glbHookRunning
then begin
PWindowRect := PRect(currentCWP.LParam);
glbWindowRect := PWindowRect^;
case currentCWP.WParam
of
WMSZ_BOTTOM, WMSZ_TOP,
WMSZ_TOPLEFT, WMSZ_TOPRIGHT, WMSZ_BOTTOMLEFT, WMSZ_BOTTOMRIGHT:
begin
newSize := trunc(SimpleRoundTo((PWindowRect.Bottom - PWindowRect.Top + 1) * glbRatio, 0));
PWindowRect.Right := PWindowRect.Left + newSize;
end;
WMSZ_LEFT, WMSZ_RIGHT:
begin
newSize := trunc(SimpleRoundTo((PWindowRect.Right - PWindowRect.Left + 1) / glbRatio, 0));
PWindowRect.Bottom := PWindowRect.Top + newSize;
end;
end;
MoveWindow(currentCWP.hwnd,
PWindowRect.Left,
PWindowRect.Top,
PWindowRect.Right - PWindowRect.Left,
PWindowRect.Bottom - PWindowRect.Top,
TRUE);
glbHookRunning := TRUE;
end;
end;
WM_SIZE:
begin
if not glbHookRunning
then begin
newSize := WORD(glbWindowRect.Bottom - glbWindowRect.Top)
shl (SizeOf(WORD) * 8) + WORD(glbWindowRect.Right - glbWindowRect.Left);
SendMessage(currentCWP.hwnd, WM_SIZE, w, newSize);
glbHookRunning := TRUE;
end;
end;
end;
glbHookRunning := FALSE;
end;
result := CallNextHookEx(glbSizeHook, code, w, l);
end;
procedure StartProportionalSizing;
begin
if glbSizeHook = 0
then
glbSizeHook := SetWindowsHookEx(WH_CALLWNDPROC, @Hook, 0, GetCurrentThreadID());
end;
procedure StopProportionalSizing;
begin
if glbSizeHook <> 0
then
UnhookWindowsHookEx(glbSizeHook);
end;
end.