用我自己的TCustomEdit上下文菜单 [英] Replace TCustomEdit context menu with my own
问题描述
我想要一个像钩子拦截对这个系统菜单的调用并替换为我自己的菜单。这是可能吗?
在主窗体中,添加以下代码。它应该适用于所有表单的自定义控件。
TForm2 = class(TForm)
procedure FormCreate(Sender:TObject );
private
procedure ActiveControlChanged(Sender:TObject);
结束
实现
类型
TCustomEditAccess =类(TCustomEdit);
TCustomGridAccess = class(TCustomGrid);
程序TForm2.ActiveControlChanged(发件人:TObject);
begin
如果(Screen.ActiveControl是TCustomEdit)而不是分配(TCustomEditAccess(Screen.ActiveControl).PopupMenu)然后
TCustomEditAccess(Screen.ActiveControl).PopupMenu:= MyPopupMenu
否则if(Screen.ActiveControl是TCustomGrid)然后
begin
TCustomGridAccess(Screen.ActiveControl).ShowEditor;
如果分配(TCustomGridAccess(Screen.ActiveControl).InplaceEditor)然后
TCustomEditAccess(TCustomGridAccess(Screen.ActiveControl).InplaceEditor).PopupMenu:= MyPopupMenu;
结束
结束
procedure TForm2.FormCreate(Sender:TObject);
begin
Screen.OnActiveControlChange:= ActiveControlChanged;
结束
它只是一个简化的版本(在编码的角度来看)kobik的答案,也将解决由代码或其他不使用Form作为所有者的其他复杂控件创建的任何TCustomEdit。
他的指导如何确定哪个CustomEdit弹出窗口适用。 >
编辑:添加网格InplaceEditor支持
I want to replace all the popup menus displayed by delphi in the TCustomEdit components like TEdit or TMemo using my own popup menu (which has a lot of more actions). So far I replace the PopUpMenu property of each component manually with my own TPopUpMenu. but I wondering if I can do this without modify this property manually for each component in all my forms.
I want something like a hook to intercept the calls to this system menu and replace for my own menu. is this possible?
In your main form, add the following code. It should apply to all your form's custom control.
TForm2 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure ActiveControlChanged(Sender: TObject);
end;
implementation
type
TCustomEditAccess = class(TCustomEdit);
TCustomGridAccess = class(TCustomGrid);
procedure TForm2.ActiveControlChanged(Sender: TObject);
begin
if (Screen.ActiveControl is TCustomEdit) and not Assigned(TCustomEditAccess(Screen.ActiveControl).PopupMenu) then
TCustomEditAccess(Screen.ActiveControl).PopupMenu := MyPopupMenu
else if (Screen.ActiveControl is TCustomGrid) then
begin
TCustomGridAccess(Screen.ActiveControl).ShowEditor;
if Assigned(TCustomGridAccess(Screen.ActiveControl).InplaceEditor) then
TCustomEditAccess(TCustomGridAccess(Screen.ActiveControl).InplaceEditor).PopupMenu := MyPopupMenu;
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
Screen.OnActiveControlChange := ActiveControlChanged;
end;
It is just a simplified version (in the point of view of coding) of kobik's answer and will also address any TCustomEdit that are created by code or other complex Controls which do not use the Form as Owner.
His instruction on how to determine which CustomEdit popup apply.
Edit : Add Grid InplaceEditor Support
这篇关于用我自己的TCustomEdit上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!