[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1137
  • Last Modified:

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
0
wantime
Asked:
wantime
  • 3
  • 3
  • 3
  • +2
4 Solutions
 
ThommyCommented:
#13#10 works with a TLabel, but not with TGroupBox

The only way I can think of is to place a TLabel over the TGroupBox caption...
0
 
Mahdi78Commented:
Why you do not use TLabel, put TLabel on the GroupBox area and swish wordWrap of TLabel to True
0
 
ThommyCommented:
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...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mohammed NasmanSoftware DeveloperCommented:
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.
0
 
ThommyCommented:
RAIZE Components are not really cheap (379 Euro).

I'd rather move a TLabel with a TGroupBox than spending this money...
0
 
Mahdi78Commented:
>> 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.
0
 
Mohammed NasmanSoftware DeveloperCommented:
@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.
0
 
Geert GruwezOracle dbaCommented:
you can simulate it with
TPanel + TBevel + TLabel

the secret is the margins and alignwith margins
 Screenshot simulation running
.pas
 
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxContainer, cxEdit, cxGroupBox, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Bevel1: TBevel;
    Label1: TLabel;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    RadioButton3: TRadioButton;
    RadioButton4: TRadioButton;
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Label1.Caption := 'Test1234567890 1234567890 1234567890 1234567890'#10#13
    + 'Test line 2 1234567890 1234567890 1234567890';
  Bevel1.Margins.Top := Label1.Height div 2;
end;

end.

Open in new window


.dfm
 
object Form1: TForm1
  Left = 245
  Top = 94
  Caption = 'Form1'
  ClientHeight = 364
  ClientWidth = 579
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 8
    Top = 24
    Width = 497
    Height = 273
    Caption = 'Panel1'
    TabOrder = 0
    object Bevel1: TBevel
      AlignWithMargins = True
      Left = 4
      Top = 11
      Width = 489
      Height = 258
      Margins.Top = 10
      Align = alClient
      Shape = bsFrame
      ExplicitLeft = -28
    end
    object Label1: TLabel
      AlignWithMargins = True
      Left = 14
      Top = 5
      Width = 37
      Height = 13
      Caption = ' Label1 '
      Transparent = False
    end
    object RadioButton1: TRadioButton
      Left = 32
      Top = 40
      Width = 113
      Height = 17
      Caption = 'RadioButton1'
      Checked = True
      TabOrder = 0
      TabStop = True
    end
    object RadioButton2: TRadioButton
      Left = 32
      Top = 72
      Width = 113
      Height = 17
      Caption = 'RadioButton2'
      TabOrder = 1
    end
    object RadioButton3: TRadioButton
      Left = 32
      Top = 104
      Width = 113
      Height = 17
      Caption = 'RadioButton3'
      TabOrder = 2
    end
    object RadioButton4: TRadioButton
      Left = 32
      Top = 136
      Width = 113
      Height = 17
      Caption = 'RadioButton4'
      TabOrder = 3
    end
  end
end

Open in new window

0
 
Mohammed NasmanSoftware DeveloperCommented:
@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 ;-)
0
 
Mahdi78Commented:
@mnasman

He should drop TLabel on TGroupBox to make it as child for it, then the TLabel (Child) will move when the TGroupBox (parent) move.

>> 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 ;-)

The author said "the Caption in one of my Groupbox is too lang" so he is looking for solution to display his long caption, he is not looking for TGroupBox caption with two lines :)
0
 
Ephraim WangoyaCommented:

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

0
 
Ephraim WangoyaCommented:

And here is an example to go
Unit3.dfm
Unit3.pas
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now