Q: What is the easiest way to change the control menu of a form based application ?
Example, I was to add an option called "Always on top" that can be checked and unchecked at the user's will. This of course, should be there in addition to the "Move", "Restore", "Maximize" etc. other control menu options.
A: type
TForm1 = class(TForm);
{...}
private
procedure WMSysCommand(VAR Message: TWMSysCommand);
message WM_SYSCOMMAND;
...
procedure TForm1.WMSysCommand(var Message: TWMSysCommand);
begin
Inherited;
IF Mesage.CmdType AND $FFF0 = $F200 THEN
MessageBeep(0); {replace with code to DO something}
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
AppendMenu(GetSystemMenu(Handle, False), MF_STRING, $F200, '&Wahoo!');
end;
This adds a new item named "Wahoo!"
to the system menu, with a command ID of $F200. I picked $F200 at random
as a number near the regular SC_xxxx constants, but greater than any of
them. Note that the ID for a
system command must be evenly divisibly by 16, as Windows uses the
lowest 4 bits of the ID. That's also why you AND the value with $FFF0 before
comparing it to the specific ID.
Here is a template for changing the system menu:
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, Menus;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
procedure winmsg(var msg:tmsg;var handled:boolean);
{This is what handles the messages}
procedure DOWHATEVER;{procedure to do whatever}
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const ItemID=99;{the ID number for your menu item--can be anything}
procedure tform1.winmsg(var msg:tmsg;var handled:boolean);
begin
if msg.message=wm_syscommand then{if the message is a system
one...}
if msg.wparam = ItemID then DOWHATEVER;{then check if
its parameter
is your Menu items ID,}
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
application.onmessage:=winmsg;
{tell your app that 'winmsg' is the application message handler}
AppendMenu(GetSystemMenu(form1.handle,false),mf_separator,0,'');
{Add a seperator bar to form1}
AppendMenu(GetSystemMenu(form1.handle,false),mf_byposition,ItemID,
'&New Item');
{add your menu item to form1}
AppendMenu(GetSystemMenu(application.handle,false),mf_separator,0,'');
{Add a seperator bar to the application system menu(used when app
is minimized)}
AppendMenu(GetSystemMenu(application.handle,false),mf_byposition,
ItemID,'&New Item'
{add your menu itemto the application system menu(used when app is
minimized)}
{for more information on the AppendMenu and GetSystemMenu see online
help}
end;
procdure TForm2.DOWHATEVER;
begin
{add whatever you want to this procedure}
end;
end.