我有ListView(vsReport)和StringGrid,我想要的是如果我点击ListView中的某个元素,StringGrid中的特定单元格必须更改颜色.我该怎么做?
路径填充1(向上移动)和0(向右移动),它从左下角开始,到右上角结束,我必须为这些单元格着色.
谢谢你的回答,我处理了我的问题,但还有一个小问题,如何在单元格中显示文本?FillRect填充整个单元格.
procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem; Selected: Boolean);
var aRect: TRect;
a,x,y:integer;
path:string;
begin
path:=ListView1.Items[Item.Index].Caption;
x:=0;
y:=StringGrid1.RowCount;
for a := 0 to length(path) do
begin
if path[a]='1' then y:=y-1 else x:=x+1;
aRect := StringGrid1.CellRect(x-1,y-1);
StringGrid1.Canvas.Brush.Color := clBlue;
StringGrid1.Canvas.FillRect(aRect);
end;
end;
Run Code Online (Sandbox Code Playgroud) 我用Google搜索并找到了很多建议,但这一切似乎已经好几年了,但没有任何帮助.
我有一个8列的字符串网格,一旦我得到超过几百行,它需要超过2秒来填充(我使用GetTickCount进行比较).
我试过StringGrid.Perform(WM_SETREDRAW, 0, 0)(0, 1最后).我
Visible := False在更新时尝试过设置.两者都没用.
没有BeginUpdate()方法.
有什么建议?德尔福XE2启动器.如果经过试用和测试,我愿意使用FOSS第三方VCL字符串网格.
[更新]使用TDrawGrid ..."TDrawGrid没有属性"Cells",就像它的兄弟TStringGrid一样.你的代码必须计算显示数据的位置,然后它必须在"Canvas"上绘制数据的表示"电网."
这听起来像我很多工作:-(
使用VirtualTreeView - 如果足够快,听起来不错.我不会有任何子节点.(更新++我刚刚在主页上阅读了这篇文章"Virtual Treeview非常快.添加一百万个节点只需要700毫秒").那么速度没问题.但是只使用字符串网格会很好.特别是用户可以点击和排序的地方.
或者,stringgrid只有20行高.也许我只能处理滚动条点击并在用户滚动时清除并重新填充这20行?
[Furtehr更新]我从TStringGrid更改为TListView代码有Beginupdate()),但这可以忽略不计.Ops,我忘记了"viortual模式" - brb.
顺便说一句,数据是只读的,只是为了显示.
当然这是一个非常常见的探测器?
在Delphi VCL如果我想看到一个TStringGrid的哪个小区(列和行)我的鼠标盘旋在我会用MouseToCell.对于FireMonkey应用程序,此方法不再适用于Delphi(XE2).有谁知道我怎么能确定我的鼠标结束的细胞?OnMouseMove具有X和Y值,但这些是屏幕坐标而不是单元格坐标.
非常感谢.
嗨,有没有人知道是否可以将图片显示为字符串网格的背景,或者是否有人知道任何可以执行此操作的免费网格组件.
谢谢
科林
第一个问题:
如何调用stringgrid中不可见的部分?你需要滚动才能看到它.
例如:
stringgrid中有20行,但一次只能看到10行.你需要滚动查看其他10.如何调用"隐藏"的?
第二个问题:
我知道这可能不是正确的方法,所以一些指针将不胜感激.
我有一个带有1个固定行的字符串网格.我在运行时添加ColorButtons.所以我用按钮填充1列.我用这个按钮来"插入/删除"行.只要所有网格都处于"可见"部分,就可以正常工作.当我"插入"新行并将按钮移动到"隐藏"部分时出现问题.然后将最后一个按钮绘制到Cell [0,0]."隐藏"部分中的其他按钮被正确绘制.知道为什么会这样吗?我应该找到一种在OnDraw方法中管理此问题的方法,还是有更好(正确)的方法来执行此操作?
码:
procedure Tform1.addButton(Grid : TStringGrid; ACol : Integer; ARow : Integer);
var
bt : TColorButton;
Rect : TRect;
index : Integer;
begin
Rect := Grid.CellRect(ACol,ARow);
bt := TColorButton.Create(Grid);
bt.Parent := Grid;
bt.BackColor := clCream;
bt.Font.Size := 14;
bt.Width := 50;
bt.Top := Rect.Top;
bt.Left := Rect.Left;
bt.Caption := '+';
bt.Name := 'bt'+IntToStr(ARow);
index := Grid.ComponentCount-1;
bt :=(Grid.Components[index] as TColorButton);
Grid.Objects[ACol,ARow] := Grid.Components[index];
bt.OnMouseUp := Grid.OnMouseUp;
bt.OnMouseMove := Grid.OnMouseMove;
bt.Visible := true;
end; …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用stringgrid.数据从数据库(后端mysql)获取并显示在stringgrid中.

我想在每行的状态单元格中插入图像.即
if status =online then -->image1
else --->image2
Run Code Online (Sandbox Code Playgroud)
有谁知道如何做到这一点?
为什么 Delphi StringGrid 有时会OnClick在OnKeyDown?
调试截图:

我的 OnKeyDown 事件处理程序:
var
Top: Integer;
Bottom: Integer;
CurrentRow: Integer;
begin
Top := Grid.TopRow;
Bottom := Grid.TopRow + Grid.VisibleRowCount - 1;
if (Key = 38) then CurrentRow := Grid.Row - 1
else if (Key = 40) then CurrentRow := Grid.Row + 1;
// Disable OnClick because sometimes a 'TStringGrid.Click' is called anyway...
// (when clicking on form top window bar and navigating)
Grid.OnClick := nil;
if (CurrentRow < Top - 1) …Run Code Online (Sandbox Code Playgroud) 我想知道是否有办法在Delphi Pascal中为已存在/包含的类组件添加自定义方法.
我想用它来像这样旋转StringGrid:
StringGridn.rotate(angle);
Run Code Online (Sandbox Code Playgroud)
代替:
rotate(StringGridn, angle);
Run Code Online (Sandbox Code Playgroud)
谢谢你的建议 :)
我正在尝试编写自定义日期选择器(日历)。日期将显示在字符串网格上。我正在尝试用自定义颜色填充单击的单元格并使所选的单元格文本变为粗体。
这是我的代码:
type
TStringGrid = Class(Vcl.Grids.TStringGrid)
private
FHideFocusRect: Boolean;
protected
Procedure Paint;override;
public
Property HideFocusRect:Boolean Read FHideFocusRect Write FHideFocusRect;
End;
TfrmNepaliCalendar = class(TForm)
...
...
...
end;
procedure TfrmNepaliCalendar.StringGridDrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if gdSelected in State then begin
StringGrid.Canvas.Brush.Color := $00940A4B;
StringGrid.Canvas.FillRect(Rect);
StringGrid.Canvas.Font.Style := [fsBold];
StringGrid.Canvas.Font.Color := clHighlightText;
StringGrid.Canvas.TextOut(Rect.Left + 3, Rect.Top + 5, StringGrid.Cells[ACol,ARow]);
StringGrid.HideFocusRect := True;
end;
end;
{ TStringGrid }
procedure TStringGrid.Paint;
var
LRect: TRect;
begin
inherited;
if HideFocusRect then begin …Run Code Online (Sandbox Code Playgroud) OnDrawCell事件给我一个“矩形”记录,以便我知道真正的像素坐标在哪里:
OnDrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
Run Code Online (Sandbox Code Playgroud)
另一方面,OnSelectCell只提供行/列坐标。
OnSelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean);
Run Code Online (Sandbox Code Playgroud)
如何将行/列转换为像素坐标?
我需要这些,因为我需要在运行时创建一个组合框来覆盖整个选定的单元格,但仅当选择该单元格时。我知道如何做所有事情,但我不知道如何获取坐标(现在,我只是在父 tStringGrid 的 0,0 处创建组合)。
我使用的是Delphi7,但我认为这是一个普遍问题,与Delphi版本无关。
PS:我知道有很多商业和免费软件组件可以在单元格内实现组合框,但我不能和/或不想使用它们。
谢谢
delphi ×10
tstringgrid ×10
image ×2
canvas ×1
coordinates ×1
delphi-xe8 ×1
extends ×1
firemonkey ×1
lazarus ×1
listview ×1
methods ×1
mouse ×1
onclick ×1
onkeydown ×1