Link to home
Start Free TrialLog in
Avatar of wantime
wantime

asked on

Delphi2010: 2 line Caption for Groupbox

hi all,

the Caption in one of my Groupbox is too lang, i would like to write it in two lines. But i don't know how does it work?

i have tried to place  #13#10 between two strings, but it doesn't work.

for example, if the Caption is "abcdefghijk", i would like to show it as

abcde
fghijk

thanks,

wantime
ASKER CERTIFIED SOLUTION
Avatar of Thommy
Thommy
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mahdi78
Why you do not use TLabel, put TLabel on the GroupBox area and swish wordWrap of TLabel to True
With WordWrap you cannot truly control where the line break occurs!!!

Setting a multi-line Caption for a TLabel (at design-time)
http://delphi.about.com/od/adptips2005/qt/tlabelmultiline.htm

Use #13#10 in the label caption to determine the exact line break position...
If you have Raize components, TRzGroupBox allow you to have 2 lines caption.

It's better than placing TLabel on TGroupBox, because each when you need to move the TGroupBox, you have to adjust TLabel again.
RAIZE Components are not really cheap (379 Euro).

I'd rather move a TLabel with a TGroupBox than spending this money...
>> It's better than placing TLabel on TGroupBox, because each when you need to move the TGroupBox, you have to adjust TLabel again.

How does TGroupBox move, i think you mean when you need to resize TGroupBox,
If that, you should turn the property Anchors>>akRight of TLabel to true to resolve the problem.
@Thommy, I asked if he have them, didn't ask to buy them because this functionality.

also for 399$ you can get more component and better functionally than the VCL ones, I haven't used standard VCL components since I start using Raize components few years ago.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Mahdi78,
Nope I didn't mean that,  I mean in design time, if he wants to change the TGroupBox position.

but if he needs to resize the TgroupBox by setting Anchors>>akRight  it will not work, because it will resize the Tlabel and make the two lines becomes one line again,  just try that ;-)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Here you go
Plain and simple and functional
type
  TGroupBox = class(StdCtrls.TGroupBox)
  private
    function GetNormalHeight: Integer;
    function GetThemedCaptionSize: TSize;
  protected
    procedure AdjustClientRect(var Rect: TRect); override;
    procedure Paint; override;
  end;

implementation

{ TGroupBox }

procedure TGroupBox.AdjustClientRect(var Rect: TRect);
var
  AdjHeight: Integer;
begin
  inherited AdjustClientRect(Rect);
  Canvas.Font := Font;
  if Canvas.TextWidth(Caption) > ClientWidth then
    AdjHeight := (Canvas.TextWidth(Caption) div ClientWidth) * Canvas.TextHeight('0')
  else
    AdjHeight := Canvas.TextHeight('0');

  Inc(Rect.Top, AdjHeight);
  InflateRect(Rect, -1, -1);
  if Ctl3d then
    InflateRect(Rect, -1, -1);
end;

function TGroupBox.GetNormalHeight: Integer;
begin
  Result := Canvas.TextHeight('0');
  if Canvas.TextWidth(Text) > ClientWidth then
    Result := Result * ((Canvas.TextWidth(Text) + ClientWidth) div ClientWidth);
end;

function TGroupBox.GetThemedCaptionSize: TSize;
var
  TextWidth: Integer;
begin
  GetTextExtentPoint32(Canvas.Handle, Text, Length(Text), Result);
  TextWidth := Canvas.TextWidth(Text);
  if TextWidth > ClientWidth then
  begin
    Result.cy := Result.cy * ((TextWidth + ClientWidth) div ClientWidth);
    Result.cx := ClientWidth - 15;
  end;
end;

procedure TGroupBox.Paint;
var
  H: Integer;
  R: TRect;
  Flags: Longint;
  CaptionRect,
  OuterRect: TRect;
  Size: TSize;
  Box: TThemedButton;
  Details: TThemedElementDetails;
begin
  with Canvas do
  begin
    Font := Self.Font;

    if ThemeControl(Self) then
    begin
      if Text <> '' then
      begin
        Size := GetThemedCaptionSize;
        CaptionRect := Rect(0, 0, Size.cx, Size.cy);
        if not UseRightToLeftAlignment then
          OffsetRect(CaptionRect, 8, 0)
        else
          OffsetRect(CaptionRect, Width - 8 - CaptionRect.Right, 0);
      end
      else
        CaptionRect := Rect(0, 0, 0, 0);

      OuterRect := ClientRect;
      OuterRect.Top := (CaptionRect.Bottom - CaptionRect.Top) div 2;
      with CaptionRect do
        ExcludeClipRect(Handle, Left, Top, Right, Bottom);
      if Enabled then
        Box := tbGroupBoxNormal
      else
        Box := tbGroupBoxDisabled;
      Details := ThemeServices.GetElementDetails(Box);
      ThemeServices.DrawElement(Handle, Details, OuterRect);

      SelectClipRgn(Handle, 0);
      if Text <> '' then
        ThemeServices.DrawText(Handle, Details, Text, CaptionRect, DT_LEFT or DT_WORDBREAK, 0);
    end
    else
    begin
      H := GetNormalHeight();
      R := Rect(0, H div 2 - 1, Width, Height);
      if Ctl3D then
      begin
        Inc(R.Left);
        Inc(R.Top);
        Brush.Color := clBtnHighlight;
        FrameRect(R);
        OffsetRect(R, -1, -1);
        Brush.Color := clBtnShadow;
      end else
        Brush.Color := clWindowFrame;
      FrameRect(R);
      if Text <> '' then
      begin
        if not UseRightToLeftAlignment then
          R := Rect(8, 0, 0, H)
        else
          R := Rect(R.Right - Canvas.TextWidth(Text) - 8, 0, 0, H);
        Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_WORDBREAK);
        DrawText(Handle, Text, Length(Text), R, Flags or DT_CALCRECT);
        Brush.Color := Color;
        DrawText(Handle, Text, Length(Text), R, Flags);
      end;
    end;
  end;
end;

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial