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