|
Technical answers from the trenches |
|
Adding Commands to a Form's System Menu
| ||||
Posted: 8 October 2002 |
||||
  |
Audience: Intermediate |
|||
Question: I want to add a menu command to the System menu. How do I do this?Answer: This is a three step process:
The following code sample illustrates each step by adding a separator and an About command to a form's System menu. unit mainform;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
// Lets form respond to system command messages
procedure wmSysCommand( var msg: TMessage ); message WM_SYSCOMMAND;
public
{ Public declarations }
end;
const
// Define WM_USER constants for custom
// menu commands.
ID_SYS_ABT = WM_USER + 1024; // About command constant
ID_SYS_SEP = ID_SYS_ABT + 1;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
// Adds items to the system menu
var
hSysMenu : tHandle;
begin
hSysMenu := getSystemMenu( Handle, False );
InsertMenu( hSysMenu, Word( 5 ),
MF_SEPARATOR + MF_BYPOSITION, ID_SYS_SEP, '' );
InsertMenu( hSysMenu, Word( 6 ), MF_BYPOSITION,
ID_SYS_ABT, 'About' );
end;
procedure TForm1.wmSysCommand;
// Responds to system command messages.
begin
case Msg.wParam of
ID_SYS_ABT : showMessage( 'About this application...' );
end;
inherited;
end;Please note that we also declared a constant for the About command to simplify the evaluation of the wParam parameter in wmSysCommand. While this particular example could have used a simple IF statement to evaluate wParam, SWITCH statements require less code when adding multiple wParam alternatives. Use the approach that seems most appropriate for your needs. When using this approach, please keep the following in mind:
|
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 31 May 2003
|
||
|
|
||
|
[- End -]
|