Solved

How to get the word under the mouse cursor in a label component

Posted on 1997-12-11
6
1,459 Views
Last Modified: 2009-07-29
How to get the word under the mouse cursor in a multiline label component ?
(Not in a RichEdit or Memo/Edit component)

I want to get the word under the mouse cursor in a multiline label component (with proportional font) ?
Ex. I want to click on a word in the label, and get in a property the whole word.

The code should also work with any text drawn on a bitmap or on a paintbox, on a single line or on a rect with drawtext.


Thanks for the help.

0
Comment
Question by:jpdupont
  • 3
  • 2
6 Comments
 

Author Comment

by:jpdupont
ID: 1353352
Adjusted points to 200
0
 
LVL 5

Accepted Solution

by:
JimBob091197 earned 200 total points
ID: 1353353
Hi

The following routine returns the character position that the cursor is over:  (It returns -1 if the cursor isn't over a character.)

function GetCharAtPos(Caption: string; Canvas: TCanvas; ARect: TRect; X, Y: Integer): Integer;
var
  i, LblPos, LineHeight, LineBreakX, TempLblPos: Integer;
  SubCap: string;
  ASize: TSize;
begin
  Result := -1;
  if (Caption = '') then
    Exit;

  GetTextExtentPoint32(Canvas.Handle, PChar(Caption), Length(Caption), ASize);
  LineHeight := ASize.cy;

  LblPos := 1;
  if (Y > LineHeight) then
    begin
      for i := 1 to Y div LineHeight do
        begin
          LineBreakX := 0;
          SubCap := '';
          while (LblPos <= Length(Caption)) and (LineBreakX < WidthOf(ARect)) do
            begin
              SubCap := SubCap + Copy(Caption, LblPos, 1);
              GetTextExtentPoint32(Canvas.Handle, PChar(SubCap), Length(SubCap), ASize);
              LineBreakX := ASize.cx;
              Inc(LblPos);
            end;
          while (LblPos > 1) and (LblPos <= Length(Caption)) and (Caption[LblPos] <> ' ') do
            Dec(LblPos);
        end;
      Inc(LblPos);
    end;

  LineBreakX := 0;
  SubCap := '';
  while (LblPos <= Length(Caption)) and (LineBreakX < X) do
    begin
      SubCap := SubCap + Copy(Caption, LblPos, 1);
      GetTextExtentPoint32(Canvas.Handle, PChar(SubCap), Length(SubCap), ASize);
      LineBreakX := ASize.cx;
      Inc(LblPos);
    end;

  TempLblPos := LblPos;
  while (TempLblPos > 0) and (LineBreakX < WidthOf(ARect)) and (TempLblPos <= Length(Caption)) do
    begin
      SubCap := SubCap + Copy(Caption, TempLblPos, 1);
      GetTextExtentPoint32(Canvas.Handle, PChar(SubCap), Length(SubCap), ASize);
      LineBreakX := ASize.cx;
      if (Caption[TempLblPos] = ' ') then
        TempLblPos := -1
      else
        Inc(TempLblPos);
    end;

  if (TempLblPos > Length(Caption)) and (LineBreakX >= X) then
    TempLblPos := -1;

  if (TempLblPos = -1) then
    Result := LblPos - 1;
end;


This routine uses WidthOf, declared as follows:

function WidthOf(ARect: TRect): Integer;
begin
  Result := ARect.Right - ARect.Left;
end;


To get the word under the cursor, call the following:  (This routine uses GetCharAtPos declared above.)

function GetWordAtPos(Caption: string; Canvas: TCanvas; ARect: TRect; X, Y: Integer): string;
var
  CharPos, StartPos, EndPos: Integer;
begin
  CharPos := GetCharAtPos(Caption, Canvas, ARect, X, Y);
  if (CharPos <= 0) then
    Result := ''
  else
    begin
      StartPos := CharPos;
      while (StartPos > 1) and (Caption[StartPos] <> ' ') do
        Dec(StartPos);
      if (Caption[StartPos] = ' ') then
        Inc(StartPos);
      EndPos := CharPos;
      while (EndPos <= Length(Caption)) and (Caption[EndPos] <> ' ') do
        Inc(EndPos);
      Result := Copy(Caption, StartPos, EndPos - StartPos);
    end;
end;


An example to show the word of a label as the mouse moves over the label could be as follows.  This assumes the label's name is lblTest and the word under the cursor is put into a TEdit called edWord.

procedure TForm1.lblTestMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  edWord.Text := GetWordAtPos(lblTest.Caption, lblTest.Canvas, Rect(0, 0, lblTest.Width, lblTest.Height), X, Y);
end;


Hope this helps you.
JB
0
 

Author Comment

by:jpdupont
ID: 1353354
Hi JimBob,

Excuse my delay to answer, but I had a modem breakdown ...  
 
The code functions well except in this case:  
- I have a label component with definite size (AutoSize=false)  
- I set the label caption dynamically.  
 
In this case the recuperation of the word doesn't function.  
Rect(0, 0, lblTest.Width, lblTest.Height) is not correct.  

On the other hand,  
with regard to the second part of my question ,  
 
how to make this with your code:  
 
- I create a offsceen BMP  
- I draw on the BMP  
- I write a text to a position x,y on the BMP.  
- I copy the text in a component Picture, or I copy it on a paintbox.  

Now I want to recover the text under the cursor.  

Regards,

Jean-Pol  

0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 5

Expert Comment

by:JimBob091197
ID: 1353355
Hi again

Regarding your comments:
If you have a fixed-size label, the code should work fine as long as the label width is big enough.  Maybe I missed something here...

Regarding your comment about creating an offscreen bitmap, I found the following example works:
(I have a form with a paintbox called PaintBox1.  There are global variables "Bmp: TBitmap" and "TestCaption: string".)

In the form's Create event, I create a bitmap and put some text on it at X=30 and Y=20:
procedure TForm1.FormCreate(Sender: TObject);
begin
  TestCaption := 'This is a test.';
  Bmp := TBitmap.Create;
  Bmp.Width := 100;
  Bmp.Height := 100;
  Bmp.Canvas.TextOut(30, 20, TestCaption);
end;

In the paint event for the paintbox, I paint the bitmap:
procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  PaintBox1.Canvas.Draw(0, 0, Bmp);
end;

In the paintbox's MouseMove event:
procedure TForm1.PaintBox1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
  // edWord is a TEdit on Form1.
  edWord.Text := GetWordAtPos(TestCaption, Bmp.Canvas, Rect(0, 0, 100, 100), X - 30, Y - 20);
end;

Note that in the mousemove event, the rect uses Bmp's width and height, and you pass X-30 and Y-20 which are the TextOut positions for Bmp's text.


You also need to change GetWordAtPos as follows:
function GetWordAtPos(Caption: string; Canvas: TCanvas; ARect: TRect; X, Y: Integer): string;
var
  CharPos, StartPos, EndPos: Integer;
begin
  CharPos := GetCharAtPos(Caption, Canvas, ARect, X, Y);
  Result := '';
  if (CharPos > 0) and (X >= 0) and (Y >= 0) then
  etc...

Regards,
JB
0
 

Author Comment

by:jpdupont
ID: 1353356
Thanks for the answer.

I give you a A grade, but the question is not locked ...
Why ?

0
 
LVL 3

Expert Comment

by:AFZDeveloper
ID: 9535801
hi experts
text(word) under mouse evry where?
delphi programming on question:
http://experts-exchange.com/Programming/Programming_Languages/Delphi/Q_20763876.html

please help.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
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…

773 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