【问题标题】:Custom gauge fails to draw at runtime自定义仪表在运行时无法绘制
【发布时间】:2012-05-09 15:59:12
【问题描述】:

FPercentDone0,在错误的位置分配了它的值。添加UpdatePercent 过程并在值更改时调用它会修复它并绘制所有内容。

愚蠢的错误,很抱歉浪费您的时间。


首先,这是我第一次尝试编写任何类型的组件。属性、方法等是一个简单的部分,但是我在画布上绘图时遇到了困难。我确信这是一些新手错误,但我根本看不到它。我一直盯着 delphi 中包含的TGauge,因为我正在尝试类似但更简单的东西,它仍然只是一个单杠。我无法让它在运行时绘制进度,这是最奇怪的部分,无论如何,对我来说,我可以在设计时看到它工作,但在我运行它时却看不到......我做到了至少获得正确的背景颜色,但没有进度条。

没有任何代码粘贴,因为它无论如何都类似于TGauge。我有两个TBitmap's,一个用于背景,另一个用于进度条本身,我用背景颜色填充一个,将其绘制到组件画布上,如果有边框偏移第二个的原点并减小其矩形,则绘制它使用进度颜色并将其绘制到画布上......对我来说似乎很简单,但我做错了什么?

相关代码:

type
  TCustomGaugeComp = class(TGraphicControl)
  private
    FMaxValue, FMinValue, FCurValue: DWord;
    FFillBackColor, FFillForeColor: TColor;
    FPercentDone: Real;
    FBorderStyle: TBorderStyle;
    FBorderWidth: Integer;
    procedure SetMaxValue(Value: DWord);
    procedure SetMinValue(Value: DWord);
    procedure SetProgress(Value: DWord);
    procedure SetFillBackColor(Value: TColor);
    procedure SetFillForeColor(Value: TColor);
    procedure SetBorderStyle(Value: TBorderStyle);
    function GetPercentDone: String;
    procedure SetBorderWidth(Value: integer);
  protected
    procedure Paint; override;
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Align;
    property BorderStyle: TBorderStyle read FBorderStyle write SetBorderStyle default bsSingle;
    property BorderWidth: Integer read FBorderWidth write SetBorderWidth default 1;
    property Constraints;
    property Enabled;
    property Font;
    property FillForeColor: TColor read FFillForeColor write SetFillForeColor default clBlack;
    property FillBackColor: TColor read FFillBackColor write SetFillBackColor default clWhite;
    property MinValue: DWord read FMinValue write SetMinValue default 0;
    property MaxValue: DWord read FMaxValue write SetMaxValue default 100;
    property Progress: DWord read FCurValue write SetProgress default 0;
    property PercentDone: String read GetPercentDone;
    property Visible;
  end;


procedure TCustomGaugeComp.Paint;
var
  Background, Progress: TBitMap;
begin
  with Canvas do
  begin
    Background := TBitMap.Create;
    try
      Background.Height := Height;
      Background.Width := Width;
      Background.Canvas.Brush.Color := FFillBackColor;
      Background.Canvas.Brush.Style := bsSolid;
      Background.Canvas.FillRect(ClientRect);
      Progress := TBitMap.Create;
      try
        Progress.Height := Height;
        Progress.Width := Width;
        if FBorderStyle = bsSingle then
        begin
          Progress.Height := Progress.Height - BorderWidth*2;
          Progress.Width := Progress.Width - BorderWidth*2;
        end;
        Progress.Width := trunc(Progress.Width*FPercentDone/100);
        Progress.Canvas.Brush.Color := FFillForeColor;
        Progress.Canvas.FillRect(Rect(0,0,Progress.Width,Progress.Height));
        Background.Canvas.Draw(BorderWidth,BorderWidth,Progress);
      finally
        Progress.Free;
      end;
      Draw(0,0,Background);
    finally
      Background.Free;
    end;
  end;
end;

RePaint(或 Refresh)在值更改时调用:min/max/position/borderwidth。

事实上,它在设计时也表现不完美,有时会绘制进度,有时甚至根本没有绘制,直到我只是打开对象检查器,然后用我的鼠标在那里.. .TGauge 过度使用CopyMode,我刚开始使用,我还不太了解CopyMode 的值或它的正确用法,所以复制粘贴和调整代码是行不通的。

【问题讨论】:

  • 我对 Delphi 一无所知,但我可以这样说:如果您向我们展示代码中最相关的部分,您更有可能获得有用的响应。如果它与 Delphi 附带的东西非常相似并且已知可以工作,那么您可能会发现以下策略很有用:从 TGauge 本身开始,逐渐修改它以使其更接近您的代码,然后查看它在哪里中断。或者用你的代码说明并逐渐TGaugeify 它,看看它从哪里开始工作。或者在中间选择一个点,看看它是否有效,然后继续细分。
  • 我们确实需要代码来回答您的问题。你重写了paint方法吗?
  • 要详细说明 NGLN 的评论,声明应该类似于 procedure Paint; override; 您不一定要自己调用 Paint,让 Windows 为您调用。相反,调用 Invalidate; 会告诉 Windows 在有机会时调用 Paint。
  • @NGLN 是的,paint 已被覆盖。添加了代码。
  • @Raith - 在Progress.Width := trunc(.. 行上放置一个断点并将鼠标悬停在“FPercentDone”上。如果为“0”,则预计您看不到进度条。

标签: delphi components


【解决方案1】:

FPercentDone0,在错误的位置分配了它的值。添加UpdatePercent 过程并在值更改时调用它会修复它并绘制所有内容。

愚蠢的错误,很抱歉浪费您的时间。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-27
    • 2016-03-18
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多