2013-12-08 24 views
5

Bu hatta sağ tıklatarak stringgrid'deki bir satırdaki tüm hücrelerin metnine vurmak istiyorum. Benim kod hakkında Tamam, ama hattan tıklanan hücre vurulmadı (diğer iyi)!?! Ayrıca, bir satırdaki ilk tıklamak zorunda ve sonra sağa devam etmek tıklayın, sadece sağ tıklayıp istiyorum ama nasıl bilmiyorum: -/ Kodum:stringgrid (delphi) içinde metin vurma

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; 

Acayip !! ! Ancak, grevlenmiş durumu kaydetmek istiyorsam, 'x' içeren bir sütun ekliyorum; ben formu oluştururken AMA ben bu satır grev form.create bu kodu kullanmayı deneyin, 3. sütunda stringrid değerini ve bazı 'x' yük çalışıyor ama

:-(çalışmıyor mülkiyet Satırlar yana
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; 

herhangi bir fikir ???

+4

Çizim kodunuz iyi görünüyor, ancak genel fikir çalışmayacak. Az önce çizdiğiniz metinler silinecek ve hücre varsayılan stil ile yeniden çizilecektir. OnDrawCell işleyicisini yazmalı ve tüm hücrelerini çizmelisin. –

+0

FC'nin yorumuna eklediğinizde, OnMouseDown işleyiciniz verilerinizin durumunu basitçe güncellemeli ve yeniden çizmeyi tetiklemek için Yenile 'yi çağırmalıdır. OnDrawCell işleyiciniz, çizdiği herhangi bir hücre için grevin kullanılıp kullanılmayacağına karar vermek için bu duruma bakmalıdır. –

+0

* Seçim * ile ilgilenmiyorsunuz, fare tıklandığında nereye ilgileniyorsunuz. 'StringGrid1.MouseCoord (X, Y) .Y' ile' StringGrid1.Selection.Top' değiştirin. Onlardan iki tane var (tabii ki 'j' yi ikincisi için kullanabilirsiniz). –

cevap

3

(hızlı ve tamsayı olarak kirli örneğin) ilk öğenin Nesne silinmiş olarak işaretlenmesini hakkında gerekli bilgileri depolayabilir TStrings tiptedir. Boyama, depolanmış bilgileri kullanarak OnDrawCell içinde gerçekleştirilir

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;