### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Highlighting row in TSTringGrid on mouse hover  (Read 296 times)

#### bruce.button

• New Member
• Posts: 48
##### Highlighting row in TSTringGrid on mouse hover
« on: June 05, 2023, 06:07:43 pm »
Is there a (relatively) easy way of highlighting a row in a TStringGrid when the mouse cursor hovers over it? I'm sure it must be possible, but I can't even find an event for mouseover  on the TStringGrid.

Thank you in advance!

#### Handoko

• Hero Member
• Posts: 4934
• My goal: build my own game engine using Lazarus
##### Re: Highlighting row in TSTringGrid on mouse hover
« Reply #1 on: June 05, 2023, 07:28:28 pm »
Easy.

Basically it can be divided into 2 parts:
- Converting mouse position to row:col values
- Coloring the grid.

I wrote the code for converting mouse position to grid's row:col values.

Code: Pascal  [Select][+][-]
1. unit Unit1;
2.
3. {\$mode objfpc}{\$H+}
4.
5. interface
6.
7. uses
8.   Classes, SysUtils, Forms, Controls, Grids, StdCtrls;
9.
10. type
11.
12.   { TForm1 }
13.
14.   TForm1 = class(TForm)
15.     Label1: TLabel;
16.     StringGrid1: TStringGrid;
17.     procedure StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
18.       Y: Integer);
19.   end;
20.
21. var
22.   Form1: TForm1;
23.
24. implementation
25.
26. {\$R *.lfm}
27.
28. { TForm1 }
29.
30. procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
31.   Y: Integer);
32. var
33.   R, C:        Integer;
34.   Start, Size: Integer;
35.   i:           Integer;
36. begin
37.   // Row
38.   R     := -1;
39.   Start := 0;
40.   for i := 0 to StringGrid1.RowCount-1 do
41.   begin
42.     Size := StringGrid1.RowHeights[i];
43.     if (Y > Start) and (Y < Start + Size) then
44.     begin
45.       R := i;
46.       Break;
47.     end;
48.     Inc(Start, Size);
49.   end;
50.   // Column
51.   C     := -1;
52.   Start := 0;
53.   for i := 0 to StringGrid1.ColCount-1 do
54.   begin
55.     Size := StringGrid1.ColWidths[i];
56.     if (X > Start) and (X < Start + Size) then
57.     begin
58.       C := i;
59.       Break;
60.     end;
61.     Inc(Start, Size);
62.   end;
63.   // Show result
64.   Caption := 'Mouse X:Y = ' +  X.ToString + ':' + Y.ToString;
65.   case (R < 0) or (C < 0) of
66.     True:  Label1.Caption := 'Mouse is outside the grid';
67.     False: Label1.Caption := 'Row:Col = ' + R.ToString + ':' + C.ToString;
68.   end;
69. end;
70.
71. end.

For the second part, you can study and use the code of the demo for doing coloring in the link below ( Searchable StringGrid, category "User Interface) :
https://wiki.freepascal.org/Portal:HowTo_Demos#User_Interface

#### wp

• Hero Member
• Posts: 11198
##### Re: Highlighting row in TSTringGrid on mouse hover
« Reply #2 on: June 05, 2023, 07:56:03 pm »
Use OnPrepareCanvas to highlight the row under the mouse. It is fired immediately before a specific cell is painted. To get the row index under the mouse query Mouse.CursorPos (which is relative to the screen) and convert it to grid pixels by the grid's ScreenToClient method. Finally call MouseToCell to determine the col and row indices of the mouse-over point. Compare them with the arguments passed to OnPreparecanvas to determine whether the currently requested cell needs a different color. Finally, in order to update the color whenever the mouse moves handle the OnMouseMove event and request the grid to repaint itself (Grid.Invalidate).

See attached demo.

#### bruce.button

• New Member
• Posts: 48
##### Re: Highlighting row in TSTringGrid on mouse hover
« Reply #3 on: June 05, 2023, 08:18:25 pm »
Thank you so much, Handoko and wp. That is extremely helpful.