make a Delphi TPanel caption wrap
Asked Answered
C

2

7

Is there a way to wrap the text in a TPanel.Caption in Delphi (in my case Delphi 6)?

Code answered 13/8, 2009 at 20:54 Comment(0)
M
10

Not by default, I'm afraid. As you can see from the sourcecode for TPanel, the text is drawn by the DrawText( )-windows API:

procedure TCustomPanel.Paint;
{snip}
begin
  {snip}
  Flags := DT_EXPANDTABS or DT_SINGLELINE or
    VerticalAlignments[FVerticalAlignment] or Alignments[FAlignment];
  Flags := DrawTextBiDiModeFlags(Flags);
  DrawText(Handle, PChar(Caption), -1, Rect, Flags);
end;

You can either derive and override the Paint-method, or you could just use a label instead.

Mizell answered 13/8, 2009 at 21:11 Comment(2)
+1 for suggesting using a label. Just drop it inside the TPanel (if you want the borders) and set the anchors.Assai
+1 for showing that many questions can be answered easily by just having a quick look at the VCL source code.Sentimentalism
R
0

Here is code that actually works:

const
  Alignments: array[TAlignment] of Longint = (DT_LEFT, DT_RIGHT, DT_CENTER);
  VerticalAlignments: array[TVerticalAlignment] of Longint = (DT_TOP, DT_BOTTOM, DT_VCENTER);
var
  Rect: TRect;

   ... 
    
   if ShowCaption AND (Caption <> '') then
    begin
     Rect := GetClientRect;
     Canvas.Brush.Style := bsClear;
     Canvas.Font := Self.Font;
     Flags := DT_NOPREFIX OR
              DT_WORDBREAK OR
              VerticalAlignments[VerticalAlignment] OR
              Alignments[Alignment];
     Flags := DrawTextBiDiModeFlags(Flags);
     DrawText(Canvas.Handle, PChar(Caption), Length(Caption), Rect, Flags);
    end;
Rede answered 6/5, 2021 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.