gpt4 book ai didi

delphi - 将 Canvas 添加到 TScrollBox

转载 作者:行者123 更新时间:2023-12-03 15:09:13 26 4
gpt4 key购买 nike

我正在尝试做简单的事情:在 TScrollBox 后代上添加 Canvas 属性。我已阅读this article

但我的 ScrollBox 后代根本无法在 Canvas 上绘制。有人可以告诉我,出了什么问题吗?

  TfrmScrollContainer = class (TScrollBox)
private
FCanvas: TCanvas;
FControlState:TControlState;
procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
protected
procedure Paint;
procedure PaintWindow(DC: HDC); override;
property Canvas: TCanvas read FCanvas;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;

...这是精确的副本,TWincontrol 如何转向 TCustomControl(但它在某个地方失败了)

constructor TfrmScrollContainer.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FCanvas := TControlCanvas.Create;
TControlCanvas(FCanvas).Control := Self;
end;

destructor TfrmScrollContainer.Destroy;
begin
FCanvas.Free;
inherited Destroy;
end;

procedure TfrmScrollContainer.WMPaint(var Message: TWMPaint);
begin
Include(FControlState, csCustomPaint);
inherited;
Exclude(FControlState, csCustomPaint);
end;

procedure TfrmScrollContainer.PaintWindow(DC: HDC);
begin
FCanvas.Lock;
try
FCanvas.Handle := DC;
try
TControlCanvas(FCanvas).UpdateTextFlags;
Paint;
finally
FCanvas.Handle := 0;
end;
finally
FCanvas.Unlock;
end;
end;

procedure TfrmScrollContainer.Paint; // this is not executed (I do not see any ellipse)
begin
Canvas.Brush.Color:=clRed;
Canvas.Ellipse(ClientRect);
end;

谢谢

最佳答案

您没有将 csCustomPaint 包含到 ControlState 中,而是将其包含到您声明的类似名称的字段中。你的字段没有被检查,上升控件对此一无所知。解决办法,更换

procedure TfrmScrollContainer.WMPaint(var Message: TWMPaint);
begin
Include(FControlState, csCustomPaint);
inherited;
Exclude(FControlState, csCustomPaint);
end;

procedure TfrmScrollContainer.WMPaint(var Message: TWMPaint);
begin
ControlState := ControlState + [csCustomPaint];
inherited;
ControlState := ControlState - [csCustomPaint];
end;


或者,您的滚动框可以为您的自定义绘画工作的任何控件提供父级。继承的 WM_PAINT 处理程序检查控件计数,如果不为 0,则调用绘制处理程序,而不是将消息传递给默认处理程序。

关于delphi - 将 Canvas 添加到 TScrollBox,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18244481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com