?
Solved

Delphi2010: 2 line Caption for Groupbox

Posted on 2011-03-22
12
Medium Priority
?
1,105 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +2
12 Comments
 
LVL 19

Accepted Solution

by:
Thommy earned 500 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 22

Expert Comment

by:Mohammed Nasman
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
 
LVL 22

Expert Comment

by:Mohammed Nasman
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 38

Assisted Solution

by:Geert Gruwez
Geert Gruwez earned 500 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:Mohammed Nasman
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 500 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:Ephraim Wangoya
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:Ephraim Wangoya
Ephraim Wangoya earned 500 total points
ID: 35195224

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

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

764 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