gpt4 book ai didi

delphi - 如何阻止 TRadioButton 对方向键使用react?

转载 作者:行者123 更新时间:2023-12-03 15:51:26 25 4
gpt4 key购买 nike

我有一个面板,其中有几个水平放置的TRadioButton。如果最左边的按钮获得焦点并且我按向左箭头,则焦点会跳转到最右边的按钮。我想在所有箭头到达边缘时停止这种行为。是否可以 ?我尝试覆盖 WM_KEYDOWN 但当按下箭头键时按钮永远不会收到此消息。

  TRadioButton = class(StdCtrls.TRadioButton)
protected
procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
procedure WMKeyUp(var Message: TWMKeyUp); message WM_KEYUP;
public
BlockLeft, BlockRight: Boolean;
constructor Create(AOwner: TComponent); override;
end;

constructor TRadioButton.Create(AOwner: TComponent);
begin
inherited;
BlockLeft:= False;
BlockRight:= False;
end;

procedure TRadioButton.WMKeyDown(var Message: TWMKeyDown);
begin
if BlockLeft and (Message.CharCode = VK_LEFT) then Exit;
if BlockRight and (Message.CharCode = VK_RIGHT) then Exit;

inherited;
end;

procedure TRadioButton.WMKeyUp(var Message: TWMKeyUp);
begin
if BlockLeft and (Message.CharCode = VK_LEFT) then Exit;
if BlockRight and (Message.CharCode = VK_RIGHT) then Exit;
inherited;
end;

最佳答案

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;


如果这不是一次性行为,我会像维多利亚在问题评论中提到的那样编写一个容器控件。

关于delphi - 如何阻止 TRadioButton 对方向键使用react?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51307893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com