【发布时间】: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