Search code examples
delphitstringgrid

Mouse over stringgrid when dragging?


Parts of my stringgrid are eligible drop targets, and some are not (first row is column headings, first column is a sort of index and subsequent columns may be dropped to). I have that coded and working.

Now I am thinking that it might be nice to gve a visual indiation to the user as he drags the mouse over a cell which is a potential drop target. I woudl like to highlight the first cell in the row and column of the cell over which he is currently hovering (or possibly the entire row and column, forming a sort of crosshair; I am as yet undecided). I reckon I can code that in OnDrawCell.

I had thought to use OnMouseMove and cehck if Dragging then, but ...

My problem is that when I am dragging the OnMouseMove event never gets called.

Is there any other way to know when the cursor is hovering over a strigngrid during a drag operation?


Solution

  • The OnDragOver event is specifically designed for doing this; it's called automatically, and provides the X and Y coordinates where the mouse pointer is located. There's a code sample available at that link location that demonstrates using it as well - it's for a TListBox, but the principle is the same.

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      ListBox1.Items.Add('Not');
      ListBox1.Items.Add('In');
      ListBox1.Items.Add('Alphabetical');
      ListBox1.Items.Add('Order');
    end;
    
    // This OnDragOver event handler allows the list box to
    // accept a dropped label.
    
    procedure TForm1.ListBox1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    begin
      Accept := Source is TLabel;
    end;