动态控件响应事件
请教一个简单的问题:动态产生的按钮控件,parent不是窗口,只是窗口里面的表格,怎么响应事件?
btns[i]:=TButton.Create(nil);
btns[i].Parent:=tb;
btns[i].OnClick:=MyButtonClick;
procedure TForm1.MyButtonClick(Sender: TObject);
begin
ShowMessage('abc');
end;
TCustomGrid在响应WM_Command时未调用inherited(原因未知),所以不能处理其子控件按钮的点击事件。
解决办法是让TStringGrid自行处理WM_COMMAND。
type
TStringGrid=class(Grids.TStringGrid)
procedure WMCommand(var Message: TWMCommand); message WM_COMMAND;
end;
TForm1 = class(TForm)
sg: TStringGrid;//是我们重新定义过的TStringGrid
...
procedure TStringGrid.WMCommand(var Message: TWMCommand);
begin
inherited;//保留TCustomGrid的处理代码
with Message, TMessage(Message) do//将消息传给子控件
Message.Result := SendMessage(Ctl, Msg + CN_BASE, WParam, LParam);
end;