?
Solved

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

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month14 days, 20 hours left to enroll

770 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