Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 1998-06-08
3
Medium Priority
?
262 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 800 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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