Solved

Delphi2010: 2 line Caption for Groupbox

Posted on 2011-03-22
12
999 Views
Last Modified: 2012-05-11
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
Comment
Question by:wantime
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 19

Accepted Solution

by:
Thommy earned 125 total points
ID: 35190973
#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
 
LVL 9

Expert Comment

by:Mahdi78
ID: 35191422
Why you do not use TLabel, put TLabel on the GroupBox area and swish wordWrap of TLabel to True
0
 
LVL 19

Expert Comment

by:Thommy
ID: 35192543
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
 
LVL 22

Expert Comment

by:mnasman
ID: 35192818
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
 
LVL 19

Expert Comment

by:Thommy
ID: 35192855
RAIZE Components are not really cheap (379 Euro).

I'd rather move a TLabel with a TGroupBox than spending this money...
0
 
LVL 9

Expert Comment

by:Mahdi78
ID: 35192914
>> 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 22

Expert Comment

by:mnasman
ID: 35192977
@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
 
LVL 36

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 125 total points
ID: 35192993
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
 
LVL 22

Expert Comment

by:mnasman
ID: 35193048
@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
 
LVL 9

Assisted Solution

by:Mahdi78
Mahdi78 earned 125 total points
ID: 35193149
@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
 
LVL 32

Expert Comment

by:ewangoya
ID: 35195214

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
 
LVL 32

Assisted Solution

by:ewangoya
ewangoya earned 125 total points
ID: 35195224

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

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now