【问题标题】:How to test if a TImage has a graphic assigned to it?如何测试 TImage 是否分配有图形?
【发布时间】:2011-02-12 15:55:57
【问题描述】:

在我的程序中,表单上有一个 TImage 组件。

在某些情况下,程序必须测试:

如果“有一个图像分配给 TImage 组件的图片属性”那么 ...

我该怎么做?

【问题讨论】:

    标签: delphi testing components variable-assignment timage


    【解决方案1】:
    if Image1.Picture.Graphic = NIL 
    then ShowMessage("There is no image.")
    else ShowMessage("Image found.");
    

    如果使用位图,您也可以这样做:

    if Image.Picture.Bitmap.Empty then ShowMessage("There is no spoon");
    

    【讨论】:

    • if NOT Image.Picture.Empty then DoStuff,因为 OP 要求仅在有图片时运行 DoStuff
    • @FabricioAraujo - 我只是假设 OP 知道如何调整代码(使用否定)以适应其需要:)
    【解决方案2】:

    迟到总比不到好!
    正确的做法是:

    if Assigned(Image1.Picture.Graphic) then ...

    【讨论】:

      【解决方案3】:

      你没有说,但我假设你在谈论 Delphi。

      可以通过测试检查TImage控件中是否有位图:

      if Image.Picture.Bitmap.Width > 0 then
        // do whatever
      

      【讨论】:

      • 我认为测试对象本身的存在比测试该对象的属性之一要好。
      猜你喜欢
      • 2019-06-22
      • 2021-11-05
      • 1970-01-01
      • 1970-01-01
      • 2016-01-27
      • 1970-01-01
      • 2013-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多