向 TLabel 添加图形时出现问题

发布于 2024-07-05 08:30:39 字数 335 浏览 7 评论 0原文

我正在尝试使用 Delphi 创建一个从 TLabel 继承的组件,并在 TLabel.Paint 上添加一些自定义图形。 我希望图形位于文本的左侧,因此我覆盖了 GetClientRect:

function TMyComponent.GetClientRect: TRect;
begin
  result := inherited GetClientRect;
  result.Left := 20;
end;

此解决方案有我想解决的主要问题:无法单击控件的“图形区域”,只能单击标签区域。 如果标题为空字符串,则根本无法通过单击来选择设计器中的组件。 有任何想法吗?

I'm trying to create with Delphi a component inherited from TLabel, with some custom graphics added to it on TLabel.Paint. I want the graphics to be on left side of text, so I overrode GetClientRect:

function TMyComponent.GetClientRect: TRect;
begin
  result := inherited GetClientRect;
  result.Left := 20;
end;

This solution has major problem I'd like to solve: It's not possible to click on the "graphics area" of the control, only label area. If the caption is empty string, it's not possible to select the component in designer by clicking it at all. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

埋情葬爱 2024-07-12 08:30:39

您从 TLabel 获得了哪些需要该组件执行的方法/功能?

您也许会更好地制作(例如,TImage)的后代并绘制文本作为其绘制方法的一部分吗?

如果它确实是 TLabel 的后代(包含所有这些),那么我认为您将陷入这个设计时问题,因为当标题为空时 TLabel 不会有这个问题吗?

我会对您得到的其他答案感兴趣! :-)

What methods/functionality are you getting from TLabel that you need this component to do?

Would you perhaps be better making a descendent of (say, TImage) and draw your text as part of it's paint method?

If it's really got to be a TLabel descendant (with all that this entails) then I think you'll be stuck with this design-time issue, as doesn't TLabel have this problem anyway when the caption is empty?

I'll be interested in the other answers you get! :-)

怎樣才叫好 2024-07-12 08:30:39

首先请原谅我的英语不好。
我认为更改组件的 ClientRect 不是一个好主意。 此属性用于许多内部方法和过程,因此您可能会意外更改该组件的功能/操作。

我认为您可以更改写入文本的点(例如,DoDrawText 过程中的 20 像素),并且组件可以响应图形区域中的事件。

procedure TGrlabel.DoDrawText(var Rect: TRect; Flags: Integer);
begin
  Rect.Left := 20;
  inherited;
end;

procedure TGrlabel.Paint;
begin
  inherited;

  Canvas.Brush.Color := clRed;
  Canvas.Pen.Color := clRed;
  Canvas.pen.Width := 3;
  Canvas.MoveTo(5,5);
  Canvas.LineTo(15,8);

end;

First excuse-me for my bad English.
I think it is not a good idea change the ClientRect of the component. This property is used for many internal methods and procedures so you can accidentally change the functionality/operation of that component.

I think that you can change the point to write the text (20 pixels in the DoDrawText procedure -for example-) and the component can respond on events in the graphic area.

procedure TGrlabel.DoDrawText(var Rect: TRect; Flags: Integer);
begin
  Rect.Left := 20;
  inherited;
end;

procedure TGrlabel.Paint;
begin
  inherited;

  Canvas.Brush.Color := clRed;
  Canvas.Pen.Color := clRed;
  Canvas.pen.Width := 3;
  Canvas.MoveTo(5,5);
  Canvas.LineTo(15,8);

end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文