Solved

Get the position of a word in a line written on a canvas?

Posted on 1998-06-08
3
252 Views
Last Modified: 2010-04-04
I have a function which writes a line onto a canvas. This line should fit the entire width of my canvas so that I call SetTextJustification API function which "distributes the specified extra space evenly among the specified "number of spaces".

But how can I retrieve the X, Y position of the word - say - 'sample' on my canvas ?

Any help is greatly appreciated.

Greetings from Hungary,
Levente
slv@mcse.hu

----------------------------------------------------------------------------
-----------------------
var Text  : PChar;
    TextW : integer;   { TextWidth }
begin
   Text:=StrAlloc(128);
   Text:='This is a sample text for SetTextJustification.';

   { TextWidth}
   TextW:=LoWord( GetTextExtent(Image1.Canvas.Handle, Text, StrLen(Text)));

   SetTextJustification( Image1.Canvas.Handle, Image1.Width - TextW, 6 );
                       
   TextOut(Image1.Canvas.Handle, 0, 0, Text, StrLen(Text) );
   StrDispose(Text);
end;
----------------------------------------------------------------------------
----------------------------
0
Comment
Question by:Levente
  • 2
3 Comments
 

Author Comment

by:Levente
ID: 1351562
Edited text of question
0
 
LVL 5

Accepted Solution

by:
inter earned 200 total points
ID: 1351563
Hi,
The following may solve the problem: Assuming the text contains the string above and all the other values are computed

var
  SPos : integer; //at the end this will be the X position of sample
  TStr   : PChar;
  ...
begin
  ....
  TStr := AllocStr(128);
  // Find the character position of 'sample', here I do not understand why you use
  // null terminated strings and API calls. The width of the strings is given simply
  // by Image1.Canvas.TextWidth(Text); Any way
  SPos := StrLen(Text) - StrLen(StrPos(Text, 'sample'));
  // copy string upto sample
  StrLCopy(TStr, Text, SPos);
  // measure the string, this gives unadjusted X to sample
  SPos := Image1.Canvas.TextWidth(StrPas(SPos));
  // now add the extra spacing introduced by (Image1.Width - TextW)/ 6 (LOOK ABOVE)
  // one routine should count the number of SPACES for X pos be correct
  // Getbreakcount count the number of specified characters in you string
  SPos := SPos + ((Image1.Width - TextW) div 6)*GetBreakCount(TStr, ' ');
  // OK NOW YOU HAVE X POS OF SAMPLE IN SPOS
  StrDispose(TStr);
.
end;

function GetBreakCount( S : PChar; C:char):integer;
begin
   Result := 0;
  while S^ <> #0 do
  begin
      if S^ = C then Inc(Result);
      Inc(S);
  end;
end;

Note that your problem is ambigious. for example what if we do if the string contains the same word twice. So in most cases the user should know what he is doing

May this helps
Regards, Igor
0
 

Author Comment

by:Levente
ID: 1351564
Thanx for your answer. It exactly does what I needed.
(I used API calls because they're significantly faster (>50 %)
than Delphi functions - accodring to MY measurements.)

Levente
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection between libmysql.dll and MySQL Versions 7 99
find a node in VST 2 69
Firemonkey android show image from resource ? 1 42
enhance the following code 3 32
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

825 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