2

FPercentDone0,在错误的地方分配了它的价值。添加UpdatePercent过程并在值更改时调用它可以修复它并绘制所有内容。

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


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

没有任何代码粘贴,因为它类似于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值或其正确使用,所以复制粘贴和调整代码是行不通的。

4

1 回答 1

1

FPercentDone was 0, was assigning it's value in the wrong place. Adding UpdatePercent procedure and calling it when a value changes fixes it and everything gets drawn.

Dumb mistake, sorry for wasting your time.

于 2012-05-01T11:05:51.700 回答