2015-07-29 3 views
5

Можно ли выделить выделение в моем StrinGrid? Я хотел бы только разрешить пользователю выбирать ячейки по горизонтали, даже если мышь может перемещаться вверх и вниз при выборе, я хочу, чтобы stringgrid показывал выделение только в строке, где был MouseDown. Итак, когда пользователь хочет выбрать диапазон ячеек, он нажимает на мышь, перетаскивает мышь вправо (или влево), видя, как ячейки выбираются один за другим, а затем появляется событие MouseUp. Во время перетаскивания я не хочу, чтобы пользователь видел другие строки (чем тот, где началось перетаскивание), когда он перемещает мышь. Предполагаю, что я должен что-то сделать в onMouseMove из StringGrid ... но как?Выбор силы ячеек в StringGrid - Delphi

Мой код до сих пор:

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

Возможно ли это? Как я могу это сделать?

ответ

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; 
+1

Спасибо, это работает. – user1137313

Смежные вопросы