Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 1997-12-11
6
Medium Priority
?
1,583 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 400 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Integration Management Part 2
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

972 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