[456] | 1 | unit uFormMonitor;
|
---|
| 2 |
|
---|
| 3 | interface
|
---|
| 4 |
|
---|
| 5 | uses
|
---|
| 6 | SysUtils, Forms, Classes, Windows, Messages, ExtCtrls, Contnrs, DateUtils;
|
---|
| 7 |
|
---|
| 8 | procedure SetFormMonitoring(activate: boolean);
|
---|
| 9 |
|
---|
| 10 | procedure MarkFormAsStayOnTop(Form: TForm; IsStayOnTop: Boolean);
|
---|
| 11 |
|
---|
| 12 | // Some forms have display tasks when first displayed that are messed up by the
|
---|
| 13 | // form monitor - such as making a combo box automatically drop down. These forms
|
---|
| 14 | // should call FormMonitorBringToFrontEvent, which will be called when the
|
---|
| 15 | // form monitor calls the form's BringToFront method. The Seconds parameter is the
|
---|
| 16 | // amount of time that must transpire before the form monitor will call
|
---|
| 17 | // BringToFront again, unless another form has received focus since the event was called.
|
---|
| 18 |
|
---|
| 19 | procedure FormMonitorBringToFrontEvent(Form: TForm; AEvent: TNotifyEvent; Seconds: integer = 3);
|
---|
| 20 |
|
---|
| 21 | implementation
|
---|
| 22 |
|
---|
[830] | 23 | const
|
---|
| 24 | TIMER_INTERVAL = 8;
|
---|
| 25 | TIMER_CHECKS_BEFORE_TIMEOUT = 1000 div TIMER_INTERVAL;
|
---|
| 26 |
|
---|
[456] | 27 | type
|
---|
| 28 | TFormMonitor = class
|
---|
| 29 | private
|
---|
| 30 | FOldActiveFormChangeEvent: TNotifyEvent;
|
---|
| 31 | FOldActivateEvent: TNotifyEvent;
|
---|
| 32 | FOldRestore: TNotifyEvent;
|
---|
| 33 | FModifyingZOrder: boolean;
|
---|
| 34 | FModifyPending: boolean;
|
---|
| 35 | FActiveForm: TForm;
|
---|
| 36 | FZOrderHandles: TList;
|
---|
| 37 | FLastModal: boolean;
|
---|
| 38 | fTopOnList: TList;
|
---|
| 39 | fTopOffList: TList;
|
---|
| 40 | fTimer: TTimer;
|
---|
| 41 | FTimerCount: integer;
|
---|
| 42 | FMenuPending: boolean;
|
---|
| 43 | FWindowsHook: HHOOK;
|
---|
| 44 | FRunning: boolean;
|
---|
| 45 | FFormEvents: TObjectList;
|
---|
| 46 | FLastActiveFormHandle: HWND;
|
---|
| 47 | procedure ManageForms;
|
---|
| 48 | function FormValid(form: TForm): boolean;
|
---|
| 49 | function HandleValid(handle: HWND): boolean;
|
---|
| 50 | procedure MoveOnTop(Handle: HWND);
|
---|
| 51 | procedure MoveOffTop(Handle: HWND);
|
---|
| 52 | procedure Normalize(Handle: HWND; Yes: boolean);
|
---|
| 53 | procedure NormalizeReset;
|
---|
| 54 | function IsNormalized(Handle: HWND): boolean;
|
---|
| 55 | function GetActiveFormHandle: HWND;
|
---|
| 56 | procedure StartZOrdering;
|
---|
| 57 | function SystemRunning: boolean;
|
---|
| 58 | function ModalDelphiForm: boolean;
|
---|
| 59 | function IsTopMost(Handle: HWND): boolean;
|
---|
| 60 | public
|
---|
| 61 | procedure Start;
|
---|
| 62 | procedure Stop;
|
---|
| 63 | procedure Timer(Sender: TObject);
|
---|
| 64 | procedure Activate(Sender: TObject);
|
---|
| 65 | procedure ActiveFormChange(Sender: TObject);
|
---|
| 66 | procedure Restore(Sender: TObject);
|
---|
| 67 | end;
|
---|
| 68 |
|
---|
| 69 | TFormEvent = class(TObject)
|
---|
| 70 | private
|
---|
| 71 | FForm: TForm;
|
---|
| 72 | FEvent: TNotifyEvent;
|
---|
| 73 | FSeconds: integer;
|
---|
| 74 | FTimeStamp: TDateTime;
|
---|
| 75 | end;
|
---|
| 76 |
|
---|
| 77 | var
|
---|
| 78 | FormMonitor: TFormMonitor = nil;
|
---|
| 79 |
|
---|
| 80 | type
|
---|
| 81 | HDisableGhostProc = procedure(); stdcall;
|
---|
| 82 |
|
---|
| 83 | const
|
---|
| 84 | NORMALIZED = $00000001;
|
---|
| 85 | UN_NORMALIZED = $FFFFFFFE;
|
---|
| 86 | STAY_ON_TOP = $00000002;
|
---|
| 87 | NORMAL_FORM = $FFFFFFFD;
|
---|
| 88 |
|
---|
| 89 |
|
---|
| 90 | procedure DisableGhosting;
|
---|
| 91 | const
|
---|
| 92 | DisableProc = 'DisableProcessWindowsGhosting';
|
---|
| 93 | UserDLL = 'user32.dll';
|
---|
| 94 |
|
---|
| 95 | var
|
---|
| 96 | DisableGhostProc: HDisableGhostProc;
|
---|
| 97 | User32Handle: THandle;
|
---|
| 98 |
|
---|
| 99 | begin
|
---|
| 100 | User32Handle := LoadLibrary(PChar(UserDLL));
|
---|
| 101 | try
|
---|
| 102 | if User32Handle <= HINSTANCE_ERROR then
|
---|
| 103 | User32Handle := 0
|
---|
| 104 | else
|
---|
| 105 | begin
|
---|
| 106 | DisableGhostProc := GetProcAddress(User32Handle, PChar(DisableProc));
|
---|
| 107 | if(assigned(DisableGhostProc)) then
|
---|
| 108 | begin
|
---|
| 109 | DisableGhostProc;
|
---|
| 110 | end;
|
---|
| 111 | end;
|
---|
| 112 | finally
|
---|
| 113 | if(User32Handle <> 0) then
|
---|
| 114 | FreeLibrary(User32Handle);
|
---|
| 115 | end;
|
---|
| 116 | end;
|
---|
| 117 |
|
---|
| 118 | procedure SetFormMonitoring(activate: boolean);
|
---|
| 119 | var
|
---|
| 120 | running: boolean;
|
---|
| 121 | begin
|
---|
| 122 | running := assigned(FormMonitor);
|
---|
| 123 | if(activate <> running) then
|
---|
| 124 | begin
|
---|
| 125 | if(running) then
|
---|
| 126 | begin
|
---|
| 127 | FormMonitor.Stop;
|
---|
| 128 | FormMonitor.Free;
|
---|
| 129 | FormMonitor := nil;
|
---|
| 130 | end
|
---|
| 131 | else
|
---|
| 132 | begin
|
---|
| 133 | FormMonitor := TFormMonitor.Create;
|
---|
| 134 | FormMonitor.Start;
|
---|
| 135 | end;
|
---|
| 136 | end;
|
---|
| 137 | end;
|
---|
| 138 |
|
---|
| 139 | procedure MarkFormAsStayOnTop(Form: TForm; IsStayOnTop: Boolean);
|
---|
| 140 | var
|
---|
| 141 | Data: Longint;
|
---|
| 142 | begin
|
---|
| 143 | Data := GetWindowLong(Form.Handle, GWL_USERDATA);
|
---|
| 144 | if(IsStayOnTop) then
|
---|
| 145 | begin
|
---|
| 146 | Data := Data or STAY_ON_TOP;
|
---|
| 147 | SetWindowPos(Form.Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);
|
---|
| 148 | end
|
---|
| 149 | else
|
---|
| 150 | begin
|
---|
| 151 | Data := Data and NORMAL_FORM;
|
---|
| 152 | SetWindowPos(Form.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE + SWP_NOSIZE);
|
---|
| 153 | end;
|
---|
| 154 | SetWindowLong(Form.Handle, GWL_USERDATA, Data);
|
---|
| 155 | end;
|
---|
| 156 |
|
---|
| 157 | function FindFormEventIndex(Form: TForm): integer;
|
---|
| 158 | var
|
---|
| 159 | i: integer;
|
---|
| 160 | event: TFormEvent;
|
---|
| 161 | begin
|
---|
| 162 | Result := -1;
|
---|
| 163 | for i := 0 to FormMonitor.FFormEvents.Count-1 do
|
---|
| 164 | begin
|
---|
| 165 | event := TFormEvent(FormMonitor.FFormEvents[i]);
|
---|
| 166 | if(event.FForm = Form) then
|
---|
| 167 | begin
|
---|
| 168 | Result := i;
|
---|
| 169 | exit;
|
---|
| 170 | end;
|
---|
| 171 | end;
|
---|
| 172 | end;
|
---|
| 173 |
|
---|
| 174 | function FindFormEvent(Form: TForm): TFormEvent;
|
---|
| 175 | var
|
---|
| 176 | idx: integer;
|
---|
| 177 | begin
|
---|
| 178 | idx := FindFormEventIndex(Form);
|
---|
| 179 | if(idx < 0) then
|
---|
| 180 | Result := nil
|
---|
| 181 | else
|
---|
| 182 | Result := TFormEvent(FormMonitor.FFormEvents[idx]);
|
---|
| 183 | end;
|
---|
| 184 |
|
---|
| 185 | procedure FormMonitorBringToFrontEvent(Form: TForm; AEvent: TNotifyEvent; Seconds: integer);
|
---|
| 186 | var
|
---|
| 187 | event: TFormEvent;
|
---|
| 188 | idx: integer;
|
---|
| 189 | begin
|
---|
| 190 | event := FindFormEvent(Form);
|
---|
| 191 | if(assigned(AEvent)) then
|
---|
| 192 | begin
|
---|
| 193 | if(event = nil) then
|
---|
| 194 | begin
|
---|
| 195 | event := TFormEvent.Create;
|
---|
| 196 | event.FForm := Form;
|
---|
| 197 | event.FTimeStamp := 0;
|
---|
| 198 | FormMonitor.FFormEvents.Add(event);
|
---|
| 199 | end;
|
---|
| 200 | event.FEvent := AEvent;
|
---|
| 201 | event.FSeconds := Seconds;
|
---|
| 202 | end
|
---|
| 203 | else
|
---|
| 204 | if(event <> nil) then
|
---|
| 205 | begin
|
---|
| 206 | idx := FindFormEventIndex(Form);
|
---|
| 207 | FormMonitor.FFormEvents.Delete(idx);
|
---|
| 208 | // event.Free; - TObjectList frees object automatically
|
---|
| 209 | end;
|
---|
| 210 | end;
|
---|
| 211 |
|
---|
| 212 | function IsFormStayOnTop(form: TForm): boolean;
|
---|
| 213 | begin
|
---|
| 214 | Result := (form.FormStyle = fsStayOnTop);
|
---|
| 215 | if(not Result) then
|
---|
| 216 | Result := ((GetWindowLong(Form.Handle, GWL_USERDATA) and STAY_ON_TOP) <> 0);
|
---|
| 217 | end;
|
---|
| 218 |
|
---|
| 219 | { TFormMonitor }
|
---|
| 220 |
|
---|
| 221 | procedure TFormMonitor.Activate(Sender: TObject);
|
---|
| 222 | begin
|
---|
| 223 | if(Assigned(FOldActivateEvent)) then
|
---|
| 224 | FOldActivateEvent(Sender);
|
---|
| 225 | NormalizeReset;
|
---|
| 226 | StartZOrdering;
|
---|
| 227 | end;
|
---|
| 228 |
|
---|
| 229 | procedure TFormMonitor.ActiveFormChange(Sender: TObject);
|
---|
| 230 | begin
|
---|
| 231 | if(Assigned(FOldActiveFormChangeEvent)) then
|
---|
| 232 | FOldActiveFormChangeEvent(Sender);
|
---|
| 233 | StartZOrdering;
|
---|
| 234 | end;
|
---|
| 235 |
|
---|
| 236 | procedure TFormMonitor.Restore(Sender: TObject);
|
---|
| 237 | begin
|
---|
| 238 | if(Assigned(FOldRestore)) then
|
---|
| 239 | FOldRestore(Sender);
|
---|
| 240 | NormalizeReset;
|
---|
| 241 | StartZOrdering;
|
---|
| 242 | end;
|
---|
| 243 |
|
---|
| 244 | function TFormMonitor.FormValid(form: TForm): boolean;
|
---|
| 245 | begin
|
---|
| 246 | Result := assigned(form);
|
---|
| 247 | if Result then
|
---|
| 248 | Result := (form.Parent = nil) and (form.ParentWindow = 0) and form.Visible and (form.Handle <> 0);
|
---|
| 249 | end;
|
---|
| 250 |
|
---|
| 251 | function TFormMonitor.HandleValid(handle: HWND): boolean;
|
---|
| 252 | begin
|
---|
| 253 | Result := (handle <> 0);
|
---|
| 254 | if(Result) then
|
---|
| 255 | Result := IsWindow(handle) and IsWindowVisible(handle) and isWindowEnabled(handle);
|
---|
| 256 | end;
|
---|
| 257 |
|
---|
| 258 | function FindWindowZOrder(Window: HWnd; Data: Longint): Bool; stdcall;
|
---|
| 259 | begin
|
---|
| 260 | if(IsWindow(Window) and IsWindowVisible(Window)) then
|
---|
| 261 | FormMonitor.FZOrderHandles.Add(Pointer(Window));
|
---|
| 262 | Result := True;
|
---|
| 263 | end;
|
---|
| 264 |
|
---|
| 265 | procedure TFormMonitor.ManageForms;
|
---|
| 266 | var
|
---|
| 267 | i, j: integer;
|
---|
| 268 | form: TForm;
|
---|
| 269 | formHandle, activeHandle: HWND;
|
---|
| 270 | modal, doCall: boolean;
|
---|
| 271 | event: TFormEvent;
|
---|
| 272 |
|
---|
| 273 | begin
|
---|
| 274 | if(FModifyingZOrder) then exit;
|
---|
| 275 | if(not SystemRunning) then exit;
|
---|
| 276 | FModifyingZOrder := TRUE;
|
---|
| 277 | try
|
---|
| 278 | activeHandle := GetActiveFormHandle;
|
---|
| 279 | modal := ModalDelphiForm;
|
---|
| 280 | FZOrderHandles.Clear;
|
---|
| 281 | fTopOnList.Clear;
|
---|
| 282 | fTopOffList.Clear;
|
---|
| 283 |
|
---|
| 284 | EnumThreadWindows(GetCurrentThreadID, @FindWindowZOrder, 0);
|
---|
| 285 | for i := 0 to FZOrderHandles.Count-1 do
|
---|
| 286 | begin
|
---|
| 287 | formHandle := HWND(FZOrderHandles[i]);
|
---|
| 288 | for j := 0 to Screen.FormCount-1 do
|
---|
| 289 | begin
|
---|
| 290 | form := Screen.Forms[j];
|
---|
| 291 | if(form.Handle = formHandle) then
|
---|
| 292 | begin
|
---|
| 293 | if formValid(form) and (form.Handle <> activeHandle) and IsFormStayOnTop(form) then
|
---|
| 294 | begin
|
---|
| 295 | if(modal and (not IsWindowEnabled(form.Handle))) then
|
---|
| 296 | fTopOffList.Add(Pointer(form.Handle))
|
---|
| 297 | else
|
---|
| 298 | fTopOnList.Add(Pointer(form.Handle));
|
---|
| 299 | end;
|
---|
| 300 | break;
|
---|
| 301 | end;
|
---|
| 302 | end;
|
---|
| 303 | end;
|
---|
| 304 | for i := fTopOffList.Count-1 downto 0 do
|
---|
| 305 | MoveOffTop(HWND(fTopOffList[i]));
|
---|
| 306 | for i := fTopOnList.Count-1 downto 0 do
|
---|
| 307 | MoveOnTop(HWND(fTopOnList[i]));
|
---|
| 308 |
|
---|
| 309 | if(activeHandle <> 0) then
|
---|
| 310 | begin
|
---|
| 311 | if(assigned(FActiveForm)) then
|
---|
| 312 | begin
|
---|
| 313 | event := FindFormEvent(FActiveForm);
|
---|
| 314 | doCall := (event = nil);
|
---|
| 315 | if(not doCall) then
|
---|
| 316 | doCall := (activeHandle <> FLastActiveFormHandle);
|
---|
| 317 | if(not doCall) then
|
---|
| 318 | doCall := SecondsBetween(Now, event.FTimeStamp) > event.FSeconds;
|
---|
| 319 | if(doCall) then
|
---|
| 320 | begin
|
---|
| 321 | if IsFormStayOnTop(FActiveForm) then
|
---|
| 322 | begin
|
---|
| 323 | SetWindowPos(activeHandle, HWND_TOPMOST, 0, 0, 0, 0,
|
---|
| 324 | SWP_NOMOVE or SWP_NOSIZE);
|
---|
| 325 | Normalize(activeHandle, FALSE);
|
---|
| 326 | end;
|
---|
| 327 | FActiveForm.BringToFront;
|
---|
| 328 | if(event <> nil) then
|
---|
| 329 | begin
|
---|
| 330 | if(FormValid(event.FForm)) then
|
---|
| 331 | begin
|
---|
| 332 | event.FEvent(FActiveForm);
|
---|
| 333 | event.FTimeStamp := now;
|
---|
| 334 | end;
|
---|
| 335 | end;
|
---|
| 336 | end;
|
---|
| 337 | end
|
---|
| 338 | else
|
---|
| 339 | begin
|
---|
| 340 | if(activeHandle <> 0) then
|
---|
| 341 | begin
|
---|
| 342 | SetFocus(activeHandle);
|
---|
| 343 | BringWindowToTop(activeHandle);
|
---|
| 344 | if(IsTopMost(activeHandle)) then
|
---|
| 345 | SetWindowPos(activeHandle, HWND_TOPMOST, 0, 0, 0, 0,
|
---|
| 346 | SWP_NOMOVE or SWP_NOSIZE);
|
---|
| 347 | end;
|
---|
| 348 | end;
|
---|
| 349 | end;
|
---|
| 350 | FLastActiveFormHandle := activeHandle;
|
---|
| 351 | finally
|
---|
| 352 | FModifyingZOrder := FALSE;
|
---|
| 353 | end;
|
---|
| 354 | end;
|
---|
| 355 |
|
---|
| 356 | function CallWndHook(Code: Integer; WParam: wParam; Msg: PCWPStruct): Longint; stdcall;
|
---|
| 357 | begin
|
---|
| 358 | case Msg.message of
|
---|
| 359 | WM_INITMENU, WM_INITMENUPOPUP, WM_ENTERMENULOOP:
|
---|
| 360 | FormMonitor.FMenuPending := TRUE;
|
---|
| 361 | WM_MENUSELECT, WM_EXITMENULOOP:
|
---|
| 362 | FormMonitor.FMenuPending := FALSE;
|
---|
| 363 | end;
|
---|
| 364 | Result := CallNextHookEx(FormMonitor.FWindowsHook, Code, WParam, Longint(Msg));
|
---|
| 365 | end;
|
---|
| 366 |
|
---|
| 367 | procedure TFormMonitor.Start;
|
---|
| 368 | begin
|
---|
| 369 | if(FRunning) then exit;
|
---|
| 370 | FRunning := TRUE;
|
---|
| 371 | FTimer := TTimer.Create(Application);
|
---|
| 372 | fTimer.Enabled := FALSE;
|
---|
| 373 | FTimer.OnTimer := Timer;
|
---|
[830] | 374 | FTimer.Interval := TIMER_INTERVAL;
|
---|
[456] | 375 | FMenuPending := FALSE;
|
---|
| 376 | FLastActiveFormHandle := 0;
|
---|
| 377 |
|
---|
| 378 | FZOrderHandles := TList.Create;
|
---|
| 379 | fTopOnList := TList.Create;
|
---|
| 380 | fTopOffList := TList.Create;
|
---|
| 381 | FFormEvents := TObjectList.Create;
|
---|
| 382 | FModifyingZOrder := false;
|
---|
| 383 | FLastModal := false;
|
---|
| 384 | FOldActiveFormChangeEvent := Screen.OnActiveFormChange;
|
---|
| 385 | Screen.OnActiveFormChange := ActiveFormChange;
|
---|
| 386 | FOldActivateEvent := Application.OnActivate;
|
---|
| 387 | Application.OnActivate := Activate;
|
---|
| 388 | FOldRestore := Application.OnRestore;
|
---|
| 389 | Application.OnRestore := Restore;
|
---|
| 390 | FWindowsHook := SetWindowsHookEx(WH_CALLWNDPROC, @CallWndHook, 0, GetCurrentThreadID)
|
---|
| 391 | end;
|
---|
| 392 |
|
---|
| 393 | procedure TFormMonitor.Stop;
|
---|
| 394 | begin
|
---|
| 395 | if(not FRunning) then exit;
|
---|
| 396 | FRunning := FALSE;
|
---|
| 397 | if FWindowsHook <> 0 then
|
---|
| 398 | begin
|
---|
| 399 | UnHookWindowsHookEx(FWindowsHook);
|
---|
| 400 | FWindowsHook := 0;
|
---|
| 401 | end;
|
---|
| 402 | Screen.OnActiveFormChange := FOldActiveFormChangeEvent;
|
---|
| 403 | Application.OnActivate := FOldActivateEvent;
|
---|
| 404 | Application.OnRestore := FOldRestore;
|
---|
| 405 |
|
---|
| 406 | FZOrderHandles.Free;
|
---|
| 407 | fTopOnList.Free;
|
---|
| 408 | fTopOffList.Free;
|
---|
| 409 | FFormEvents.Free;
|
---|
| 410 | fTimer.Enabled := FALSE;
|
---|
| 411 | fTimer.Free;
|
---|
| 412 | end;
|
---|
| 413 |
|
---|
| 414 | procedure TFormMonitor.MoveOffTop(Handle: HWND);
|
---|
| 415 | begin
|
---|
| 416 | if(not IsNormalized(Handle)) then
|
---|
| 417 | begin
|
---|
| 418 | SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0,
|
---|
| 419 | SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOOWNERZORDER);
|
---|
| 420 | Normalize(Handle, TRUE);
|
---|
| 421 | end;
|
---|
| 422 | end;
|
---|
| 423 |
|
---|
| 424 | procedure TFormMonitor.MoveOnTop(Handle: HWND);
|
---|
| 425 | begin
|
---|
| 426 | if(isNormalized(Handle)) then
|
---|
| 427 | begin
|
---|
| 428 | SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
|
---|
| 429 | SWP_NOMOVE or SWP_NOSIZE or SWP_NOACTIVATE or SWP_NOOWNERZORDER);
|
---|
| 430 | Normalize(Handle, FALSE);
|
---|
| 431 | end;
|
---|
| 432 | end;
|
---|
| 433 |
|
---|
| 434 | procedure TFormMonitor.Normalize(Handle: HWND; Yes: boolean);
|
---|
| 435 | var
|
---|
| 436 | Data: Longint;
|
---|
| 437 | begin
|
---|
| 438 | Data := GetWindowLong(Handle, GWL_USERDATA);
|
---|
| 439 | if(yes) then
|
---|
| 440 | Data := Data or NORMALIZED
|
---|
| 441 | else
|
---|
| 442 | Data := Data and UN_NORMALIZED;
|
---|
| 443 | SetWindowLong(Handle, GWL_USERDATA, Data);
|
---|
| 444 | end;
|
---|
| 445 |
|
---|
| 446 | function TFormMonitor.IsNormalized(Handle: HWND): boolean;
|
---|
| 447 | begin
|
---|
| 448 | Result := ((GetWindowLong(Handle, GWL_USERDATA) and NORMALIZED) <> 0);
|
---|
| 449 | end;
|
---|
| 450 |
|
---|
| 451 | function TFormMonitor.IsTopMost(Handle: HWND): boolean;
|
---|
| 452 | begin
|
---|
| 453 | Result := ((GetWindowLong(Handle, GWL_EXSTYLE) and WS_EX_TOPMOST) <> 0);
|
---|
| 454 | end;
|
---|
| 455 |
|
---|
| 456 | function FindWindows(Window: HWnd; Data: Longint): Bool; stdcall;
|
---|
| 457 | begin
|
---|
| 458 | FormMonitor.Normalize(Window, FALSE);
|
---|
| 459 | Result := True;
|
---|
| 460 | end;
|
---|
| 461 |
|
---|
| 462 | procedure TFormMonitor.NormalizeReset;
|
---|
| 463 | begin
|
---|
| 464 | EnumThreadWindows(GetCurrentThreadID, @FindWindows, 0);
|
---|
| 465 | end;
|
---|
| 466 |
|
---|
| 467 | var
|
---|
| 468 | uActiveWindowHandle: HWND;
|
---|
| 469 | uActiveWindowCount: integer;
|
---|
| 470 |
|
---|
| 471 | function IsHandleOK(Handle: HWND): boolean;
|
---|
| 472 | var
|
---|
| 473 | i: integer;
|
---|
| 474 |
|
---|
| 475 | begin
|
---|
| 476 | Result := FALSE;
|
---|
| 477 | if(not formMonitor.HandleValid(Handle)) or (Handle = Application.Handle) then exit;
|
---|
| 478 | for i := 0 to Screen.FormCount-1 do
|
---|
| 479 | begin
|
---|
| 480 | if(Handle = Screen.Forms[i].Handle) then exit;
|
---|
| 481 | end;
|
---|
| 482 | Result := TRUE;
|
---|
| 483 | end;
|
---|
| 484 |
|
---|
| 485 | function FindActiveWindow(Window: HWnd; Data: Longint): Bool; stdcall;
|
---|
| 486 | begin
|
---|
| 487 | Result := True;
|
---|
| 488 | if(IsHandleOK(Window)) then
|
---|
| 489 | begin
|
---|
| 490 | inc(uActiveWindowCount);
|
---|
| 491 | if(uActiveWindowCount = 1) then
|
---|
| 492 | uActiveWindowHandle := Window
|
---|
| 493 | else
|
---|
| 494 | if(uActiveWindowCount > 1) then
|
---|
| 495 | Result := false;
|
---|
| 496 | end;
|
---|
| 497 | end;
|
---|
| 498 |
|
---|
| 499 | function TFormMonitor.GetActiveFormHandle: HWND;
|
---|
| 500 | var
|
---|
| 501 | i: integer;
|
---|
| 502 | form: TForm;
|
---|
| 503 |
|
---|
| 504 | begin
|
---|
| 505 | FActiveForm := Screen.ActiveForm;
|
---|
| 506 | if(assigned(FActiveForm)) then
|
---|
| 507 | Result := FActiveForm.Handle
|
---|
| 508 | else
|
---|
| 509 | Result := 0;
|
---|
| 510 | if(FormValid(FActiveForm) and IsWindowEnabled(FActiveForm.Handle)) then
|
---|
| 511 | exit;
|
---|
| 512 | for i := 0 to Screen.FormCount-1 do
|
---|
| 513 | begin
|
---|
| 514 | form := Screen.Forms[i];
|
---|
| 515 | if(form.Handle = Result) then
|
---|
| 516 | begin
|
---|
| 517 | if FormValid(form) and IsWindowEnabled(form.Handle) then
|
---|
| 518 | begin
|
---|
| 519 | FActiveForm := form;
|
---|
| 520 | Result := form.Handle;
|
---|
| 521 | exit;
|
---|
| 522 | end;
|
---|
| 523 | end;
|
---|
| 524 | end;
|
---|
| 525 | FActiveForm := nil;
|
---|
| 526 | Result := GetActiveWindow;
|
---|
| 527 | if(IsHandleOK(Result)) then exit;
|
---|
| 528 | uActiveWindowHandle := 0;
|
---|
| 529 | uActiveWindowCount := 0;
|
---|
| 530 | EnumThreadWindows(GetCurrentThreadID, @FindActiveWindow, 0);
|
---|
| 531 | if(uActiveWindowCount = 1) then
|
---|
| 532 | begin
|
---|
| 533 | Result := uActiveWindowHandle;
|
---|
| 534 | end;
|
---|
| 535 | end;
|
---|
| 536 |
|
---|
| 537 |
|
---|
| 538 | procedure TFormMonitor.StartZOrdering;
|
---|
| 539 | begin
|
---|
| 540 | if(FModifyPending) then exit;
|
---|
| 541 | if(SystemRunning) then
|
---|
| 542 | begin
|
---|
| 543 | FModifyPending := TRUE;
|
---|
| 544 | FTimerCount := 0;
|
---|
| 545 | FTimer.Enabled := TRUE;
|
---|
| 546 | end;
|
---|
| 547 | end;
|
---|
| 548 |
|
---|
| 549 | function TFormMonitor.SystemRunning: boolean;
|
---|
| 550 | begin
|
---|
| 551 | Result := assigned(Application.MainForm) and
|
---|
| 552 | (Application.MainForm.Handle <> 0) and
|
---|
| 553 | IsWindowVisible(Application.MainForm.Handle);
|
---|
| 554 | end;
|
---|
| 555 |
|
---|
| 556 |
|
---|
| 557 | function TFormMonitor.ModalDelphiForm: boolean;
|
---|
| 558 | var
|
---|
| 559 | i: integer;
|
---|
| 560 | form: TForm;
|
---|
| 561 | begin
|
---|
| 562 | for i := 0 to Screen.FormCount-1 do
|
---|
| 563 | begin
|
---|
| 564 | form := screen.Forms[i];
|
---|
| 565 | if(FormValid(form) and (fsModal in form.FormState)) then
|
---|
| 566 | begin
|
---|
| 567 | Result := TRUE;
|
---|
| 568 | exit;
|
---|
| 569 | end;
|
---|
| 570 | end;
|
---|
| 571 | Result := FALSE;
|
---|
| 572 | end;
|
---|
| 573 |
|
---|
| 574 | procedure TFormMonitor.Timer(Sender: TObject);
|
---|
| 575 | var
|
---|
| 576 | NoMenu: boolean;
|
---|
| 577 | begin
|
---|
| 578 | inc(FTimerCount);
|
---|
[830] | 579 | if(FTimerCount > TIMER_CHECKS_BEFORE_TIMEOUT) then
|
---|
[456] | 580 | begin
|
---|
| 581 | FTimer.Enabled := FALSE;
|
---|
| 582 | FMenuPending := FALSE;
|
---|
| 583 | FModifyPending := FALSE;
|
---|
| 584 | exit;
|
---|
| 585 | end;
|
---|
| 586 | if(FTimerCount <> 1) then exit;
|
---|
| 587 | FTimer.Enabled := FALSE;
|
---|
| 588 | NoMenu := not FMenuPending;
|
---|
| 589 | FMenuPending := FALSE;
|
---|
| 590 | if(NoMenu and SystemRunning) then
|
---|
| 591 | ManageForms;
|
---|
| 592 | FModifyPending := FALSE;
|
---|
| 593 | end;
|
---|
| 594 |
|
---|
| 595 | initialization
|
---|
| 596 | DisableGhosting;
|
---|
| 597 |
|
---|
| 598 | finalization
|
---|
| 599 |
|
---|
| 600 | end.
|
---|