Solved

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

Posted on 1997-12-11
6
1,475 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
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…

856 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