【问题标题】:Why doesn't my paintbox image appear when the form activates为什么表单激活时我的画框图像没有出现
【发布时间】:2017-03-29 17:00:24
【问题描述】:

我正在尝试编写模拟 VU 表。我使用 VU 表的位图并将指针画在位图上。我正在使用轨迹栏,它是 onChange 事件来测试仪表:

procedure TForm1.TrackBar1Change(Sender: TObject);
var
  angle : integer;
  x,y : integer;
  Peaked : boolean;
begin
   Angle := 120 - Round(sTrackBar1.Position / sTrackBar1.Max * 100 )+20;
   Peaked := Angle < PeakVol;
   if Peaked then
    Buffer.Picture := VUImagePeaked.Picture
   else
    buffer.picture := VUImage.Picture;
   buffer.Picture.Bitmap.Canvas.Pen.Color := clSilver;
   buffer.Picture.Bitmap.Canvas.Pen.Width:=2;
   buffer.Canvas.MoveTo(pivot.x,Pivot.y);
   x := 150 + Round(Cos(DegToRad(Angle)) * NeedleLen);
   y := PaintBox1.Height - Round(Sin(DegToRad(Angle)) *NeedleLen);
   buffer.Canvas.LineTo(x,y);
   PaintBox1.Canvas.Draw(0,0,buffer.Picture.Bitmap)
end;

似乎可以工作,但我无法在程序启动时显示仪表的位图。我什至求助于将上面的代码复制到 Form.Create 和 Form.Activate 事件处理程序中,但没有任何乐趣。我创建了一个按钮并添加了以下代码来触发轨迹栏的 onChange 事件处理程序。这可以工作并显示仪表。

procedure TForm1.Button1Click(Sender: TObject);
begin
  TrackBar1.Position := 1;
end;

当我将它复制到 Form.Activate 处理程序时,它没有。谁能告诉我我做错了什么?我正在使用 Delphi Berlin 入门版。谢谢

【问题讨论】:

  • PaintBox1.Canvas.Draw(0,0,buffer.Picture.Bitmap) - 这需要移动到TPaintBox.OnPaint 事件。

标签: delphi


【解决方案1】:

必须使用其OnPaint 事件绘制TPaintBox。每次 Windows 需要您重绘控件时都会触发此事件。您不能在任何时候简单地绘制到控件的Canvas,因为它只会在下次重新绘制控件时被绘制。

在您的特定情况下,您不需要将整个代码块移动到OnPaint 事件处理程序。相反,您只需要:

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  PaintBox1.Canvas.Draw(0,0,buffer.Picture.Bitmap)
end;

【讨论】:

  • 感谢你们这么快回复我。使用 onPaint 事件有效。
猜你喜欢
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-27
  • 1970-01-01
相关资源
最近更新 更多