Focus in the right window

The scenario is that I have a “main application” and a “helper application”. The “helper application” appears on the keyboard hook, it populates, and then reorients the main application window. The problem is that if the main application pops up a modal dialog when the assistant is active and the assistant refocuses the wrong window, the modal dialog is hidden and the main application looks “frozen”.

Any suggestions on strategies to solve this problem?

+4
source share
2 answers

It seems that the modal form of the "main application" does not belong to the main window of the application, otherwise the modal form will always be above the main form. Thus, it is possible that there is either no MainFormOnTaskbar property for the version of Delphi that was compiled, or it is not set. Then it should be a hidden window of the application to which the windows belong.

You can check if the main application window is disabled when closing the "auxiliary application" form (this will be the case if there is a modal form), and restore the last active pop-up window that belongs to the hidden application window, if it is.

 var Wnd: HWND; // handle to 'main app main form mWnd: HWND; // handle to possible modal form AppWnd: HWND; // handle to hidden Application window begin .. if not IsWindowEnabled(Wnd) then begin // test if there a modal form AppWnd := GetWindowLong(Wnd, GWL_HWNDPARENT); // TApplication window handle mWnd := GetLastActivePopup(AppWnd); // most recently active popup window // restore focus to mWnd end else // restore focus to Wnd 

(Remember to include tests for the result of API functions, of course.)

+1
source

Try Application.Restore or Application.RestoreTopMosts . And when this modal dialog is shown by calling WinAPI, try Application.Normalize(All)TopMosts before.

Now this may be enough, but in my application, which hides the application descriptor from the taskbar, it is not, and I need the following procedure, which is the TApplicationEvents.OnActivate event TApplicationEvents.OnActivate :

 procedure TMainForm.AppEventsActivate(Sender: TObject); var TopWindow: HWND; begin TopWindow := GetLastActivePopup(Application.Handle); if (TopWindow <> 0) and (TopWindow <> Application.Handle) then begin BringToFront; SetForegroundWindow(TopWindow); end; end; 

Clarification: the code in Application.BringToFront almost the same, but it does not guarantee that the main form is displayed in the background either. That is, Application.BringToFront can only show a modal dialog box.

0
source

Source: https://habr.com/ru/post/1386643/


All Articles