是否可以在StrinGrid中强制选择? 我想只允许用户水平选择单元格,即使鼠标在选择时可以上下移动,我想要stringgrid只在有MouseDown的行上显示选择。 因此,当用户想要选择一系列单元格时,他将单击鼠标,向右(或向左)拖动鼠标,同时查看如何一个接一个地选择单元格,然后是MouseUp事件。 在拖动时,我不希望用户在移动鼠标时看到其他行(而不是拖动开始的行)。 我假设我应该在StringGrid的onMouseMove中做一些事情......但是怎么做?
到目前为止我的代码是:
CASE
有可能吗? 我怎么能这样做?
答案 0 :(得分:5)
是的,它是可行的。我没有控制网格,而是在选择时设置鼠标移动的界限:
使用Windows.ClipCursor
;
首先在mouseDown上计算有效边界:
procedure TForm8.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
StringGrid: TStringGrid;
GridRect: TGridRect;
Row: Integer;
CursorClipArea: TRect;
BoundsRect: TRect;
begin
// The Sender argument to StringGrid1Click is actually the StringGrid itself,
// and the following "as" cast lets you assign it to the StringGrid local variable
// in a "type-safe" way, and access its properties and methods via the temporary variable
StringGrid := Sender as TStringGrid;
// Now we can retrieve the use selection
GridRect := StringGrid.Selection;
// and hence the related GridRect
// btw, the value returned for Row automatically takes account of
// the number of FixedRows, if any, of the grid
Row := GridRect.Top;
//Then set the bounds of the mouse movement.
//ClipCursor uses Screen Coordinates to you'll have to use ClientToScreen
CursorClipArea.TopLeft := StringGrid.ClientToScreen(StringGrid.CellRect(StringGrid.FixedCols, Row).TopLeft);
CursorClipArea.BottomRight := StringGrid.ClientToScreen(StringGrid.CellRect(StringGrid.ColCount - 1, Row).BottomRight);
Windows.ClipCursor(@CursorClipArea)
end;
//Then on mouse up release the mouse
procedure TForm8.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
//Release mouse
Windows.ClipCursor(nil)
end;