VCL将键盘消息偏移为
控制通知
并将其发送到消息的预定控件。所以你应该拦截
CN_KEYDOWN
取而代之的是信息。
如果这是一次性的设计考虑,我更愿意在表单级别处理此行为,因为imo控件本身不应该关心它放在何处。对于希望所有单选按钮的行为都相似的窗体,例如:
procedure TForm1.CMDialogKey(var Message: TCMDialogKey);
begin
if ActiveControl is TRadioButton then
case Message.CharCode of
VK_LEFT, VK_UP: begin
if ActiveControl.Parent.Controls[0] = ActiveControl then begin
Message.Result := 1;
Exit;
end;
end;
VK_RIGHT, VK_DOWN: begin
if ActiveControl.Parent.Controls[ActiveControl.Parent.ControlCount - 1]
= ActiveControl then begin
Message.Result := 1;
Exit;
end;
end;
end;
inherited;
end;
如果这不是一次性的行为,我会去写一个容器控件,维多利亚在评论中提到的问题。