我想通过右键单击该行来触发stringgrid中一行中所有单元格的文本.我的代码是关于ok的,但是来自该行的单击单元格没有被攻击(另一个很好)!?!此外,我必须首先点击一行,然后右键单击继续,我想只是右键单击但不知道如何: – /
我的代码:
我的代码:
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],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;
完美的事情!
但是,如果我想存储受到攻击的状态,我还会添加一个包含’x’的列;
它工作正常但是
当我创建表单时,我加载stringrid值和第3列中的一些’x’,
我尝试在form.create中使用该代码来打击这些行但不起作用:-(
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]),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,DT_CENTER); StringGrid1.Invalidate; end; end;
任何的想法???
解决方法
由于属性Rows是TStrings类型,因此您可以在第一个项目的Object中存储有关被标记为已删除的所需信息(例如,快速和脏整数).使用存储的信息在OnDrawCell中完成绘画.
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]),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),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;