Modal form BringToTop from wm_activate lazarus / 31-LCL ---------------------------------------------------------------------- quantumfoam · 2021-12-23 01:11 My application main form is not standard TForm, but an object created via WinApi functions which runs it own message loop. I can open a secondary, modal form (type TForm, showModal) from my main form, or call e.g. TopenDialog.execute. When I hide both my opened forms with some other applications (Firefox,  Windows explorer etc.) and then activate my application again (clicking on application icon in taskbar) then my main form pops up, but the secondary one is buried under. How do I bring secondary form on top? Does Lazarus keep a list of opened forms? I tried EnumChildWindows, GetForegroundWindow, GetActiveWindow in WM_ACTIVATE of my main window with no success. When main form is TForm, this works. ---------------------------------------------------------------------- balazsszekely · 2021-12-23 05:55 @quantumfoam QuoteMy application main form is not standard TForm, but an object created via WinApi functions which runs it own message loop. I can open a secondary, modal form (type TForm, showModal) from my main form, or call e.g. TopenDialog.execute. When I hide both my opened forms with some other applications (Firefox,  Windows explorer etc.) and then activate my application again (clicking on application icon in taskbar) then my main form pops up, but the secondary one is buried under. Try something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Winapi; {$mode Delphi} uses Windows, Classes, Forms, Dialogs, Controls, Interfaces; var  wClass: TWndClass;  hInst: HWND;  hWindow: HWND;  hButton: HWND;  Msg: TMSG;  ModalForm: TForm; procedure DoOnclick;begin  EnableWindow(hWindow, False);  ModalForm.ShowModal;end; procedure DoFormClose;begin  EnableWindow(hWindow, True);end; procedure DoOnApplicationActivate;begin  ModalForm.BringToFront;end; procedure DoCreate;var  Method: TMethod;begin  Application.Initialize;  Method.Data := nil;  Method.Code := @DoOnApplicationActivate;  Application.OnActivate := TNotifyEvent(Method);   ModalForm := TForm.Create(nil);  ModalForm.Caption := 'My modal form';  Method.Data := nil;  Method.Code := @DoFormClose;  ModalForm.OnClose := TCloseEvent(Method);end; procedure DoDestroy;begin  ModalForm.Free;  Windows.UnRegisterClass('My class', hInst);  ExitProcess(hInst);end; function WindowProc(hWnd, Msg, wParam, lParam: DWord): Longint; stdcall;begin  Result := DefWindowProc(hWnd, Msg, wParam, lParam);  case Msg of     WM_COMMAND: if lparam = hButton then DoOnClick;     WM_CREATE: DoCreate;     WM_DESTROY: DoDestroy;  end;end; begin  hInst := GetModuleHandle(nil);  with wClass do  begin    hIcon := LoadIcon(hInst, 'MAINICON');    lpfnWndProc:= @WindowProc;    hInstance := hInst;    hbrBackground := COLOR_BTNFACE;    lpszClassName := 'My class';  end;  Windows.RegisterClass(wClass);  hWindow := CreateWindow('My class', 'My main window', WS_SYSMENU or WS_VISIBLE or WS_SIZEBOX, 10, 10, 400, 300, 0, 0, hInst, nil);  hButton := CreateWindow('Button', 'Show modal form', WS_CHILD or BS_PUSHLIKE or BS_TEXT or WS_VISIBLE, (400 - 125) div 2, 125, 125, 24, hWindow, 0, hInst, nil);  SetFocus(hButton);  while(GetMessage({%H-}Msg, hWindow, 0, 0))do  begin     TranslateMessage(Msg);     DispatchMessage(Msg);  end;end.  QuoteHow do I bring secondary form on top? Does Lazarus keep a list of opened forms? Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  for I := 0 to Screen.FormCount - 1 do  if (fsModal in Screen.Forms[I].FormState) and (Screen.Forms[I].ClassName = 'TMySpecialModalForm') then    Screen.Forms[I].BringToFront; ---------------------------------------------------------------------- quantumfoam · 2021-12-23 14:18 Quote from: GetMem on December 23, 2021, 06:55:06 am Thank you. This works on any modal TForm. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure DoOnApplicationActivate;  var i:integer;begin  for I := 0 to Screen.FormCount - 1 do    Screen.Forms[I].BringToFront;end;  However,  its not working on TopenDialog. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure DoCreate;var  Method: TMethod;begin  Application.Initialize;  Method.Data := nil;  Method.Code := @DoOnApplicationActivate;  Application.OnActivate := TNotifyEvent(Method);  od := TopenDialog.create(nil);end;   procedure DoOnclick;begin  od.execute;end; ---------------------------------------------------------------------- balazsszekely · 2021-12-23 16:28 @ quantumfoam QuoteHowever,  its not working on TopenDialog.No because OpenDialog is created by the OS. You should get the handle of the dialog when the OnShow events fires, then set the ZOrder, on application activate.