delphi 如果你在列标题上,你如何进行HitTest?

p5fdfcr1  于 2023-05-17  发布在  其他
关注(0)|答案(1)|浏览(109)

我在TDBGrid上执行以下操作来启动拖动操作:

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

这是可行的,但如果我尝试调整列的大小,它将启动拖动操作。
什么是正确的方法来“HitTest”的TDBGrid检查鼠标是否在列标题,所以我可以跳过开始拖动操作?

z9zf31ra

z9zf31ra1#

似乎没有任何简单的答案,但我想出了这个问题的解决方案:

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;
}

相关问题