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
Lazarus broke my project
by
af0815
[
Today
at 10:59:53 am]
SandTris (Tetris variant)
by
Boleeman
[
Today
at 10:43:15 am]
Fpcupdeluxe
by
DonAlfredo
[
Today
at 10:39:15 am]
Lowercase Drive in LAZ 4....
by
JuhaManninen
[
Today
at 10:18:09 am]
New Mac mini M4 - FPC 3.2...
by
mischi
[
Today
at 09:32:46 am]
Get "serial number" of (r...
by
Thaddy
[
Today
at 09:25:53 am]
Some testing the water qu...
by
paweld
[
Today
at 07:07:33 am]
WaveFunction Collapse
by
Boleeman
[
Today
at 04:11:50 am]
Free Vision Build Failing...
by
swissbob
[November 15, 2025, 11:54:34 pm]
Add new unit to a Lazarus...
by
n7800
[November 15, 2025, 11:50:01 pm]
Programmatic filtering in...
by
jamie
[November 15, 2025, 08:05:46 pm]
How to hide IEEE754 limit...
by
srvaldez
[November 15, 2025, 07:42:38 pm]
This worries me about our...
by
Curt Carpenter
[November 15, 2025, 06:32:55 pm]
Porting PDF Viewer from D...
by
Boleeman
[November 15, 2025, 06:21:52 pm]
Form scaling aspect ratio
by
jamie
[November 15, 2025, 05:01:41 pm]
Demo #3 of my "home grown...
by
TBMan
[November 15, 2025, 03:05:57 pm]
Seems like good timing to...
by
mdlueck
[November 15, 2025, 02:17:23 pm]
Lazarus Bugfix Release 4...
by
jwdietrich
[November 15, 2025, 01:54:34 pm]
synedit
by
Martin_fr
[November 15, 2025, 01:49:12 pm]
[SOLVED] How to update 'F...
by
Hartmut
[November 15, 2025, 01:33:36 pm]
Help with search and repa...
by
avk
[November 15, 2025, 07:09:59 am]
Variable "xyz" doesn't no...
by
Mr.Madguy
[November 15, 2025, 06:22:15 am]
Problem Installing CHM He...
by
Curt Carpenter
[November 15, 2025, 03:03:11 am]
EditorMacroScript did not...
by
jamie
[November 15, 2025, 02:55:41 am]
Mapping Images Like Odome...
by
SandyG
[November 14, 2025, 11:53:01 pm]
« previous
next »
Print
Pages: [
1
]
Author
Topic: Highlighting row in TSTringGrid on mouse hover (Read 1152 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: 5495
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: 13226
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: 860
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