Board index » delphi » Pixel width of string

Pixel width of string

Hello there,

I'm writing a component that automatically can adjusts its own and the
sub-components with to fit a string. The sub-components are panels and
the string is shown as there captions.
If I remeber correctly there should be a function that resturns the
width in pixels of a string. Does anybody knows what function I mean, or
maybe you have some other sollution?
Thanks for helping me.

Darius Blaszijk

 

Re:Pixel width of string


:Darius Blaszijk <DBlasz...@ZonNet.nl> schrieb in im Newsbeitrag: 38842DCB.EC89B...@ZonNet.nl...
: Hello there,
:
: I'm writing a component that automatically can adjusts its own and the
: sub-components with to fit a string. The sub-components are panels and
: the string is shown as there captions.
: If I remeber correctly there should be a function that resturns the
: width in pixels of a string. Does anybody knows what function I mean, or
: maybe you have some other sollution?
: Thanks for helping me.
:
: Darius Blaszijk
:

It's a method of TCanvas called TextWidth.

Hth,
Matthias.

Re:Pixel width of string


Quote
Darius Blaszijk <DBlasz...@ZonNet.nl> wrote in message

news:38842DCB.EC89BE4D@ZonNet.nl...

Quote
> Hello there,

> I'm writing a component that automatically can adjusts its own and the
> sub-components with to fit a string. The sub-components are panels and
> the string is shown as there captions.
> If I remeber correctly there should be a function that resturns the
> width in pixels of a string. Does anybody knows what function I mean, or
> maybe you have some other sollution?
> Thanks for helping me.

> Darius Blaszijk

Have a look at TextWidth(...). Ist a function of TCanvas!

HTH Martin

Re:Pixel width of string


Tanks, but... TCanvas is not a property of TPanel. It is of TCustomControl,
parent of TCustomPanel, parent of TPanel. How do I handle this??
Quote
"Martin Wei?" wrote:

> Have a look at TextWidth(...). Ist a function of TCanvas!

> HTH Martin

Re:Pixel width of string


Hello!

Quote
Darius Blaszijk <DBlasz...@ZonNet.nl> wrote in message

news:3884E929.50EE6A1F@ZonNet.nl...

Quote
> Tanks, but... TCanvas is not a property of TPanel. It is of
TCustomControl,
> parent of TCustomPanel, parent of TPanel. How do I handle this??

> "Martin Wei?" wrote:

> > Have a look at TextWidth(...). Ist a function of TCanvas!

> > HTH Martin

Sorry, my answer was to short. If there is no Canvas property you have to
create your own canvas.
There is an example in the OH how to do this. Look at TCanvas.Create -
example. There it is shown how to create and use own created canvas.

HTH Martin

Re:Pixel width of string


In article <3884E929.50EE6...@ZonNet.nl>, Darius Blaszijk <DBlasz...@ZonNet.nl>
writes:

Quote
>Tanks, but... TCanvas is not a property of TPanel. It is of TCustomControl,
>parent of TCustomPanel, parent of TPanel. How do I handle this??

It is a Protected property of TCustomControl. It is _in_ the descendant but the
compiler does not allow access to it. But Delphi allows access to such a
property in a another descendant class which is declared in the same unit
(that's how dscendants can expose such properties).

So either make a descendant of TPanel (a lot of code and not necessarily a
design-time visual component) or declare a descendant of TPanel and type-cast
your TPanel to that descendant.

ie

type
  TCanvasPanel = class(TPanel); // I usually name it for the purpose I declare
it

then

PixelWidth := TCanvasPanel(MyPanel).Canvas.TextWidth('ABCDEF');

Alan Lloyd
alangll...@aol.com

Re:Pixel width of string


On Tue, 18 Jan 2000 23:29:00 +0100, Darius Blaszijk <DBlasz...@ZonNet.nl>
wrote:

Quote
>Tanks, but... TCanvas is not a property of TPanel. It is of TCustomControl,
>parent of TCustomPanel, parent of TPanel. How do I handle this??

The TPanel component does indeed have a Canvas property. It is just that it
is never surfaced. That is, it is inherited from an ancestor and scoped
Protected, so it is never seen in the Object Inspector. But, with a little
sleight of hand, you can deftly get access to it.

The first step is to create a hack class that descends from TPanel and
raises the scope of the Canvas property to Public.

  type
    THackPanel = class(TPanel)
    public
      Canvas: TCanvas;
    end;

This can be in the same Interface section as the owning form.

Then, typecast a TPanel object as a THackPanel to access its Canvas
property.

  procedure TForm1.ButtonsClick(Sender: TObject);
  begin
    (Panel1 as THackPanel).Font.Name := (Sender as TButton).Caption;
  end;

_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
Steve Koterski              "Health nuts are going to feel stupid someday,
Felton, CA                  lying in hospitals dying of nothing."
                                                              -- Redd Foxx

Re:Pixel width of string


You can also call GetTextExtentPoint32, declared in Windows.pas.

Robin Gerrets
R.Gerr...@student.nyenrode.nl

"Martin Wei?" <d...@ping.at> schreef in bericht
news:863q24$gn6$1@news.netway.at...
| Hello!
|
| Darius Blaszijk <DBlasz...@ZonNet.nl> wrote in message
| news:3884E929.50EE6A1F@ZonNet.nl...
| > Tanks, but... TCanvas is not a property of TPanel. It is of
| TCustomControl,
| > parent of TCustomPanel, parent of TPanel. How do I handle this??
| >
| > "Martin Wei?" wrote:
| >
| > >
| > > Have a look at TextWidth(...). Ist a function of TCanvas!
| > >
| > > HTH Martin
|
| Sorry, my answer was to short. If there is no Canvas property you have to
| create your own canvas.
| There is an example in the OH how to do this. Look at TCanvas.Create -
| example. There it is shown how to create and use own created canvas.
|
| HTH Martin
|
|

Re:Pixel width of string


You know, often if you ask yourself a question or rephrase the question it
will lead you to the
answer?

How can I get the Width of some Text?
How can I get the TextWidth?

Returns the width, in pixels, of a string rendered in the current font.

function TextWidth(const Text: string): Integer;

Description

Use TextWdith to determine the length a string will
occupy in the image. TextWidth indicates whether a
given string will fit in the available space. Other
graphical elements in the image such as lines, or
additional strings can be positioned to accommodate
the width of the text.

TextWidth returns the same value as TextExtent(Text).cx.

This project requires a TPageControl and several TTabPages.
To add new pages, right click the TPageControl and select
"New Page".
During the form creation the width and height of all tabs
on the Page Control becomes double the largest previous value.
So if there are three tabs with captions that are 12, 23 and
19 characters long, the TabWidth would be the number of pixels
required to fit 46 characters.

procedure TForm1.FormCreate(Sender: TObject);
var
  i: integer;
begin
  with PageControl1 do
  begin
    for i := 0 to PageCount - 1 do
    begin
      if (Canvas.TextWidth(Pages[i].Caption) * 2) > TabWidth then
        TabWidth := Canvas.TextWidth(Pages[i].Caption) * 2;
      if (Canvas.TextHeight(Pages[i].Caption) * 2) > TabHeight then
        TabHeight := Canvas.TextHeight(Pages[i].Caption) * 2;

    end;
end;
end;

Quote
Darius Blaszijk <DBlasz...@ZonNet.nl> wrote in message

news:38842DCB.EC89BE4D@ZonNet.nl...
Quote
> Hello there,

> I'm writing a component that automatically can adjusts its own and the
> sub-components with to fit a string. The sub-components are panels and
> the string is shown as there captions.
> If I remeber correctly there should be a function that resturns the
> width in pixels of a string. Does anybody knows what function I mean, or
> maybe you have some other sollution?
> Thanks for helping me.

> Darius Blaszijk

Re:Pixel width of string


This will return the width of a string in pixels.

"AFont" is the font you are using
"St" is the string you are measuring.

function TextWidth(AFont:TFont; St:String):Integer;
var
  TW:Integer;
  Bitmap:TBitmap;
begin
  Bitmap:=TBitmap.Create;
  try
    Bitmap.Canvas.Font.Assign(AFont);
    TW:=Bitmap.Canvas.TextWidth(St);
  finally
    Bitmap.Free;
  end;
  Result:=TW;
end;

Andy

Quote
Darius Blaszijk wrote in message <38842DCB.EC89B...@ZonNet.nl>...
>Hello there,

>I'm writing a component that automatically can adjusts its own and the
>sub-components with to fit a string. The sub-components are panels and
>the string is shown as there captions.
>If I remeber correctly there should be a function that resturns the
>width in pixels of a string. Does anybody knows what function I mean, or
>maybe you have some other sollution?
>Thanks for helping me.

>Darius Blaszijk

Other Threads