Solved

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

Posted on 1997-12-11
6
1,497 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
[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
  • 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
Independent Software Vendors: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

739 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