gpt4 book ai didi

delphi - 标记/取消标记一组枚举中的另一个选项

转载 作者:行者123 更新时间:2023-12-02 06:12:48 27 4
gpt4 key购买 nike

我在组件中有一组枚举,如下所示:

type
TOption = (clVisible, clVisibleAlways, clRenderable, clEditable);
TOptions = set of TOption;

const
defaultOptions = [clVisible, clRenderable];

type
TMyComp = class(TComponent)
private
FOptions: TOptions;
procedure SetOptions(const Value: TOptions);
public
property Options: TOptions read FOptions write SetOptions default defaultOptions;

...

procedure TMyComp.SetOptions(const Value: TOptions);
var
ToBeSet, ToBeCleared: TOptions;
begin
if FOptions <> Value then
begin
ToBeCleared:= FOptions - Value;
ToBeSet:= Value - FOptions;

FOptions:= Value;

//clVisible -> clRenderable
if (clVisible in ToBeSet) and (clRenderable in ToBeCleared) then
begin
Include(FOptions, clRenderable);
Include(ToBeSet, clRenderable);
end;

//not clRenderable -> not clVisible
if (clRenderable in ToBeCleared) and (clVisible in ToBeSet) then
begin
Exclude(FOptions, clVisible);
Exclude(ToBeSet, clVisible);
end;

//not clVisible -> not clVisibleAlways
if (clVisible in ToBeCleared) and (clVisibleAlways in ToBeSet) then
begin
Exclude(FOptions, clVisibleAlways);
Exclude(ToBeSet, clVisibleAlways);
end;

//clVisibleAlways -> clVisible
if (clVisibleAlways in ToBeSet) and (clVisible in ToBeCleared) then
begin
Include(FOptions, clVisible);
Include(ToBeSet, clVisible);
end;
end;
end;

我想做但行不通的是:

  • 如果检查了 clVisibleAlways,还检查 clVisible
  • 如果检查了 clVisible,还检查 clRenderable
  • 如果未选中 clRenderable,也取消选中 clVisible
  • 如果未选中 clVisible,也取消选中 clVisibleAlways

请对这个主题给予一些支持。

最佳答案

我认为你的方法不必要地复杂。

更新

必须清楚地区分添加或删除选项(我之前没有这样做)。更新后的代码做到了这一点,并且在 Delphi 10.1 Berlin 中运行良好。我的测试组件名为 TNewButton,基于 TButton

procedure TNewButton.SetOptions(const Value: TOptions);
var
Opts: TOptions; { Because of "const"; you can also remove "const"
and use Value instead of Opts. }
begin
if FOptions <> Value then
begin
Opts := Value;
{ Find out if we are adding or removing an option. }
if Opts - FOptions <> [] then
begin
{ We are adding an option. }
if clVisibleAlways in Opts then
Include(Opts, clVisible);
if clVisible in Opts then
Include(Opts, clRenderable)
end
else
begin
{ We are removing an option. }
if not (clRenderable in Opts) then
Exclude(Opts, clVisible);
if not (clVisible in Opts) then
Exclude(Opts, clVisibleAlways);
end;
FOptions := Opts;
end;
end;

我对其进行了多次测试,并且至少在我的对象检查器中,它完全符合您的要求。

关于delphi - 标记/取消标记一组枚举中的另一个选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38472314/

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