Автор: 0D0A
Дата сообщения: 04.07.2010 17:45
Bazzill
Код:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TForm1 = class(TForm)
RichEdit1: TRichEdit;
procedure RichEdit1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
procedure FormDestroy(Sender: TObject);
private
FEditMenu: HMENU;
FEditSubMenu: HMENU;
procedure LoadEditContextMenu;
procedure WMCommand(var message: TWMCommand); message WM_COMMAND;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
const
ID_SELALL = 177;
ID_CUT = 768;
ID_COPY = 769;
ID_PASTE = 770;
ID_DELETE = 771;
ID_UNDO = 772;
procedure TForm1.FormDestroy(Sender: TObject);
begin
if (FEditMenu <> 0) then
DestroyMenu(FEditMenu);
end;
procedure TForm1.LoadEditContextMenu;
begin
if (FEditMenu = 0) then
begin
FEditMenu := LoadMenu(GetModuleHandle('user32.dll'),
MAKEINTRESOURCE(1));
if (FEditMenu <> 0) then
begin
FEditSubMenu := GetSubMenu(FEditMenu, 0);
if (FEditSubMenu <> 0) then
begin
// Удаляем непонятное
RemoveMenu(FEditSubMenu, 32769, MF_BYCOMMAND);
RemoveMenu(FEditSubMenu, 32768, MF_BYCOMMAND);
RemoveMenu(FEditSubMenu, 32787, MF_BYCOMMAND);
RemoveMenu(FEditSubMenu, 8, MF_BYPOSITION);
end;
end;
end;
end;
procedure TForm1.RichEdit1ContextPopup(Sender: TObject; MousePos: TPoint;
var Handled: Boolean);
begin
LoadEditContextMenu;
if (FEditSubMenu <> 0) then
begin
Handled := True;
// Тут изменяем состояние айтемов, если надо
// EnableMenuItem(FEditSubMenu, ID_COPY, MF_GRAYED or MF_BYCOMMAND);
// EnableMenuItem(FEditSubMenu, ID_CUT, MF_GRAYED or MF_BYCOMMAND);
// и т.д.
// Показываем
MousePos := RichEdit1.ClientToScreen(MousePos);
TrackPopupMenu(FEditSubMenu, TPM_LEFTALIGN or TPM_RIGHTBUTTON,
MousePos.X, MousePos.Y, 0, Handle, nil);
end;
end;
procedure TForm1.WMCommand(var message: TWMCommand);
begin
inherited;
case message.ItemID of
ID_SELALL:
ShowMessage('Select All');
ID_CUT:
ShowMessage('Cut');
ID_COPY:
ShowMessage('Copy');
ID_PASTE:
ShowMessage('Paste');
ID_DELETE:
ShowMessage('Delete');
ID_UNDO:
ShowMessage('Undo');
end;
end;
end.