给个例子,具体功能可以自己改
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Menus;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormCreate(Sender: TObject);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
sitemenu:tpopupmenu;
procedure createpopupmenu;
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure tform1.CreatePopupMenu();
var
NewItem:TMenuItem;
begin
SiteMenu:=TPopupMenu.Create(Self);
SiteMenu.AutoHotkeys:=maManual;
//加入弹出菜单项
NewItem:=TMenuItem.Create(Self);
NewItem.Caption:='编辑站点';
// NewItem.OnClick:=EditSiteMenuClick;
SiteMenu.Items.Add(NewItem);
NewItem:=TMenuItem.Create(Self);
NewItem.Caption:='删除站点';
// NewItem.OnClick:=DelSiteMenuClick;
SiteMenu.Items.Add(NewItem);
NewItem:=TMenuItem.Create(Self);
NewItem.Caption:='删除连接';
// NewItem.OnClick:=DelSiteLinkMenuClick;
SiteMenu.Items.Add(NewItem);
NewItem:=TMenuItem.Create(Self);
NewItem.Caption:='查询站点';
// NewItem.OnClick:=QuerySiteMenuClick;
SiteMenu.Items.Add(NewItem);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
createpopupmenu;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if sitemenu<>nil then
sitemenu.free;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
sitemenu:=nil;
end;
procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if (button=mbright) and (sitemenu<>nil) then
begin
sitemenu.Popup(mouse.CursorPos.x,mouse.CursorPos.y);
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
mt:tmenuitem;
begin
mt:=sitemenu.Items.Find('查询站点');
mt.Checked:=not mt.Checked;
end;
end.