delphi - 在 StringGrid 中强制选择单元格 - Delphi

标签 delphi

是否可以在我的 StrinGrid 中强制进行选择? 我想只允许用户水平选择单元格,即使鼠标在选择时可能上下移动,我想 stringgrid 仅在有 MouseDown 的行上显示选择。 因此,当用户想要选择一系列单元格时,他会单击鼠标,向右(或向左)拖动鼠标,同时查看如何逐个选择单元格,然后出现 MouseUp 事件。 拖动时,我不希望用户在移动鼠标时看到其他行(而不是拖动开始的行)被选中。 我想我应该在 StringGrid 的 onMouseMove 中做一些事情......但是怎么做?

到目前为止我的代码是:

// this draws a focus rect around the selected cell (DefaultDrawing=false)
procedure TForm2.sgDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect;
  State: TGridDrawState);
begin
if (gdFocused in State)or(gdSelected in State) then
            begin
              sg.Canvas.Pen.Color:=$00FFEECC;
              sg.Canvas.MoveTo(Rect.Left,Rect.Top);
              sg.Canvas.LineTo(Rect.Right,Rect.Top);
              sg.Canvas.LineTo(Rect.Right,Rect.Bottom);
              sg.Canvas.LineTo(Rect.Left,Rect.Bottom);
              sg.Canvas.LineTo(Rect.Left,Rect.Top);
            end
            else
            begin
              sg.Canvas.Brush.Color:=clWhite;
              sg.Canvas.FillRect(Rect);
            end;
end;

procedure TForm2.sgMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
 myrow:=sg.Row;
 mycol:=sg.Col;
end;

procedure TForm2.sgMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  sg.Row:=myrow;
end;

这可能吗? 我怎样才能做到这一点?

最佳答案

是的,这是可能的。我不会控制网格,而是在选择时设置鼠标移动的边界: 使用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;

关于delphi - 在 StringGrid 中强制选择单元格 - Delphi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31691810/

相关文章:

Delphi 工具栏样式已更改

delphi - 在多个应用程序之间共享图像的最佳方式

delphi - 在 GetObject() 调用后,Graphics.TBitmap 成功从 JPEG 帧解码导致空 TDibSection

delphi - ADOQuery.Filter 不适用于 LIKE

java - 如何获取对 DLL 的 java JNA 调用以获取参数中返回的数据?

delphi - 是否可以将自己的帮助文件添加到 Delphi XE2 IDE 中?

delphi - 功能 'rich' 为何是 FireMonkey 框架

delphi - 在执行 DBExpress 查询时,如何让我的程序响应用户输入?

delphi - Delphi RemoteDebugger 使用哪个端口?

c# - 如何处理 COM 互操作中的 var(或 ref)参数?