app always on top [solved] lazarus / 34-Windows ---------------------------------------------------------------------- diego bertotti · 2022-01-15 14:17 hi i need to know how to make a window always on top, on W10. when i try my own code runs fine while ide run (pressing F9), but fails if a run a .exe file alone i try with admin rights, and fail. just open a new app, put a timer at 2000 ms and in it timeout procedure: 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 TForm1.Timer1Timer(Sender: TObject);Begin    //SetActiveWindow(form1.handle);    form1.Bringtofront;    form1.SetFocus;    form1.WindowState:= wsnormal; end;   anybody knows why? ---------------------------------------------------------------------- winni · 2022-01-15 14:58 Hi! use 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  FormStyle := fsStayOnTop; Winni ---------------------------------------------------------------------- diego bertotti · 2022-01-15 15:08 winni thanks, but fails too ---------------------------------------------------------------------- winni · 2022-01-15 15:15 Hi! Lousy Windows. Try 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 Application.bringToFront; Winni ---------------------------------------------------------------------- balazsszekely · 2022-01-15 15:24 fsStayOnTop property will make the form stay above other forms in your application, while fsSystemStayOnTop is system wide. Which one do you prefer? ---------------------------------------------------------------------- diego bertotti · 2022-01-15 20:27 thanks for replays i made a mistake describing my problem. really i want 2 things: 1_ window always on top , and with focus, it means, receiving keystrokes. 2_ why code works inside lazarus ide and why fail running alone, even with admin rights. getmen: using fsSystemStayOnTop, is always on top, but not always with focus. winni: don't work. somebody knows privileges or permisions executable file have when fired inside IDE? ---------------------------------------------------------------------- 440bx · 2022-01-15 20:42 Quote from: diego bertotti on January 15, 2022, 09:27:08 pmthanks for replays i made a mistake describing my problem. really i want 2 things: 1_ window always on top , and with focus, it means, receiving keystrokes. 2_ why code works inside lazarus ide and why fail running alone, even with admin rights. getmen: using fsSystemStayOnTop, is always on top, but not always with focus. winni: don't work. There is a big difference between a window always being on top and a window always having the focus.  Normally, it is not possible for a window to always have the focus because it's the O/S that decides which window should have the focus.  That said, if you need the window to see _all_ keystrokes whether or not they are meant for it or another window then you have to install a system-wide keyboard hook. Did I misunderstand what you're trying to do ? ---------------------------------------------------------------------- balazsszekely · 2022-01-15 20:42 @diego bertotti Quotegetmen: using fsSystemStayOnTop, is always on top, but not always with focus.It's called ForegroundWindow, which receive input focus. From Microsoft documentation: QuoteThe window with which the user is currently working. The system assigns a slightly higher priority to the thread that creates the foreground window than it does to other threads.Put a timer to your form, set Interval to 500, Enabled to true, then create an OnTimer event: 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 uses windows; procedure TForm1.Timer1Timer(Sender: TObject);begin  if Self.Handle <> GetForegroundWindow then    SetForegroundWindow(Self.Handle);end; Now your application is always on focus, until you stop the timer. As a side node this is a very abusive approach. As a user, I would delete you application immediately. ---------------------------------------------------------------------- 440bx · 2022-01-15 20:50 Quote from: GetMem on January 15, 2022, 09:42:57 pmAs a side node this is a very abusive approach. As a user, I would delete you application immediately. It certainly is very abusive and Windows doesn't cooperate with an application with that attitude.  There are a good number of restrictions on setting the foreground window.  I have some doubts that a window can continuously and successfully make itself the foreground window. I wonder what it is that @diego is _really_ trying to do. ---------------------------------------------------------------------- diego bertotti · 2022-01-15 20:59 getmem: 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 uses windows; procedure TForm1.Timer1Timer(Sender: TObject);begin  if Self.Handle <> GetForegroundWindow then    SetForegroundWindow(Self.Handle);end; it fail. i try it before and now again. but why all these diferent codes works fine inside ide? i need these behavior because is only one app running and user only will have an numeric keyboard, no mouse, then if a popup or on another thing happens, user will can't solve it. its about a special case user 440bx: my first motherboard was a triton 440bx!!! ---------------------------------------------------------------------- balazsszekely · 2022-01-15 21:04 @diego, @440bx Quoteit fail. i try it before. but why all these diferent codes works fine inside ide?I give up. I seriously doubt that works inside the IDE and fails when your program runs as a standalone application. Quotei need these behavior because is only one app running and user only will have an numeric keyboard, no mouse, then if a popup or onther thing happens, user will can't solve.Simulate ALT + TAB? ---------------------------------------------------------------------- diego bertotti · 2022-01-15 21:23 Quote from: GetMem on January 15, 2022, 10:04:01 pm@diego, @440bx Quoteit fail. i try it before. but why all these diferent codes works fine inside ide?I give up. I seriously doubt that works inside the IDE and fails when your program runs as a standalone application. Quotei need these behavior because is only one app running and user only will have an numeric keyboard, no mouse, then if a popup or onther thing happens, user will can't solve.Simulate ALT + TAB? my apologizes if i disturb you, but if you don't believe me, you can try it, just 5 min to do it with your computer and try it out. user will not have and ctrl and tab keys. numeric keyboerd normally don't have it ---------------------------------------------------------------------- diego bertotti · 2022-01-15 21:24 Quote from: 440bx on January 15, 2022, 09:42:22 pmQuote from: diego bertotti on January 15, 2022, 09:27:08 pmthanks for replays i made a mistake describing my problem. really i want 2 things: 1_ window always on top , and with focus, it means, receiving keystrokes. 2_ why code works inside lazarus ide and why fail running alone, even with admin rights. getmen: using fsSystemStayOnTop, is always on top, but not always with focus. winni: don't work. There is a big difference between a window always being on top and a window always having the focus.  Normally, it is not possible for a window to always have the focus because it's the O/S that decides which window should have the focus.  That said, if you need the window to see _all_ keystrokes whether or not they are meant for it or another window then you have to install a system-wide keyboard hook. Did I misunderstand what you're trying to do ? you got it!! in was thinking something like this or maybe a windows message with a mouse click with an app coordinates. but app will have a child windows. then keystrokes must be redirected to this and not to mainform of app...i'll see ---------------------------------------------------------------------- ASerge · 2022-01-16 10:19 Quote from: GetMem on January 15, 2022, 10:04:01 pmI give up. I seriously doubt that works inside the IDE and fails when your program runs as a standalone application. It's possible. From SetForegroundWindow: ...A process can set the foreground window only if one of the following conditions is true:...The process is being debugged... ---------------------------------------------------------------------- diego bertotti · 2022-01-16 12:14 Hi I found this: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser- locksetforegroundwindow Remarks The system automatically enables calls to SetForegroundWindow if the user presses the ALT key or takes some action that causes the system itself to change the foreground window (for example, clicking a background window). This function is provided so applications can prevent other applications from making a foreground change that can interrupt its interaction with the user. Ill try ---------------------------------------------------------------------- 440bx · 2022-01-16 12:24 I've seen what you want to do done... for instance, there are plenty of places where you have these "help yourself" terminals, e.g, self checkout in grocery stores, self checking in airports, etc.  Many of these run plain, standard Windows and they are limited to running that one app without interruption.  That said, there usually isn't a normal keyboard that would allow a user to get the O/S's attention (such as Alt-Tab, Ctrl-Esc, etc) What you want to do is possible and, there is sanctioned way of doing it (hacks/tricks are not a good idea in this case.)  Obviously, if you knew how it's done, I'd would offer it but, I don't. ---------------------------------------------------------------------- Soner · 2022-01-16 13:01 I needed it for our kiosk application so I made it last year. The user cant't even call taskmanager with [ctrl+alt+del] keys. have fun: 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 Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs; type   { TForm1 }   TForm1 = class(TForm)    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);    procedure FormCreate(Sender: TObject);    procedure FormDeactivate(Sender: TObject);  private   public   end; var  Form1: TForm1; implementation uses  LCLType {$IfDef WINDOWS},windows{$EndIf}; {$R *.lfm} procedure ActivateForm(Handle: HWND);begin{$IfDef WINDOWS}SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,  SWP_NOACTIVATE or SWP_NOMOVE or SWP_NOSIZE);{$else}#todo{$EndIf}end; { TForm1 } procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);begin  CanClose:=MessageDlg('Do you want close it?'+LineEnding+''+LineEnding+'(You can ask for password)', mtWarning, [mbYes, mbNo],0) = mrYes;end; procedure TForm1.FormCreate(Sender: TObject);begin  BorderStyle:=bsNone;  WindowState:=wsMaximized;  Application.OnDeactivate:=@FormDeactivate;  Application.OnMinimize:=@FormDeactivate;end; procedure TForm1.FormDeactivate(Sender: TObject);begin  //funktioniert nur im debugmodus: Application.MainForm.BringToFront;   if WindowState=wsMinimized then WindowState:=wsMaximized;  ActivateForm(Handle);end; end. And example project included too. Tested on Windows 7, Windows 10. Edit: Press [alt]+[f4] to close the application, i forget to put a button. ---------------------------------------------------------------------- winni · 2022-01-16 14:00 Hi! For the kiosk mode use 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 WindowState:=wsFullScreen; Winni ---------------------------------------------------------------------- diego bertotti · 2022-01-16 15:09 Thanks for replys But need top and focus Working on it ---------------------------------------------------------------------- Zoran · 2022-01-16 19:29 Try the attached application, see how it uses GetKeyState function. Here (Windows 10), it works same if application is focused or not. Try it! ---------------------------------------------------------------------- diego bertotti · 2022-01-17 14:08 hi finally i found it check this https://stackoverflow.com/questions/688337/how-do-i-force-my-app-to- come-to-the-front-and-take-focus just a simple conversion c to pascal. thanks to all ---------------------------------------------------------------------- diego bertotti · 2022-01-21 12:40 hi, again its me just to share with all. This code works almost perfectly. It fails only in a few cases of clicks combinations 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 Unit1; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,  windows; type   { TForm1 }   TForm1 = class(TForm)    CheckBox1: TCheckBox;    CheckGroup1: TCheckGroup;    Memo1: TMemo;    Timer1: TTimer;    procedure CheckGroup1ItemClick(Sender: TObject; Index: integer);    procedure FormActivate(Sender: TObject);    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);    procedure FormCloseQuery(Sender: TObject; var CanClose: boolean);    procedure FormCreate(Sender: TObject);    procedure FormDeactivate(Sender: TObject);    procedure FormDestroy(Sender: TObject);    procedure FormWindowStateChange(Sender: TObject);    procedure Timer1Timer(Sender: TObject);  private   public    end;   const  LSFW_LOCK = 1;  LSFW_UNLOCK = 2;  var  Form1: TForm1;  function LockSetForegroundWindow(LSFW_UNLOCK:UINT):WINBOOL; external 'user32' name 'LockSetForegroundWindow'; implementation    {$R *.lfm} { TForm1 } procedure ActivateForm(Handle: HWND);var  dwCurrentThread: DWORD;  dwFGThread: DWORD;begin dwCurrentThread := GetCurrentThreadId();dwFGThread      := GetWindowThreadProcessId(GetForegroundWindow(), NIL);  AttachThreadInput(dwCurrentThread, dwFGThread, TRUE); // Possible actions you may wan to bring the window into focus.{SetForegroundWindow(handle);LockSetForegroundWindow(1); }if SetForegroundWindow(handle) then  begin  form1.memo1.lines.add('FormActivate-SetForegroundWindow funcionó');  if LockSetForegroundWindow(LSFW_LOCK) then    form1.memo1.lines.add('FormActivate-LockSetForegroundWindow funcionó')    else    form1.memo1.lines.add('FormActivate- LockSetForegroundWindow no funcionó ' + inttostr(getlasterror));  end  else  begin  form1.memo1.lines.add('FormActivate-SetForegroundWindow falló')  end;  //SetCapture(handle);//Sets the mouse capture to the specified window belonging to the current thread //SetFocus(handle);//Sets the keyboard focus to the specified window //SetActiveWindow(handle);//Activates a window. The window must be attached to the calling thread's message queue. EnableWindow(handle, TRUE);//Enables or disables mouse and keyboard input to the specified window or control. AttachThreadInput(dwCurrentThread, dwFGThread, FALSE);  {$IfDef WINDOWS}{if SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE or SWP_SHOWWINDOW) then form1.memo1.lines.add('SetWindowsPos funcionó') else form1.memo1.lines.add('SetWindowsPos no funcionó') ; form1.formactivate(form1);  }  {$else}#todo{$EndIf}end; procedure TForm1.FormActivate(Sender: TObject);begin  end; procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin  LockSetForegroundWindow(2);end; procedure TForm1.CheckGroup1ItemClick(Sender: TObject; Index: integer);begin    case index of  9: begin     Application.terminate;     end;  0..8: begin     inputbox('Opción ' + IntToSTR(index+1), 'cantidad de opción '+ intToStr(index+1),'1');     end;    end;end; procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: boolean);begin end; procedure TForm1.FormCreate(Sender: TObject);begin    Application.OnDeactivate:=@FormDeactivate;    Application.OnMinimize:=@FormDeactivate;end; procedure TForm1.FormDeactivate(Sender: TObject);begin  if WindowState=wsMinimized then WindowState:=wsNormal;  memo1.lines.add('FormDeactivate-se desactivo');  ActivateForm(application.Handle); end; procedure TForm1.FormDestroy(Sender: TObject);begin  LockSetForegroundWindow(2);end; procedure TForm1.FormWindowStateChange(Sender: TObject);begin end; procedure TForm1.Timer1Timer(Sender: TObject);Var  hh: Handle; begin  checkbox1.checked:= not checkbox1.checked; end;   end. ---------------------------------------------------------------------- SymbolicFrank · 2022-01-21 13:48 Ah, yes. Try such an (industrial, POS or kiosk) app on multiple monitors If you do it on Windows, kill explorer.exe. If the PC is dedicated to your app, change all occurrences of 'shell=explorer.exe' to 'shell=yourapp.exe' in the registry. ---------------------------------------------------------------------- diego bertotti · 2022-01-22 14:14 now is working locksetforegroundwindow!! the problem was the function call declaration, i missed the clause stdcall. 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 function LockSetForegroundWindow(LSFW_UNLOCK:UINT):WINBOOL; stdcall; external 'user32' name 'LockSetForegroundWindow';  searching i found this function too: 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 SwitchToThisWindow(handle);  but is not like MS documentation. I'm not full tested yet. and this topic too: https://forum.lazarus.freepascal.org/index.php?topic=35282.0