Recent

Author Topic: [SOLVED]DBGrid title mouse over event  (Read 734 times)

johnmc

  • New member
  • *
  • Posts: 25
[SOLVED]DBGrid title mouse over event
« on: March 30, 2019, 07:13:43 pm »
I have a DBGrid and would like to highlight which column headings are clickable by using something like an onmouseover event. There dosen't seem to be anything simple.

One posibility seems to be to use the OnMouseMove and the determine the co-ordinates of the mouse in terms of the grid X and Y ( Col and Row) position. I have yet to try this.

Also I found there is an dgTitleHotTrack option does anyone know where this is documented or how to use it?

regards John
« Last Edit: April 01, 2019, 11:51:34 pm by johnmc »

howardpc

  • Hero Member
  • *****
  • Posts: 3041
Re: DBGrid title mouse over event
« Reply #1 on: March 30, 2019, 08:24:53 pm »
Also I found there is an dgTitleHotTrack option does anyone know where this is documented or how to use it?
You use it by including dgHeaderHotTrack in Options, and setting FixedHotColor to your desired colour.
However, this simply highlights the column header title when the mouse hovers over it (whether you have defined column items or not), and you are looking for something more than this I think, for which you would have to write an OnMouseMove handler.
« Last Edit: March 30, 2019, 08:26:44 pm by howardpc »

jesusr

  • Sr. Member
  • ****
  • Posts: 470
Re: DBGrid title mouse over event
« Reply #2 on: March 30, 2019, 08:29:42 pm »
You mean dgHeaderHotTracking, it does just what you want, it highlights  the column header where the mouse is at the moment.

Now you want something more specific, some column header should be highlighted and some not. I think the easiest solution is what you already mention. You need to cheat a little, you have to find over what column you are, check if this column is clickable and if it's so, do nothing, but if it's not, change the FixedHotColor (which is the color used to draw the 'hot look'). Something like this:

Code: Pascal  [Select]
  1. procedure formXXXXX.dbgridMouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. var
  4.   aCol, aRow: Longint;
  5. begin
  6.   dbgrid.MouseToCell(x, y, aCol, aRow);
  7.   if Unclickable(aCol) then dbgrid.FixedHotColor := dbgrid.FixedColor
  8.   else             dbgrid.FixedHotColor := FOldFixedHotColor;
  9. end;    
  10.  

Note that this will work only if the grid titleStyle property is different from tsNative because that internally uses OS theme drawing and not custom drawing. FOldFixedHotColor:=dbgrid.FixedHotColor at form create for example.
« Last Edit: March 30, 2019, 08:33:23 pm by jesusr »

sstvmaster

  • Full Member
  • ***
  • Posts: 101
Re: DBGrid title mouse over event
« Reply #3 on: March 30, 2019, 08:34:49 pm »
Or also this with tsNative:
Code: Pascal  [Select]
  1. procedure TForm1.DBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
  2.   Y: Integer);
  3. var
  4.   Col,
  5.   Row : Integer;
  6. begin
  7.   Col := DBGrid1.MouseCoord(X, Y).X;
  8.   Row := DBGrid1.MouseCoord(X, Y).Y;
  9.  
  10.   if (Col > 0) and (Col <= DBGrid1.Columns.Count) then
  11.   begin
  12.  
  13.     // 1 is needed column to highlight
  14.     if Col = 1 then
  15.       DBGrid1.Options := DBGrid1.Options + [dgHeaderHotTracking]
  16.     else
  17.       DBGrid1.Options := DBGrid1.Options - [dgHeaderHotTracking];
  18.  
  19.   end;
  20. end;
Lazarus 2.0.2 x32
Lazarus 2.1.0 Trunk x32
OS Win 7 32bit

jesusr

  • Sr. Member
  • ****
  • Posts: 470
Re: DBGrid title mouse over event
« Reply #4 on: March 30, 2019, 08:51:12 pm »
Or also this with tsNative:

Nice! and it will not require additional variable for preserving the design time fixed hot color.
Although it will queue redrawings as crazy but should not matter too much :)

johnmc

  • New member
  • *
  • Posts: 25
Re: DBGrid title mouse over event
« Reply #5 on: March 31, 2019, 12:26:00 pm »
Thanks for all the input. I will give things a try and see whether I can get the desired results.

John

johnmc

  • New member
  • *
  • Posts: 25
Re: DBGrid title mouse over event
« Reply #6 on: March 31, 2019, 04:26:46 pm »
sstvmaster, that work a treat. I did have to change the default TitleStyle from tsLazarus to tsNative. The highlighting color is brighter when using tsNative.

Regards John

sstvmaster

  • Full Member
  • ***
  • Posts: 101
Re: DBGrid title mouse over event
« Reply #7 on: March 31, 2019, 04:46:36 pm »
You can also use "tsStandard or tsLazarus" and change the "FixedHotColor" to a color you want.

Code: Pascal  [Select]
  1.     if (Col=1) or (Col=3) then
  2.     begin
  3.       DBGrid1.FixedHotColor := clLime; // <-- hover color
  4.       DBGrid1.Options := DBGrid1.Options + [dgHeaderHotTracking];
  5.     end
  6.     else
  7.     begin
  8.       DBGrid1.Options := DBGrid1.Options - [dgHeaderHotTracking];
  9.       DBGrid1.FixedHotColor := cl3DLight; // <-- std. color
  10.     end;
Lazarus 2.0.2 x32
Lazarus 2.1.0 Trunk x32
OS Win 7 32bit