【问题标题】:How to strike text in stringgrid (delphi)如何在stringgrid(delphi)中删除文本
【发布时间】:2013-12-08 07:57:52
【问题描述】:

我想通过右键单击该行从字符串网格中的一行中删除所有单元格的文本。我的代码没问题,但是该行中单击的单元格没有被击中(另一口井)!?!另外,我必须先单击一行然后右键单击才能继续,我只想右键单击但不知道如何:-/ 我的代码:

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;

太棒了!!! 但是,如果我想存储罢工状态,我还添加了一个包含“x”的列; 它工作正常但是 当我创建表单时,我在第 3 列中加载 stringrid 值和一些“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]), -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;

有什么想法???

【问题讨论】:

  • 您的绘图代码看起来不错,但总体思路行不通。您刚刚绘制的划线文本将被删除,单元格将以默认样式重新绘制。您必须编写 OnDrawCell 处理程序并绘制所有单元格。
  • 添加到 FC 的注释中,您的 OnMouseDown 处理程序应该简单地更新数据的状态,并调用 Refresh 来触发重绘。您的 OnDrawCell 处理程序应查看此状态以决定是否对其正在绘制的任何单元格使用删除线。
  • 您对选择不感兴趣,您对鼠标点击的位置感兴趣。将您的 StringGrid1.Selection.Top 替换为 StringGrid1.MouseCoord(X, Y).Y。其中有两个(当然,后者可以使用“j”)。

标签: delphi


【解决方案1】:

由于属性 Rows 是 TStrings 类型,因此您可以将有关被标记为已删除的所需信息存储在第一项的对象中(例如,快速和脏为整数)。使用存储的信息在 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]), -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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-01
    • 2016-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多