3

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?

NGLN
  • 43,011
  • 8
  • 105
  • 200
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551

1 Answers1

4

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;
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • +1 and the answer, @kenWhite ... and may I take this opportunity to say "D'oh!"? Sorry, I was coding until dark o'clock and rose before dawn to continue. This shows that I shoudl probably stop coding until I wake up ;-) Thanks, as always, for your help. – Mawg says reinstate Monica Oct 25 '12 at 01:55
  • What if the drag originates from another application? DragOver and MouseMove) is never called! – Mike Versteeg Dec 10 '14 at 15:27
  • @Mike: That would be a totally separate question (and answer); for cross-application drag and drop you have to use OLE drag functionality, which involves different methods and registration with the shell as a dragdrop client. – Ken White Dec 10 '14 at 15:34
  • Actually it took multiple readings before I figured out this was intra-app, so although I agree it is a different process, the formulation of the question remains the same. I know how to d&d between apps, but not the answer to the question of this topic for that scenario. If you do, I'd be happy to make a post with the same topic. – Mike Versteeg Dec 11 '14 at 08:57