问题
I have an application which consists of a form with FormStyle declared as "fsStayOnTop", so that it always is shown on top of all other windows. Now I would like to temporarily show another form where the user can set some settings. That form should also be shown on top, so I change the FormStyle property of the main form to "fsNormal" and the FormStyle of the form I want to show to "fsStayOnTop". When the temporary form closes the main form gets "fsStayOnTop" again.
Now the settings form stays on top, but only until I activate it by a mouse click inside the form. When I click on another window after that, the clicked form is on top and the defined FormStyle seems to have no effect anymore. Can anyone help me with that?
Here's my FormShow and FormClose mehtod:
procedure TForm3.FormClose(Sender: TObject; var Action: TCloseAction);
begin
ScaleOpen := false;
SetForegroundWindow(TempHandle);
Form1.FormStyle := fsStayOnTop;
end;
procedure TForm3.FormShow(Sender: TObject);
begin
TempHandle := GetForegroundWindow;
OldScaleM := Form1.GetScale;
SaveChanges := False;
ScaleOpen := true;
Form1.FormStyle := fsNormal;
Form3.FormStyle := fsStayOnTop;
end;
回答1:
You can set a form to the "always on top" state using this code:
SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0,
SWP_NoMove or SWP_NoSize);
You go back to normal mode with this code:
SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0,
SWP_NoMove or SWP_NoSize);
To try it, just drop two buttons on your form and associate the above code to their respective OnClick handlers.
来源:https://stackoverflow.com/questions/30070603/how-to-make-a-form-always-stay-on-top