Solved

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

Posted on 1998-06-08
3
255 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
[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
  • 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

Technology Partners: 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

Suggested Solutions

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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