Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi XE2, how to keep form ON TOP after changing VCL styles

I encountered a weird issue with XE2:

I'm using HWND_TOPMOST with SetWindowPos to set my form on top, but if I switch VCL styles at runtime, the window isn't topmost anymore, and unsetting/re-setting it doesn't fix it either.

Any way to fix this?

like image 923
hikari Avatar asked May 10 '12 00:05

hikari


2 Answers

Your problem is that the form is being recreated because of a style change and loosing its top most style since the VCL have no knowledge of this. Either use:

FormStyle := fsStayOnTop; 

or override CreateWindowHandle so that SetWindowPos is called each time the form is recreated:

type
  TForm1 = class(TForm)
    ..
  protected
    procedure CreateWindowHandle(const Params: TCreateParams); override;
  ..

procedure TForm1.CreateWindowHandle(const Params: TCreateParams);
begin
  inherited;
  SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE or SWP_NOMOVE);
end;


BTW, I couldn't duplicate "unsetting/re-setting doesn't fix it". With my tests, calling SetWindowPos again fixed it.

like image 101
Sertac Akyuz Avatar answered Nov 09 '22 16:11

Sertac Akyuz


Setting a new style on a control causes the control's window handle to be recreated, thus HWND_TOPMOST would have to be re-applied again.

like image 41
Remy Lebeau Avatar answered Nov 09 '22 17:11

Remy Lebeau