unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Menus;
type
TForm1 =
class(TForm)
PopupMenu1: TPopupMenu;
Edit1: TEdit;
Edit2: TEdit;
procedure PopupMenu1Popup(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
procedure MenuItemOnClick(Sender: TObject);
procedure AddMenuItem(PopupMenu: TPopupMenu; Caption :
string);
procedure InitPopUpMenu(PopupMenu: TPopupMenu; MyItems:
array of string);
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
ClipBrd;
// Aktion auslösen, wenn Item angelickt wird.
procedure TForm1.MenuItemOnClick(Sender: TObject);
begin
Win32Check(ActiveControl
is TEdit);
case TMenuItem(Sender).MenuIndex
of
0: TEdit(ActiveControl).Undo;
2: TEdit(ActiveControl).CutToClipboard;
3: TEdit(ActiveControl).CopyToClipboard;
4: TEdit(ActiveControl).PasteFromClipboard;
5: TEdit(ActiveControl).ClearSelection;
7: TEdit(ActiveControl).SelectAll;
end;
end;
// Dem PopupMenu ein MenuItem anhängen
procedure TForm1.AddMenuItem(PopupMenu: TPopupMenu; Caption:
string);
var
menu: TMenuItem;
begin
menu := TMenuItem.Create(Self);
menu.Caption := Caption;
menu.OnClick := MenuItemOnClick;
PopupMenu.Items.Add(menu);
end;
// Default Menuitems aus user32.dll auslesen und in Popupmenu einfügen
// Eigene Menuitems anhängen
procedure TForm1.InitPopUpMenu(PopupMenu: TPopupMenu; MyItems:
array of string);
var
H, S: HMENU;
C, i: Integer;
Buf:
array[0..255]
of Char;
begin
// auslesen
H := LoadMenu(GetModuleHandle('
user32.dll'), PChar(1));
S := GetSubMenu(H, 0);
C := GetMenuItemCount(S);
for i := 0
to { C - 1} 8
do
begin
GetMenuString(S, i, Buf, SizeOf(Buf), MF_BYPOSITION);
if Buf[0] = #0
then
Buf := '
-';
AddMenuItem(PopupMenu, Buf);
end;
DestroyMenu(H);
// Eigene Items anhängen
for i := 0
to Length(MyItems) - 1
do
AddMenuItem(PopupMenu, MyItems[i]);
end;
// Menuitems je nach Zustand des TEdits enablen/disablen
procedure TForm1.PopupMenu1Popup(Sender: TObject);
begin
Win32Check(ActiveControl
is TEdit);
TPopupMenu(Sender).Items[0].Enabled := TEdit(ActiveControl).CanUndo;
TPopupMenu(Sender).Items[2].Enabled := TEdit(ActiveControl).SelLength > 0;
TPopupMenu(Sender).Items[3].Enabled := TEdit(ActiveControl).SelLength > 0;
TPopupMenu(Sender).Items[4].Enabled := Clipboard.HasFormat(CF_TEXT);
TPopupMenu(Sender).Items[5].Enabled := TEdit(ActiveControl).SelLength > 0;
TPopupMenu(Sender).Items[7].Enabled := (TEdit(ActiveControl).SelLength <>
Length(Edit1.Text))
and (Length(Edit1.Text) > 0);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// PopUpMenu initialisieren
InitPopUpMenu(PopUpMenu1, ['
Menuitem 1', '
Menuitem 2']);
end;
end.