【问题标题】:using image inside in gauge insteade of solid color or gradient在仪表内使用图像而不是纯色或渐变
【发布时间】:2015-02-20 21:50:51
【问题描述】:

在一个旧的应用程序中,我有仪表,实际上有两个,现在因为我不知道如何用具有动画和内容的加载栏替换它们,所以我想让仪表内部有图像而不是前景和背景颜色。我看到他们正在使用TColor。我可以用TImage 以某种方式替换它吗?如果是的话怎么办? (因为我是从目录中获取颜色的,所以我不必每次都在 Delphi 中构建相同的代码,而是从我为编译更新程序应用程序而制作的应用程序,更新程序应用程序就是使用仪表的应用程序。)

procedure LoadProgressParam(list: TListFile; Pr: TGauge; name: string);
begin
  Pr.Top:=StrToInt(list.GetKeyValue('progress',name+'_top'));
  Pr.Left:=StrToInt(list.GetKeyValue('progress',name+'_left'));
  Pr.Width:=StrToInt(list.GetKeyValue('progress',name+'_width'));
  Pr.Height:=StrToInt(list.GetKeyValue('progress',name+'_height'));
  Pr.BackColor:=StringToColor(list.GetKeyValue('progress',name+'_bg'));
  Pr.ForeColor:=StringToColor(list.GetKeyValue('progress',name+'_fg'));
end;

有什么我可以在这里改变的吗?

或者我必须编写另一个代码来完成它?

抱歉,如果我使用了错误的代码片段,但我无法找到如何使用 c++ 代码

【问题讨论】:

  • 嗯,你刚刚用 Embacadero 的 (c) 发布了一个完整的单元。你确定你可以这样做吗?
  • 量规?这是什么,1990 年代 VB 回忆周?
  • 我删除了您发布的 Embarcadero 版权代码,因为它违反了他们的版权和许可条款。除非您得到他们的许可将其发布在公共场所,否则不要这样做。此外,您的帖子中几乎每个句子都没有必要使用 bolditalic。花一些时间格式化并尝试写得更清楚,少用句子、断句和标点符号。
  • TImage 用于静态图像。这对您来说可能是个糟糕的选择。

标签: image delphi delphi-xe3 gauge


【解决方案1】:

你能用 TImage 代替 TGauge 吗?是的,你可以。

现在,为了让 TImage 像 TGauge 一样工作,您需要分两步渲染它的图像。第一步渲染免费图像,第二步渲染前景图像。

这是一个小示例,您可以使用它最终自己实现。在其中,我使用 TTrack 栏来控制填充百分比,并在其 OnChange 事件中调用 CopyRect 方法将部分 ForeGround 和 BackGround 位图复制到最终图像。

procedure TForm1.TrackBar1Change(Sender: TObject);
var FillRect: TRect;
    ForeGroundBitmap: TBitmap;
    BackGroundBitmap: TBitmap;
begin
  //Just a quick code to fill up ForeGround and BackGround image canvases with
  //single color
  //You would prbobably want to load images instead
  BackGroundBitmap := TBitmap.Create;
  BackGroundBitmap.Width := Image1.Width;
  BackGroundBitmap.Height := Image1.Height;
  BackGroundBitmap.Canvas.Brush.Style := bsSolid;
  BackGroundBitmap.Canvas.Brush.Color := clGreen;
  BackGroundBitmap.Canvas.FillRect(BackGroundBitmap.Canvas.ClipRect);
  ForeGroundBitmap := TBitmap.Create;
  ForeGroundBitmap.Width := Image1.Width;
  ForeGroundBitmap.Height := Image1.Height;
  ForeGroundBitmap.Canvas.Brush.Style := bsSolid;
  ForeGroundBitmap.Canvas.Brush.Color := clRed;
  ForeGroundBitmap.Canvas.FillRect(ForeGroundBitmap.Canvas.ClipRect);

  //Background
  FillRect.Left := 0;
  FillRect.Width := Image1.Width;
  //Here we calculate the top of FillRectangle that we will use for bacground rendering
  FillRect.Top := Image1.Height - (Image1.Height * TTrackBar(sender).Position div TTRackBar(sender).Max);
  FillRect.Bottom := 0;
  Image1.Canvas.CopyRect(FillRect,BackGroundBitmap.Canvas,FillRect);

  //Foreground

  FillRect.Top := Image1.Height;
  //Here we calculate the botom of FillRectangle that we will use for foreground rendering
  FillRect.Bottom := Image1.Height - (Image1.Height * TTrackBar(sender).Position div TTRackBar(sender).Max);
  Image1.Canvas.CopyRect(FillRect,ForegroundBitmap.Canvas,FillRect);

  //Since I'm using local Bitmaps here I need to free them to avoid memory leaks
  ForeGroundBitmap.Free;
  BackGroundBitmap.Free;
end;

当然,您会想要从 TImage 制作一个派生组件以获得最终结果。

【讨论】:

  • “自然你会想要从 TImage 制作一个派生组件以获得最终结果”。甚至可能是 TGraphicControl
  • 非常感谢您,我将对其进行测试并返回结果
  • 我不赞成这种方法。使用油漆盒进行这样的定制动态绘画。或者自定义控件。但绝对不是 TImage。
  • 所有建议都很棒!我尝试了所有,现在我只需要选择哪个更完美 TgraphicControl 也很有趣!油漆盒和上面的代码是迄今为止最好的!谢谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-19
  • 2013-05-14
  • 1970-01-01
  • 1970-01-01
  • 2013-07-29
  • 2015-03-22
相关资源
最近更新 更多