TDBGrid - How do you HitTest if you are on the column headers?

70 Views Asked by At

I start a drag operation on a TDBGrid by doing:

void __fastcall TMyForm::DBGrid1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
    if (DragDetect(DBGrid1->Handle, Point(X,Y))) {
        DBGrid1->BeginDrag(true);
    }
}

This works, but if I try to resize a column it starts a drag operation instead.

What is the correct way to "HitTest" the TDBGrid to check if the mouse is over the column headers, so I can skip beginning the drag operation?

1

There are 1 best solutions below

0
user3161924 On

There doesn't seem to be any easy answer but I came up with this solution to the issue:

class TMyForm : public TForm
{
  // ...
  bool m_bIgnoreDrag=false;
  // ...
};


void __fastcall TMyForm::DBGrid1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
    // returns the column/row in the visible grid itself
    // (row 0 is always header 1, is first line after, etc..)
    // unused areas are -1,-1
    TGridCoord coord=DBGrid1->MouseCoord(X, Y);
    if (coord.Y>0) {
        if (!m_bIgnoreDrag) {
            if (DragDetect(DBGrid1->Handle, Point(X,Y))) {
                DBGrid1->BeginDrag(true);
            }
        }
    }
    else m_bIgnoreDrag=GetCapture()!=NULL;

}

void __fastcall TMyForm::DBGrid1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    // Handle edge case of no mouse move after drag of non-item to item then click to drag.
    m_bIgnoreDrag=false;
}