Lazarus
Home
Help
TinyPortal
Search
Login
Register
Lazarus
»
Forum
»
Programming
»
LCL
»
Highlighting row in TSTringGrid on mouse hover
Free Pascal
Website
Downloads
Wiki
Documentation
Bugtracker
Mailing List
Lazarus
Website
Downloads (Laz+FPC)
Packages (OPM)
FAQ
Wiki
Documentation (RTL/FCL/LCL)
Bugtracker
CCR Bugs
GIT
Mailing List
Other languages
Foundation
Website
Useful Wiki Links
Project Roadmap
Getting the Source
Screenshots
How to use the forum
Forum Rules
About donations (wiki)
Bookstore
Computer Math and Games in Pascal
(preview)
Lazarus Handbook
Search
Advanced search
Recent
Datatype of Fields in sql...
by
Zvoni
[
Today
at 08:53:09 am]
Is it possible to create ...
by
CM630
[
Today
at 07:32:46 am]
StrMath.pas a String Numb...
by
JgQDev
[
Today
at 07:30:58 am]
We are starting to use La...
by
dbannon
[
Today
at 06:25:04 am]
Fast Canvas Library V1.05...
by
backprop
[
Today
at 03:19:31 am]
TurboBird for FireBird 5
by
maurog
[
Today
at 02:28:10 am]
Status of FPC 3.4.0 or FP...
by
Graeme
[
Today
at 01:53:52 am]
TurboBird IBX
by
maurog
[
Today
at 01:53:39 am]
lazarus structure
by
crownfield
[
Today
at 12:28:23 am]
Delphi Magazine issues 1-...
by
simone
[February 16, 2026, 11:49:11 pm]
LazSerial not found
by
CM630
[February 16, 2026, 10:00:08 pm]
Duplicated icon in the Wi...
by
n7800
[February 16, 2026, 09:31:22 pm]
"PPU file isn't found by ...
by
Martin_fr
[February 16, 2026, 09:29:59 pm]
Reporting a Bug? in Strin...
by
Bart
[February 16, 2026, 06:47:13 pm]
[ANN] fpGUI Toolkit v2.0....
by
cdbc
[February 16, 2026, 06:45:05 pm]
Free Pascal for a small e...
by
devEric69
[February 16, 2026, 06:13:48 pm]
Onguard Win64
by
AgriMensor
[February 16, 2026, 05:55:41 pm]
Duplicate identifier erro...
by
Martin_fr
[February 16, 2026, 04:03:10 pm]
Frustrating Error When us...
by
TYDQ
[February 16, 2026, 02:50:02 pm]
[AGGPas] Difference betwe...
by
wp
[February 16, 2026, 02:15:23 pm]
[AGGPas] Usage of scale m...
by
Roland57
[February 16, 2026, 11:33:08 am]
makefiles
by
valdir.marcos
[February 16, 2026, 11:24:17 am]
ThorVG - test (lightweigh...
by
valdir.marcos
[February 16, 2026, 11:19:41 am]
Problems in drawing an ar...
by
valdir.marcos
[February 16, 2026, 11:18:39 am]
AI to port DBDesigner For...
by
Graeme
[February 16, 2026, 10:26:15 am]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Highlighting row in TSTringGrid on mouse hover (Read 1250 times)
bruce.button
Jr. Member
Posts: 59
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!
Logged
Handoko
Hero Member
Posts: 5515
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 »
Short answer:
Easy.
Long answer:
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]
[+]
[-]
unit
Unit1
;
{$mode objfpc}{$H+}
interface
uses
Classes
,
SysUtils
,
Forms
,
Controls
,
Grids
,
StdCtrls
;
type
{ TForm1 }
TForm1
=
class
(
TForm
)
Label1
:
TLabel
;
StringGrid1
:
TStringGrid
;
procedure
StringGrid1MouseMove
(
Sender
:
TObject
;
Shift
:
TShiftState
;
X
,
Y
:
Integer
)
;
end
;
var
Form1
:
TForm1
;
implementation
{$R *.lfm}
{ TForm1 }
procedure
TForm1
.
StringGrid1MouseMove
(
Sender
:
TObject
;
Shift
:
TShiftState
;
X
,
Y
:
Integer
)
;
var
R
,
C
:
Integer
;
Start
,
Size
:
Integer
;
i
:
Integer
;
begin
// Row
R
:
=
-
1
;
Start
:
=
0
;
for
i
:
=
0
to
StringGrid1
.
RowCount
-
1
do
begin
Size
:
=
StringGrid1
.
RowHeights
[
i
]
;
if
(
Y > Start
)
and
(
Y < Start
+
Size
)
then
begin
R
:
=
i
;
Break
;
end
;
Inc
(
Start
,
Size
)
;
end
;
// Column
C
:
=
-
1
;
Start
:
=
0
;
for
i
:
=
0
to
StringGrid1
.
ColCount
-
1
do
begin
Size
:
=
StringGrid1
.
ColWidths
[
i
]
;
if
(
X > Start
)
and
(
X < Start
+
Size
)
then
begin
C
:
=
i
;
Break
;
end
;
Inc
(
Start
,
Size
)
;
end
;
// Show result
Caption
:
=
'Mouse X:Y = '
+
X
.
ToString
+
':'
+
Y
.
ToString
;
case
(
R <
0
)
or
(
C <
0
)
of
True
:
Label1
.
Caption
:
=
'Mouse is outside the grid'
;
False
:
Label1
.
Caption
:
=
'Row:Col = '
+
R
.
ToString
+
':'
+
C
.
ToString
;
end
;
end
;
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
Logged
wp
Hero Member
Posts: 13361
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.
Logged
bruce.button
Jr. Member
Posts: 59
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.
Logged
Hansvb
Hero Member
Posts: 888
Re: Highlighting row in TSTringGrid on mouse hover
«
Reply #4 on:
October 11, 2024, 05:01:15 pm »
Hi,
I tried wp's example and at first glance it works fine. But if you make the stringgrid smaller and then leave the string grid on the right (or bottom) side, the last row remains gray.
How could you prevent a row from remaining gray?
Logged
Print
Pages: [
1
]
« previous
next »
Lazarus
»
Forum
»
Programming
»
LCL
»
Highlighting row in TSTringGrid on mouse hover
TinyPortal
© 2005-2018