2010-03-09 41 views
0

根据应用程序的状态,表单上的按键有时可能有不同的配方。见下面的示例:在父表单和子控件之间分配按键

unit Unit1; 

interface 

uses 
    Windows, 
    Messages, 
    SysUtils, 
    Variants, 
    Classes, 
    Graphics, 
    Controls, 
    Forms, 
    Dialogs, 
    ComCtrls, 
    Buttons; 

type 
    TForm1 = class(TForm) 
    private 
    ListView1: TListView; 
    ButtonOK: TBitBtn; 
    ButtonCancel: TBitBtn; 
    procedure ButtonClick(Sender: TObject); 
    public 
    constructor Create(AOwner: TComponent); override; 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

constructor TForm1.Create(AOwner: TComponent); 
begin 
    inherited CreateNew(AOwner); 
    ClientWidth := 300; 
    ClientHeight := 240; 

    ListView1 := TListView.Create(Self); 
    ListView1.Name := 'ListView1'; 
    ListView1.Parent := Self; 
    ListView1.Height := 200; 
    ListView1.Align := alTop; 
    ListView1.AddItem('aaaaa', nil); 
    ListView1.AddItem('bbbbb', nil); 
    ListView1.AddItem('ccccc', nil); 

    ButtonOK := TBitBtn.Create(Self); 
    ButtonOK.Parent := Self; 
    ButtonOK.Left := 8; 
    ButtonOK.Top := 208; 
    ButtonOK.Kind := bkOK; 
    ButtonOK.OnClick := ButtonClick; 

    ButtonCancel := TBitBtn.Create(Self); 
    ButtonCancel.Parent := Self; 
    ButtonCancel.Left := 90; 
    ButtonCancel.Top := 208; 
    ButtonCancel.Kind := bkCancel; 
    ButtonCancel.OnClick := ButtonClick; 
end; 

procedure TForm1.ButtonClick(Sender: TObject); 
begin 
    ShowMessage((Sender as TBitBtn).Caption); 
    Application.Terminate; 
end; 

end. 

(要运行此,创建一个标准的VCL应用程序,并与上述替换Unit1.pas的内容)

如果一个启动应用并按下回车Esc,适当的按钮被“点击”。但是,当开始编辑列表视图(通过点击一个项目的一个半时间)输入Esc应该接受或取消他们不需要的编辑 - 他们仍然“单击”按钮。

类似的情况存在,如果有快捷键的操作F2或含有cxGrid,默认情况下使用这些快捷键来启动编辑模式或下拉组合框中编辑表单上F4

您是否知道如何继续使用TButton.Default/Cancel和actions的舒适度,而而不是不得不重新实现我使用的所有组件的密钥处理?

回答

2

我想你使用的控件运气不好。 TMemo正确处理它,但确实可编辑的TListView不。 problem seems to originate from win32而不是VCL包装它。因此,如果你不喜欢它的当前行为,那么你必须重新实现TListView上的密钥处理。

procedure WMGetDlgCode(var Message: TMessage); message WM_GETDLGCODE; 

procedure TMyListView.WMGetDlgCode(var Message: TMessage); 
begin 
    inherited; 

    if IsEditing then 
    Message.Result := Message.Result or DLGC_WANTALLKEYS; 
end; 

由于所有控件的行为不同,它是决定他们是哪个键感兴趣的控制自己,我看不出你如何能解决它,而无需改变不受欢迎的行为。

+1

它应该也可以做同样的事情,而不用做一个派生类,就像这里:http://stackoverflow.com/questions/2363456/how-do-i-catch-a-vk-tab-key-in -my-tedit-control-and-not-let-it-lose-the-focus – kludg 2010-03-09 19:31:30

+0

而不是创建自己的TmyEdit或类似的东西,只需使用类似于类型TEdit = class(StdCtrls.TEdit){... } end;'添加任何你想要的东西,包括按键的新消息处理。你的表单上的任何地方,你有一个标准的TEdit现在将使用你的新扩展版本。或者,将您的新类型声明添加到单元以从多个表单使用它。在包含原始控件的所有单元(添加它到最后是最简单的解决方案)之后,您将必须确保将您的单元添加到uses子句中。 – 2011-09-14 17:15:30

相关问题