Solved

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

Posted on 1997-12-11
6
1,422 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
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…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

744 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

10 Experts available now in Live!

Get 1:1 Help Now