unit USysMenu;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Button1: TButton;
    GroupBox2: TGroupBox;
    Button2: TButton;
    GroupBox3: TGroupBox;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    procedure WMSysCommand(var Msg: TWMSysCommand);
                                message WM_SYSCOMMAND;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var MySystemMenu: HMENU;
const
  SC_MyItem1 = WM_USER + 1;
  SC_MyItem2 = WM_USER + 2;

procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
begin
 case Msg.CmdType of
  SC_CLOSE: ShowMessage('SC_CLOSE');
  SC_MAXIMIZE: ShowMessage('SC_MAXIMIZE');
  SC_MINIMIZE: ShowMessage('SC_MINIMIZE');
//  SC_MOVE: ShowMessage('SC_MOVE');
  SC_MOVE: begin Left := 0; Top := 0; exit; end;
  SC_RESTORE: ShowMessage('SC_RESTORE');
  SC_SIZE: ShowMessage('SC_SIZE');
  SC_MyItem1:
     if (GetMenuState(MySystemMenu, SC_MyItem1, MF_BYCOMMAND) and MF_CHECKED) <> 0
     then begin
//      FormStyle := fsNormal;     WM_STAYONTOP
      SetWindowPos(Handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE+SWP_NOSIZE);
      CheckMenuItem(MySystemMenu, SC_MyItem1, MF_BYCOMMAND+MF_UNCHECKED);
     end
     else begin
//      FormStyle := fsStayOnTop;
      SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE+SWP_NOSIZE);
      CheckMenuItem(MySystemMenu, SC_MyItem1, MF_BYCOMMAND+MF_CHECKED);
     end;
   SC_MyItem2: SendMessage(Handle,WM_SYSCOMMAND,SC_SCREENSAVE,0);
 end;
 inherited;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 DeleteMenu(MySystemMenu, SC_CLOSE, MF_BYCOMMAND);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
 GetSystemMenu(Handle, true);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 AppendMenu(MySystemMenu, MF_SEPARATOR, 0, '');
 AppendMenu(MySystemMenu, MF_STRING,
            SC_MyItem1, '&Поверх остальных');
 AppendMenu(MySystemMenu, MF_STRING,
            SC_MyItem2, '&Хранитель экрана');
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 MySystemMenu :=  GetSystemMenu(Handle, false);
end;

end.
 