Window class style CS_NOCLOSE does not work after calling RecreateWnd

I need a form to disable the close button on the menu (and also disable the close using Alt-F4), so I used the CS_NOCLOSEclass. It works as expected if I installed it in CreateParams:

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited;  
  Params.WindowClass.style := Params.WindowClass.style or CS_NOCLOSE;
end;

The close button is disabled and you cannot close the window using ALT + F4 (I have my close button).

Now I have added the flag:, FNoCloseButtonoriginally set to False.

procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited;
  if FNoCloseButton then
    Params.WindowClass.style := Params.WindowClass.style or CS_NOCLOSE;
end;

And after creating the forms, I have the following:

procedure TForm1.Button1Click(Sender: TObject);
begin
  FNoCloseButton := True;
  RecreateWnd;
end;

After clicking Button1, the window will be re-created, but CS_NOCLOSEnow it does not work and is ignored.

Why is this behavior? Why can't I change the style of the Window class after I create it? (I thought I could, because there is an SetClassLongapi)

SetClassLong :

procedure TForm1.Button2Click(Sender: TObject);
begin
  SetClassLong(Self.Handle, GCL_STYLE, GetClassLong(Self.Handle, GCL_STYLE) or CS_NOCLOSE);
  DrawMenuBar(Self.Handle); // Must call this to invalidate
end;

. ( Alt-F4), "" , , . SetClassLong .

?

+4
1
procedure TForm1.CreateParams(var Params: TCreateParams);
begin
  inherited;
  if FNoCloseButton then
    Params.WindowClass.style := Params.WindowClass.style or CS_NOCLOSE;
end;

, , , , ; FNoCloseButton .

RecreateWindow, VCL , , ERROR_CLASS_ALREADY_EXISTS. , , . , VCL.


, , , . VCL, , , . SetClassLong[Ptr], .

type
  TForm1 = class(TForm)
    ..
  protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure DestroyHandle; override;
    ...

..

procedure TForm1.DestroyHandle;
begin
  inherited;
  if not winapi.windows.UnregisterClass(PChar(ClassName), HInstance) then
    RaiseLastOSError;
end;
+2

Source: https://habr.com/ru/post/1679147/


All Articles