6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms;
8 function MsgBox(const hWnd: HWND; const Text, Caption: string; Flags: Longint = MB_OK): Integer;
9 function MsgMoveProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
15 function MsgBox(const hWnd: HWND; const Text, Caption: string; Flags: Longint = MB_OK): Integer;
17 hhk := SetWindowsHookEx(WH_CBT, @MsgMoveProc, 0, GetCurrentThreadId());
18 Result := Windows.MessageBox(hwnd, PChar(Text), PChar(Caption), Flags);
20 UnhookWindowsHookEx(hhk);
23 function MsgMoveProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
24 function GetW(ARect: TRect): Integer;
26 Result := ARect.Right - ARect.Left;
28 function GetH(ARect: TRect): Integer;
30 Result := ARect.Bottom - ARect.Top;
32 function MoveRect(ARect: TRect; X, Y: Integer): TRect;
38 Result := Rect(X, Y, X + W, Y + H);
46 if nCode = HCBT_ACTIVATE then begin
48 hParent := GetParent(wParam);
49 if (hParent <> 0) and (hMsgBox <> 0) and
50 (GetWindowRect(hParent, ParentR)) and
51 (GetWindowRect(hMsgBox, MsgBoxR)) then begin
52 MsgBoxR := MoveRect(MsgBoxR,
53 ParentR.Left + ((GetW(ParentR) - GetW(MsgBoxR)) div 2),
54 ParentR.Top + ((GetH(ParentR) - GetH(MsgBoxR)) div 2));
55 if MsgBoxR.Left < 0 then
56 MsgBoxR := MoveRect(MsgBoxR, 0, MsgBoxR.Top);
57 if MsgBoxR.Top < 0 then
58 MsgBoxR := MoveRect(MsgBoxR, MsgBoxR.Left, 0);
59 if MsgBoxR.Left + GetW(MsgBoxR) > Screen.WorkAreaWidth then
60 MsgBoxR := MoveRect(MsgBoxR, Screen.WorkAreaWidth - GetW(MsgBoxR), MsgBoxR.Top);
61 if MsgBoxR.Top + GetH(MsgBoxR) > Screen.WorkAreaHeight then
62 MsgBoxR := MoveRect(MsgBoxR, MsgBoxR.Left, Screen.WorkAreaHeight - GetH(MsgBoxR));
63 MoveWindow(hMsgBox, MsgBoxR.Left, MsgBoxR.Top, GetW(MsgBoxR), GetH(MsgBoxR), False);
65 UnhookWindowsHookEx(hhk);
68 CallNextHookEx(hhk, nCode, wParam, lParam);