对于使用DelphiObjectPascal的GUI,在(可能)更改之前检查.Visible是否有任何有用的用途?

对于使用DelphiObjectPascal的GUI,在(可能)更改之前检查.Visible是否有任何有用的用途?,delphi,delphi-7,Delphi,Delphi 7,我继承了一个在Delphi RadStudio2007中实现的GUI,该GUI是针对嵌入式Windows XP的。我看到很多代码如下所示: procedure TStatusForm.Status_refresh; begin if DataModel.CommStatus = COMM_OK then begin if CommStatusOKImage.Visible<>True then CommStatusOKImage.Visible:=True;

我继承了一个在Delphi RadStudio2007中实现的GUI,该GUI是针对嵌入式Windows XP的。我看到很多代码如下所示:

procedure TStatusForm.Status_refresh;

begin
    if DataModel.CommStatus = COMM_OK then begin
        if CommStatusOKImage.Visible<>True then CommStatusOKImage.Visible:=True;
        if CommStatusErrorImage.Visible<>False then CommStatusErrorImage.Visible:=False;
    end else begin
        if CommStatusOKImage.Visible<>False then CommStatusOKImage.Visible:=False;
        if CommStatusErrorImage.Visible<>True then CommStatusErrorImage.Visible:=True;
    end;
end
这显示了在更改之前对可见项的检查,但没有解释首先检查它会提供什么服务

我试图理解为什么最初的开发人员认为只有在要更改可视标志时才有必要设置它,而不是选择以这种方式编码:

procedure TStatusForm.Status_refresh;

begin
    CommStatusOKImage.Visible := DataModel.CommStatus = COMM_OK;
    CommStatusErrorImage.Visible := not  CommStatusOKImage.Visible;
end

是否有性能问题或外观问题(如屏幕闪烁)需要注意?

与许多属性一样,
可见属性设置器在执行任何操作之前检查新值是否与当前值不同。不需要手动检查当前属性值。

正如Remy Lebeau所说,Visible setter已经检查新值是否不同。 例如,在XE中,对于TImage,分配给Visible实际上调用了继承的代码:

procedure TControl.SetVisible(Value: Boolean);
begin
  if FVisible <> Value then
  begin
    VisibleChanging;
    FVisible := Value;
    Perform(CM_VISIBLECHANGED, Ord(Value), 0);
    RequestAlign;
  end;
end;
过程TControl.SetVisible(值:布尔);
开始
如果是可视值,则
开始
可见变化;
FVisible:=值;
执行(CM_VISIBLECHANGED,Ord(值),0);
请求对齐;
结束;
结束;
因此,检查它没有任何好处。然而,您的代码中可能使用了一些第三方或稀有组件——尽管我对此表示怀疑,但它们可能有所不同


您可以自己研究它,使用编辑器中的“查找声明”上下文菜单项(或简单地按住Ctrl键并单击),和/或在打开“使用调试dcus”编译器选项的情况下进入VCL代码。

嗯,我怀疑它会这样做,但在最近的Delphi版本中,可能会出现专门针对表单的问题。在
TCustomForm
中重新声明
Visible
属性,以确保在设置可见性之前执行
OnCreate
事件。它在技术上不会被覆盖,因为
t控件。SetVisible
不是虚拟的,但它具有相同的效果:

procedure TCustomForm.SetVisible(Value: Boolean);
begin
  if fsCreating in FFormState then
    if Value then
      Include(FFormState, fsVisible) else
      Exclude(FFormState, fsVisible)
  else
  begin
    if Value and (Visible <> Value) then SetWindowToMonitor;
    inherited Visible := Value;
  end;
end;

有人想象,如果你是按代码行付费的话,可能会有某种好处。@Downvoter——是的,说真的:-(这就是继承代码的乐趣。唉。@Jay Elston,我肯定原作者想说:
请,请,有人来重构它!
:-)
procedure TCustomForm.SetVisible(Value: Boolean);
begin
  if fsCreating in FFormState then
    if Value then
      Include(FFormState, fsVisible) else
      Exclude(FFormState, fsVisible)
  else
  begin
    if Value and (Visible <> Value) then SetWindowToMonitor;
    inherited Visible := Value;
  end;
end;
procedure TForm1.ShowPaletteButtonClick(Sender: TObject);
begin
  if not Form2.Visible then Form2.Visible := True;
  Form2.BringToFront;
end;