Solved

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

Posted on 1998-06-08
3
248 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
Comment Utility
Edited text of question
0
 
LVL 5

Accepted Solution

by:
inter earned 200 total points
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now