Display Custom Hints for TListView Sub Items
2011-03-18 14:41
471 查看
You use the TListView Delphi control to display a list of items in a fashion similar to how Windows Explorer displays files and folders. The items can be displayed in columns with column headers and sub-i items, or vertically or horizontally, with small or large icons.
TListView Sub Item Hints
In most situations, when using the list view, you will need custom hints to be displayed for every list view item. What's more, a custom hint for every list view sub item might be required.
ListView provides the OnInfoTip event which gets fired when the mouse is paused over an item in the list view.
By default, when hints are enabled (that is, when ShowHint is true), the list view displays the hint specified by its Hint property. OnInfoTip allows the list view to override this value to specify a hint that is specific to the item under the mouse.
When ViewStyle is set to vsReport the ListView displays each item on its own line with information (sub items) arranged in columns.
Unfortunately, the OnInfoTip is not fired when the mouse pauses over a sub-item.
To force a list view to display custom hints for items and sub items, you need to handle two events: OnInfoTip and OnMouseMove. Here's the implementation of the OnInfoTip event handler.
To show custom hints for every sub-item, you need to handle the OnMouseMove event.
After getting the Mouse position (Mouse.CursorPos), using ScreenToClient we get the position of the mouse in ListView1's coordinates.
Sending the LVM_SUBITEMHITTEST message to the list view fills the (list view specific) TLVHitTestInfo record. This structure helps us find which list view item or subitem is at a given position.
If the mouse is over a sub-item, we activate the hint window by calling the Application.ActivateHint method.
That's it, really.
ListView Item Hints?
The TListView exposes the Hint and ShowHint properties you use to set up whether the hint (tooltip) should be displayed for the control when the mouse hovers over it.TListView Sub Item Hints
In most situations, when using the list view, you will need custom hints to be displayed for every list view item. What's more, a custom hint for every list view sub item might be required.
ListView provides the OnInfoTip event which gets fired when the mouse is paused over an item in the list view.
By default, when hints are enabled (that is, when ShowHint is true), the list view displays the hint specified by its Hint property. OnInfoTip allows the list view to override this value to specify a hint that is specific to the item under the mouse.
When ViewStyle is set to vsReport the ListView displays each item on its own line with information (sub items) arranged in columns.
Unfortunately, the OnInfoTip is not fired when the mouse pauses over a sub-item.
To force a list view to display custom hints for items and sub items, you need to handle two events: OnInfoTip and OnMouseMove. Here's the implementation of the OnInfoTip event handler.
procedure TLVHintsForm.ListView1InfoTip(Sender: TObject; Item: TListItem; var InfoTip: string) ; begin //Show the "full" item - with all the sub items InfoTip := InfoTip + #13#10 + item.SubItems[0] + #13#10 + item.SubItems[1]; end;
To show custom hints for every sub-item, you need to handle the OnMouseMove event.
uses CommCtrl ... procedure TLVHintsForm.ListView1MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer) ; var pt: TPoint; li : TLIstItem; lvHitInfo: TLVHitTestInfo; hint : string; begin pt := ListView1.ScreenToClient(Mouse.CursorPos) ; li := ListView1.GetItemAt(pt.x, pt.y) ; //over a sub item? if li = nil then begin FillChar(lvHitInfo, SizeOf(lvHitInfo), 0) ; lvHitInfo.pt := pt; //over a sub item! if -1 <> ListView1.Perform(LVM_SUBITEMHITTEST, 0, LParam(@lvHitInfo)) then begin hint := Format('Name: %s, %s : %s',[ ListView1.Items[lvHitInfo.iItem].Caption, ListView1.Columns[lvHitInfo.iSubItem].Caption, ListView1.Items[lvHitInfo.iItem].SubItems[-1 + lvHitInfo.iSubItem]]) ; if hint <> Memo1.Lines[0] then begin Memo1.Lines.Insert(0, hint) ; //activate hint ListView1.Hint := hint; Application.ActivateHint(Mouse.CursorPos) ; end; end; end; end;
After getting the Mouse position (Mouse.CursorPos), using ScreenToClient we get the position of the mouse in ListView1's coordinates.
Sending the LVM_SUBITEMHITTEST message to the list view fills the (list view specific) TLVHitTestInfo record. This structure helps us find which list view item or subitem is at a given position.
If the mouse is over a sub-item, we activate the hint window by calling the Application.ActivateHint method.
That's it, really.
相关文章推荐
- [NodeJS] Use Now alias for custom sub-domains
- Display Actual Cost line Items for Cost Center
- SWT:Define a global keylistener for your Display
- How to get UIMenuController work for a custom view?
- Custom & Eclipse shortcuts key bindings for Sublime Text 2
- Creating Custom DirectShow SampleGrabber Filter for Windows Mobile
- BW: Hints for number range buffering
- Jquery PlugIn for lazy loading items.
- How to redirect to a Custom Page for Event Receiver in SharePoint 2010
- Utility Functions For DisplayObjects
- 慎用 JS 中的 for (var index in items) 循环数组项
- 转:Assertion failure in -[UITableView _configureCellForDisplay:forIndexPath:]
- Installation Phases and In-Script Execution for Custom Actions in Windows Installer
- 2.4使用数据项显示子元素(display children using data items)
- 在Codeproject.com上post了我的第一篇文章:Custom ComboBox server control for ASP.NET
- 序列化推荐的图模型——Selecting Sequences of Items via Submodular Maximization(更新中)
- How to Collect and Display System Statistics (CPU and IO) for CBO use
- How to create a custom directory for app files inside the ~/Library/Application Support directory.
- [Mac/Win] 查看历史命令(Display a full command prompt history for Mac and Windows OS)
- Solving the Last Item Problem for a Circular Distribution with Partially Overlapping Items