Tuesday, February 8, 2011

Problem with adding graphics to TLabel

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?

  • 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! :-)

    Harriv : Basically I was just thinking "I need label with graphics", and since the text drawing is already there adding only the graphics part was my first bet for this problem.
    robsoft : That makes sense. (sees your comment to @neftali's suggestion) - Glad that have got a solution now!
    From robsoft
  • At first excuse-me for my bad English.
    I Think that is not a good idea Change the ClientRect of the component. This property/information is used for many methods and procedures internals at component and you can change the functionemnt/operation of the component.

    I think that you can change the point to write the text (20 pixels in the DoDrawText procedure -in 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;
    
    Harriv : Yes, that seems to do the trick. Thank you.
    From NeftalĂ­

0 comments:

Post a Comment