Popup Form to close when focus is lost lazarus / 31-LCL ---------------------------------------------------------------------- daniel_sap · 2025-07-09 10:56 Hi, I created descendant of TForm which implements behavior like Popup Window. But the form is not closing itself after clicking outside of it. I tried with SetCapture and Deactivate but still nothing happens. 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 unit DSPopupForm; {$mode objfpc}{$H+} interface uses  Forms, Classes, LMessages; type   { TDSPopupForm }   TDSPopupForm = class(TForm)  protected    procedure Deactivate; override;    procedure WMLButtonDown(var AMessage: TLMLButtonDown); message LM_LBUTTONDOWN;  procedure DoClose(var CloseAction: TCloseAction); override;  public  constructor Create(TheOwner: TComponent); override;    procedure popup(x, y: Integer);  end; implementation uses  Controls, LCLIntf; { TDSPopupForm } constructor TDSPopupForm.Create(TheOwner: TComponent);begin  inherited Create(TheOwner);  BorderStyle := bsNone;  FormStyle := fsStayOnTop;end; procedure TDSPopupForm.popup(x, y: Integer);begin  Left := X;  Top := Y;  Show;  Activate;  SetCapture(Handle);end; procedure TDSPopupForm.Deactivate;begin  inherited Deactivate;  Close;end; procedure TDSPopupForm.WMLButtonDown(var AMessage: TLMLButtonDown);var  MousePos: TPoint;begin  MousePos := Mouse.CursorPos;  if not PtInRect(BoundsRect, MousePos) then  begin    Close;  end  else  begin    inherited;  end;end; procedure TDSPopupForm.DoClose(var CloseAction: TCloseAction);begin  ReleaseCapture;  inherited;end; end.  May be you can point something I cannot see or suggest some work direction AI suggested to use 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   FormStyle := fsPopup; instead of 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   FormStyle := fsStayOnTop; Which I thought will make the form behave as Popup, but actually fsPopup doesn't exists at all in the enumeration. Lazarus 4.0RC1 (rev t-fixes-4-58-g43d904ed50) FPC 3.2.2 x86_64-win64-win32/win64 ---------------------------------------------------------------------- Martin_fr · 2025-07-09 11:25 "Deactivate" iirc is only if your entire app is "switched away from". Try  (not tested) 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 WMKillFocus(var Msg: TWMKillFocus); message WM_KILLFOCUS;  May need more checks if you have focus-able controls inside that form.... You still want to keep Deactivate, as I am not sure you get a killfocus, if there is a deactivate ---------------------------------------------------------------------- Thaddy · 2025-07-09 11:39 If your popup form is the child of a real popup window then it might work... Pass the popup as the owner. Two forms: mainform with a popup menu: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 unit popupmain; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, Menus, popup2; type   { TForm1 }   TForm1 = class(TForm)    PopupMenu1: TPopupMenu;    procedure FormActivate(Sender: TObject);    procedure PopupMenu1Popup(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation {$R *.lfm} { TForm1 } procedure TForm1.PopupMenu1Popup(Sender: TObject);begin   popup2.Form2.show;;end; procedure TForm1.FormActivate(Sender: TObject);begin  popup2.Form2.hide;end; end The second form is just a form: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 unit popup2; {$mode ObjFPC}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, Menus; type   { TForm2 }   TForm2 = class(TForm)   private   public   end; var  Form2: TForm2; implementationuses popupmain; // <-- mainform {$R *.lfm} { TForm2 } end.That's all. ---------------------------------------------------------------------- nanobit · 2025-07-09 11:55 Related examples: https://github.com/salvadorbs/LazTips.PopupForm https://github.com/arctic-sun/DropDownFormEx ---------------------------------------------------------------------- Thaddy · 2025-07-09 12:10 Both of these are way to complex. See my example, it is really easy. Right click on main form pops up the popupform, which has focus. Now when the main form is focused again the popup disappears. That was all that was asked wasn't it? By using a real popup I saved hundreds of lines of code compared to the examples you mentioned.