Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Detect when mouse clicked outside displayed rows 1

Status
Not open for further replies.

DecHam29

Technical User
Mar 15, 2008
4
US
I have a Tmemo component that can display 10 rows with the current font. There are currently 5 rows assigned, i,e Memo.lines.count -1 = 4. Is there a way to detect when a row is clicked that is greater than the last assigned/displayed row. Example: If I click it the area of row 7, the function returns the last row assigned or row 4. I am looking for feedback that tells me that I am clicking a non-assigned area.

The code below identifies existing rows clicked. Am I missing something?

Thank you for reading.

Vern

from About.com
function SelectMemoLine(Memo : TCustomMemo):integer ;
var
Line : integer;
begin
with Memo do
begin
Line := Perform(EM_LINEFROMCHAR, SelStart, 0) ;
SelStart := Perform(EM_LINEINDEX, Line, 0) ;
SelLength := Length(Lines[Line]) ;
result := Line;
end;
end;
 
I wonder if there is a way to detect the XY coordinates of the cursor position within the memo? If so then you could trap the mouse position when the user clicked and compare that to your caret's position.
 
As you suggest that can be done. But, to me, it's rather involved. I was hoping that the code I attached would have info to help or some other code example.

Thank you for you concern.

Vern
 
Given that you know the font that you are using you can calculate what line the user clicked on using the mouse position when they clicked.

You just need to make sure that what canvas control to use to find the font height is using the same font and size as your memo.

Code:
procedure TForm1.Memo1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  iLine: integer;
begin
  iLine := ((Y-1) DIV Self.Canvas.TextHeight('X'))+1;

  showmessage('Line:'+inttostr(iLine));

end;
 
Great! Preliminary test indicates that your suggestion dose what I want. Thank you very much as Wolf Blitzer would say.

Vern of Indiana
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top