Sertac has answered the question: You can only move them.
Here is the complete final solution to making the Sysmenu useful, which does:
a) Moves all standard items except close to a submenu, which is hidden behind a separator.
b) Adds menu items from Form1.PopupMenu1 to the SysMenu
c) removes them (before making the window fullscreen/borderless as this destroys the sysmenu)
d) shows the sysmenu
procedure TForm1.SysMenuAddRemoveExtraItems(Add:boolean=true);
//
var
SysMenu, SubMenu : HMenu;
const NumItems:integer=0;
procedure InsertM(Position,I:integer;J:integer=-1;S:string='');
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms647987(v=vs.85).aspx
var M:TMenuItem; H:thandle; Flags:cardinal;
begin
M:=PopupMenu1.Items;
if I>=0 then M:=M.Items[I];
Flags:=MF_BYPOSITION+MF_STRING;
if M.Count>1 then Flags:=Flags+MF_POPUP;
if J>=0 then M:=M.Items[J];
H:=M.Handle;
if S='' then S:=M.Caption;
InsertMenu(SysMenu,Position,Flags,H,pwidechar(S));
inc(NumItems);
end;
procedure InsertSeparator(Position:integer);
begin
InsertMenu(SysMenu,Position,MF_BYPOSITION,MF_SEPARATOR,0); {Add a seperator bar to main form-form1}
inc(NumItems);
end;
procedure RemoveItems;
var i:integer;
begin
for i := 1 to NumItems do //remove items from top
RemoveMenu(SysMenu,0,MF_BYPOSITION);
NumItems:=0;
end;
procedure DeleteAppend(ID:cardinal); //to move standard menuitems to submenu
var
Caption: string;
CaptionLen: Integer;
begin
CaptionLen := GetMenuString(SysMenu, ID, nil, 0, MF_BYCOMMAND);
if CaptionLen > 0 then begin
Inc(CaptionLen);
SetLength(Caption, CaptionLen);
GetMenuString(SysMenu, ID, PChar(Caption), CaptionLen, MF_BYCOMMAND);
DeleteMenu(SysMenu, ID, MF_BYCOMMAND);
AppendMenu(SubMenu, MF_STRING, ID, PChar(Caption));
end;
end;
procedure MoveDefaultSysMenuItemsToSubmenu(Caption:string='';JustSeparator:boolean=false);
//Can either have a a caption or JustSeparator (submenu will be inaccessible)
// https://stackoverflow.com/questions/44735708/system-menu-how-to-hide-move-standard-menuitems/44743027#44743027
begin
SubMenu := CreateMenu; //make submenu to move them into
if SubMenu <> 0 then begin
DeleteAppend(SC_RESTORE);
DeleteAppend(SC_MOVE);
DeleteAppend(SC_SIZE);
DeleteAppend(SC_MINIMIZE);
DeleteAppend(SC_MAXIMIZE);
if JustSeparator then begin
DeleteMenu(SysMenu, 0, MF_BYPOSITION); //remove separator above CLOSE
InsertMenu(SysMenu, 0, MF_BYPOSITION or MF_POPUP or MF_SEPARATOR, SubMenu, '');
end else begin
DeleteMenu(SysMenu, 0, MF_BYPOSITION); //remove separator above CLOSE
InsertMenu(SysMenu, 0, MF_BYPOSITION or MF_POPUP, SubMenu, PChar(Caption));
InsertMenu(SysMenu, 1, MF_BYPOSITION or MF_SEPARATOR, 0, nil);
end;
end;
end;
procedure DestroySubmenu;
var Info: TMenuItemInfo;
begin
Info.cbSize := SizeOf(Info);
Info.fMask := MIIM_SUBMENU;
if GetMenuItemInfo(GetSystemMenu(Handle, False), 0, True, Info) then
DestroyMenu(Info.hSubMenu);
//GetSystemMenu(Handle, True);
end;
begin
SysMenu := GetSystemMenu(Handle, FALSE) ; {Get system menu}
//InsertMenu(SysMenu,1,MF_BYPOSITION+MF_STRING,SC_MyMenuItem2,'pqr');
if Add then begin
MoveDefaultSysMenuItemsToSubmenu('',true);
// InsertSeparator(0);
InsertM(0,PopupMenu1.Items.Count-2);
InsertM(0,-1,-1,'Menu'); //help
InsertM(0,7);
end
else begin //remove items
RemoveItems;
DestroySubmenu;
end;
end;
//------------------------------------
procedure TForm1.ShowSysMenu;
var P:TPoint;
begin
P:=ClientToScreen(Point(0,0));
TrackPopupMenu(GetSystemMenu(Handle, FALSE), TPM_LEFTALIGN+TPM_TOPALIGN+TPM_RETURNCMD+TPM_NONOTIFY,
P.X,P.Y,0,self.Handle,nil);
end;
//--------------------------------------------------------------
procedure TForm1.WMSysCommand(var Msg: TWMSysCommand);
// https://www.delphipower.xyz/guide_7/customizing_the_system_menu.html
var Item: TMenuItem;
begin
Item := PopupMenu1.FindItem (Msg.CmdType, fkCommand);
if assigned(Item) then Item.Click
else
// case Msg.CmdType of
// //put any other specials in here
// else
inherited;
// end;
end;