Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Displaying font on the canvas of a TImage

Hello everybody,

I have made programm using a Timage-component devided in 25 rows and 80 columns.
When the user selects menu-item FillBuff the TImage-canvas will be filled with 256 characters.
Only when the user selects menu-item Font and selects a new font in the Change font-dialog
like for example: Comic Sans MS with the size 10, the font will not be displayed correctly on the
canvas!!! Does anyone know how I can correct the problem. And I would like to know how to
make the TImage align Alclient without using property Stretch to True, Because when you do
that the font will be distorted.

I have put the programm on my site:

http://members.home.nl/peterkiers/
(Double click under the Construction-bar on the floppy to download the file).

Greetings,

Peter Kiers
Avatar of 2266180
2266180
Flag of United States of America image

well, the issue in Your case is that you are trying to emulate a text based output. that means you need fixed+sized fonts. you cannot use just any font.
Avatar of Peter Kiers

ASKER

Oke,

What about getting the TImage align = alclient?

P.
sorry, was in a hurry and forgot about that.

well, it's like this: you need to preserve aspect ratio. looking through the image properties you will find one named proportional, whih kind of does that. BUT, there are 2 things to consider: sizeing the image AFTER it was drawn (proportional does its job) OR BEFORE, in which case you need to draw it big yourself. this means, that in the begining you have a specific width height given by the fixed width height of the initial font. say this is W/H. If you change your image size/form size then you will have to draw in an aspect ratio mode. so:
initially you have that
image width = 80*char width
image height = 25*char height
ig you change your image width/height, your char width/height will change as well. BUT. since you don't want distortion, that means that width and height influence themselves.
so the above will be translated as:
80=image width/char width
25=image height/char height
now we have something constant.
making the ratio we get 80/25=image width/char width/image height/char height
which is 80/25=image width*image height/char width*char height

so if say you want to change one of the 4 variables, the otehr 3 will change in such way that the proportion is kept. of course this means you will not be able to make a fullscren out of the form since it depends on the screen resolution and the font used.

I hope the math is correct, I just woke up and I am again in a hurry :) I am sure you will find these somewhere on the net, maybe I'll do some search later when I get to the office.
Avatar of Pierre Cornelius
>>And I would like to know how to make the TImage align Alclient without using property Stretch
>>to True, Because when you do that the font will be distorted.

I downloaded your app. I set the ScrImage1.Align property to alClient and the Stratch property to false and it seems to be working fine... Am I missing the point?

Regards
Pierre
Hi,

I want the TImage resize with the MainForm.

Because Align = alclient doesn't work.
and Stretch to True does, but the font gets distorted.


Peter
That's what I don't understand. On my side the TImage is resizing with the form when setting Align property to alClient...
And if you resize the form does the TImage resize with it.

Peter
Ah, I see. OK, the problem is not that the TImage is not resizing, it is fact resizing with the form. The problem is that the bitmap is not resized, so hook up the following code to the form's OnResize event:

  ScrImage1.Picture.Bitmap.Width:= ClientWidth;
  ScrImage1.Picture.Bitmap.Height:= ClientHeight;
  ShowBuf;

This does not affect the font size or output. Do you want the font size to change according to the form i.e. When form is resized, always have the 80 columns and 25 rows?
yes.
Try this:

procedure TScreenf.FormResize(Sender: TObject);
var lf: LOGFONT;
begin
  ScrImage1.Picture.Bitmap.Width:= ClientWidth;
  ScrImage1.Picture.Bitmap.Height:= ClientHeight;

  GetObject(ScrImage1.Canvas.Font.Handle, SizeOf(lf), @lf);
  lf.lfWidth:= ClientWidth div 80;
  lf.lfHeight:= ClientHeight div 25;
  ScrImage1.Canvas.Font.handle:= CreateFontIndirect(lf);
  FontWidthPix:= lf.lfWidth;
  FontHeightPix:= lf.lfHeight;

  ShowBuf;
end;


You might want to restrict the form resizing to multiples of 80 pixels along the width and 25 along the height, but try the above first and get back to me...


Regards
Pierre
Hi, thank you very much for the response.

I will get back to you tommorrow, because its getting late (23:46).

Greetings,

Peter kiers
thank you for the example. Now where getting somewhere,

Only it's not 100% because when I pull the form to resize
the font doesn't fill the whole client area of the MainForm.

Peter
ASKER CERTIFIED SOLUTION
Avatar of Pierre Cornelius
Pierre Cornelius
Flag of South Africa 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
P.S.: You don't need this:

p:= ScrImage1.ScreenToClient(Mouse.CursorPos);
I have declared in the section Private:

 procedure WMSizing(var msg: TMessage);

But I get message:

[Hint] Main.pas(60): Private symbol 'WMSizing' declared but never used


Peter.

PierreC, I need your help.

P.
Sorry. Should be under private as:

procedure WMSizing(var msg: TMessage); message WM_SIZING;