2013-12-08 19 views
5

Mi piacerebbe colpire il testo di tutte le celle da una linea in una stringa con il tasto destro del mouse su quella linea. Il mio codice è ok, ma la cella cliccata dalla linea non viene barrata (l'altro bene)!?! Inoltre, devo cliccare prima su una riga e quindi fare clic destro per procedere, vorrei solo fare clic destro, ma non so come: -/ Il mio codice:Come colpire il testo in stringgrid (delphi)

procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; 
    Shift: TShiftState; X, Y: Integer); 
    var 
    i, j: integer; 
begin 
    if Button = mbRight then 
    begin 
    j:=StringGrid1.Selection.Top; 
    If MessageDlg('Disable row '+IntToStr(j), 
     mtConfirmation, [mbYes, mbNo], 0, mbYes) =mrYes then 
    begin 
     With Stringgrid1 As TStringGrid Do 
     With Canvas Do 
     begin 
      for i := 0 to 2 do 
      begin 
      Rect := CellRect (i, StringGrid1.Selection.Top); 
      Font.Style := Font.Style + [fsStrikeOut]; 
      FillRect(Rect); 
      DrawText(Canvas.Handle, PChar(Cells[i,j]), -1, Rect ,DT_CENTER); 
      end; 
     end; 
    end; 
    end; 
end; 

Wonderfull !! ! Ma se voglio memorizzare lo stato barrato aggiungo anche una colonna contenente una 'x'; funziona benissimo MA quando creo forma che carico il valore stringrid e un po 'x' nella colonna 3, cerco di usare quel codice in form.create per colpire questi fila ma non funziona :-(

for J := 1 to stringGrid1.RowCount-1 do 
begin 
if stringGrid1.Cells[3,J]='x' then 
for I:=1 to 2 do 
    begin 
    StringGrid1.Canvas.Font.Style := Font.Style + [fsStrikeOut]; 
    StringGrid1.Canvas.Brush.Color := clBtnFace; // title 
    StringGrid1.Canvas.FillRect(Rect); 
    Rect.Top := Rect.Top + 4; 
    drawText(Canvas.Handle, PChar(StringGrid1.Cells[I, J]), -1, Rect, DT_CENTER); 
    StringGrid1.Invalidate; 
    end 
    else 
    begin 
    StringGrid1.Canvas.Font.Style := Font.Style - [fsStrikeOut]; 
    StringGrid1.Canvas.Brush.Color := clBtnFace; // title 
    StringGrid1.Canvas.FillRect(Rect); 
    Rect.Top := Rect.Top + 4; 
    drawText(Canvas.Handle, PChar(StringGrid1.Cells[I, J]), -1, Rect, DT_CENTER); 
    StringGrid1.Invalidate; 
    end; 
end; 

Qualche idea ???

+4

Il codice di disegno sembra ok, ma l'idea generale non funzionerà. Il testo barrato che hai appena disegnato verrà cancellato e la cella verrà ridisegnata con lo stile predefinito. Devi scrivere il gestore di OnDrawCell e disegnare tutte le tue cellule. –

+0

In aggiunta al commento di FC, il tuo gestore OnMouseDown dovrebbe semplicemente aggiornare lo stato dei tuoi dati e chiamare Aggiorna per attivare un nuovo aggiornamento. Il tuo gestore OnDrawCell dovrebbe guardare in questo stato per decidere se utilizzare o meno il through-through per ogni cella che sta disegnando. –

+0

Non sei interessato alla * selezione *, sei interessato a dove si fa clic con il mouse. Sostituisci il tuo 'StringGrid1.Selection.Top' con' StringGrid1.MouseCoord (X, Y) .Y'. Ce ne sono due (ovviamente puoi usare 'j' per quest'ultimo). –

risposta

3

Dal momento che le righe di proprietà è di tipo TStrings si potrebbe memorizzare le informazioni necessarie riguardo fosse stato soppresso nel oggetto del primo elemento (ad esempio, rapido e sporco come intero). Il dipinto è fatto in OnDrawCell usando le informazioni memorizzate

const 
    CRLF = #13#10; 


procedure TForm3.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); 
var 
    i: Integer; 
begin 
    With TStringGrid(Sender) do 
    begin 
    With Canvas Do 
    begin 
     if Integer(Rows[ARow].Objects[0]) = 1 then 
     Font.Style := Font.Style + [fsStrikeOut] 
     else 
     Font.Style := Font.Style - [fsStrikeOut]; 
     if ARow = 0 then 
     Brush.Color := clBtnFace // title 
     else 
     Brush.Color := clWhite; 
     FillRect(Rect); 
     Rect.Top := Rect.Top + 4; 
     DrawText(Canvas.Handle, PChar(Cells[ACol, ARow]), -1, Rect, DT_CENTER); 
    end; 
    end; 
end; 

procedure TForm3.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); 
const 
    C_Message: Array [0 .. 1] of String = ('Disable', 'Enable'); 
var 
    C, R: Integer; 
begin 
    StringGrid1.MouseToCell(X, Y, C, R); 
    if (Button = mbRight) and (C > -1) and (R > 0 { -1 }) then 
    begin // Allow Disable or Enable depending on the stored state 
    if (MessageDlg(C_Message[Integer(StringGrid1.Rows[R].Objects[0])] + ' row ' + IntToStr(R), mtConfirmation, [mbYes, mbNo], 0, mbYes) = mrYes) then 
    begin 
     If Integer(StringGrid1.Rows[R].Objects[0]) = 0 then 
     StringGrid1.Rows[R].Objects[0] := TObject(1) 
     else 
     StringGrid1.Rows[R].Objects[0] := TObject(0); 
     StringGrid1.Invalidate; // force repainting 
    end; 
    end; 
end; 

procedure TForm3.FormCreate(Sender: TObject); 
var 
    R, C: Integer; 
begin // Demo content 
    With StringGrid1 do 
    begin 
    FixedCols := 0; 
    DefaultDrawing := false; 
    Rowcount := 6; 
    Colcount := 4; 
    DefaultColWidth := 100; 
    Rows[0].Text := 'COL 1' + CRLF + 'COL 2' + CRLF + 'COL 3' + CRLF + 'COL 4'; 
    for R := 1 to Rowcount - 1 do 
     for C := 0 to Colcount - 1 do 
     Cells[C, R] := Format('Content %d - %d', [C + 1, R]); 
    end; 
end; 
Problemi correlati