【发布时间】:2012-11-26 05:48:46
【问题描述】:
我使用 Delphi 2007。我有一个 TListView,其中 OwnerData 和 OwnerDraw 设置为 True。 ViewStyle 设置为 vsReport。
我有一个record。
type TAList=record
Item:Integer;
SubItem1:String;
SubItem2:String;
end;
var
ModuleData: array of TAList;
procedure TForm1.ListView3Data(Sender: TObject; Item: TListItem);
begin
Item.Caption := IntToStr(ModuleData[Item.Index].Item);
Item.SubItems.Add(ModuleData[Item.Index].SubItem1);
Item.SubItems.Add(ModuleData[Item.Index].SubItem2);
end;
procedure TForm1.ListView3DrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
LIndex : integer;
LRect: TRect;
LText: string;
TTListView: TListView;
begin
TTListView := TListView(Sender);
if (Item.SubItems[0] = '...') then
begin
TTListView.Canvas.Brush.Color := clHighlight;
TTListView.Canvas.Font.Color := clHighlightText;
end else
begin
TTListView.Canvas.Brush.Color := TTListView.Color;
TTListView.Canvas.Font.Color := TTListView.Font.Color;
end;
for LIndex := 0 to TTListView.Columns.Count - 1 do
begin
if (not(ListView_GetSubItemRect(TTListView.Handle, Item.Index, LIndex, LVIR_BOUNDS, @LRect))) then Continue;
TTListView.Canvas.FillRect(LRect);
if (LIndex = 0) then LText := Item.Caption else LText := Item.SubItems[LIndex - 1];
LRect.Left := LRect.Left + 6;
DrawText(TTListView.Canvas.Handle, PChar(LText), Length(LText), LRect, DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX or DT_END_ELLIPSIS);
end;
end;
我希望在 SubItem2 被截断时显示提示。在 Windows XP 上,根本不显示任何提示。在 Windows Vista 和 Windows 7 上,当我的鼠标悬停在某个项目上时,它会显示一个完全关闭的提示。
我没有处理提示的特殊代码。 OwnerData 和 OwnerDraw 模式中应该有一个吗?
这是我得到的图片:
(来源:noelshack.com)
(来源:noelshack.com)
编辑:
大卫问为什么将OwnerDraw 设置为True。有两个原因:
- 这样,我可以“禁止”用户选择。
- 如果我将
OwnerDraw设置为False,我会遇到另一个问题。见Why do I get white column separators on my custom-drawn listview?
编辑 2:
如果我按照 TLama 的建议处理 OnInfoTip 事件,我会收到一个非主题的气球提示和来自 Windows Vista 和 7 的错误提示。
【问题讨论】:
-
就像我写的那样,根本没有处理提示的代码。我很困惑,因为 Windows Vista 和 7 确实显示了一个提示,但是一个错误的提示——比如,如果我的鼠标在第一项上,我会得到第 13-14 项的提示。因此,我不确定是否需要编写代码来处理提示或什么。我可以补充一下,为什么 Windows Vista & 7 在没有实际代码的情况下会显示提示?
-
处理
OnInfoTip事件。在那里你可以访问当前悬停的TListItem。 -
@Ken 和其他想尝试重现此问题的人,我制作了一个简单的
testing project。只需在 Windows Vista 或 Windows 7 上的 Delphi(OP 有 D2007,我在 D2009 中尝试过)中构建并运行该项目,然后悬停第一行或第二行中的一个子项。控件始终显示最后一个悬停子项的提示。 -
@Alllain,好吧,既然这显然是一个 Windows 问题,你需要自己实现这个。我一直在考虑类似
this project的内容,但是当显示Hint并且将项目悬停时出现问题(以显示缩短项目的提示),Hint仍然显示。 -
@TLama 虽然并不完美,但这个解决方案似乎是一个不错的开始。我看看能不能改进。 :) 同时,发布您的解决方案,以便我接受。
标签: delphi delphi-2007 hint ownerdrawn tlistview