Delphi 自定义仪表无法在运行时绘制

Delphi 自定义仪表无法在运行时绘制,delphi,components,Delphi,Components,FPercentDone是0,将其值分配到错误的位置。添加UpdatePercent过程,并在值更改时调用它,可以修复该过程并绘制所有内容 愚蠢的错误,抱歉浪费了你的时间 首先,这是我第一次尝试编写任何类型的组件。属性、方法等都是一个简单的部分,但是我在画布上画画时遇到了麻烦。我肯定这是新手犯的错误,但我根本看不出来。我一直盯着delphi附带的TGauge,因为我正在尝试类似但更简单的东西,它仍然只是一个水平条。我无法让它在运行时绘制进度,这是最奇怪的部分,无论如何,对我来说,我可以看到它在

FPercentDone
0
,将其值分配到错误的位置。添加
UpdatePercent
过程,并在值更改时调用它,可以修复该过程并绘制所有内容

愚蠢的错误,抱歉浪费了你的时间


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

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

相关代码:

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;
每当值更改时都会调用重新绘制(或刷新):min/max/position/borderwidth


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

FPercentDone
was
0
,将其值分配到错误的位置。添加
UpdatePercent
过程,并在值更改时调用它,可以修复该过程并绘制所有内容


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

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