用我自己的TCustomEdit上下文菜单 [英] Replace TCustomEdit context menu with my own

查看:349
本文介绍了用我自己的TCustomEdit上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用我自己的弹出菜单(其中有更多的操作)替换TCustomEdit组件(如TEdit或TMemo)中显示的所有弹出式菜单。到目前为止,我用自己的TPopUpMenu手动替换每个组件的PopUpMenu属性。但是我想知道如果我可以手动为每个组件手动修改此属性,我所有的形式。



我想要一个像钩子拦截对这个系统菜单的调用并替换为我自己的菜单。这是可能吗?



解决方案

在主窗体中,添加以下代码。它应该适用于所有表单的自定义控件。

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆