ListView.On Item Click & ListView.On Item Double Click

To be able to locate the clicked (if there is one) item when the OnClick event for the list view is fired, you need to determine what elements of the list view lie under the point specified by the X and Y parameters - that is the location of the mouse at the moment of "click".
The TListiew's GetHitTestInfoAt function returns information about the specified point in the list view’s client area.

To make sure the item was clicked (or double clicked) you need to call the GetHitTestInfoAt and react only if the click event occurred on an actual item.

Here's an example implementation of the ListView1's OnDblClick event:

//handles ListView1's On Double Click

procedure TForm.ListView1DblClick(Sender: TObject) ;

var

hts : THitTests;

ht : THitTest;

sht : string;

ListViewCursosPos : TPoint;

selectedItem : TListItem;

begin

//position of the mouse cursor related to ListView

ListViewCursosPos := ListView1.ScreenToClient(Mouse.CursorPos) ; //double click where?

hts := ListView1.GetHitTestInfoAt(ListViewCursosPos.X, ListViewCursosPos.Y) ; //"debug" hit test

Caption := '';

for ht in hts do begin

sht := GetEnumName(TypeInfo(THitTest), Integer(ht)) ;

Caption := Format('%s %s | ',[Caption, sht]) ;

end;

//locate the double-clicked item

if hts <= [htOnIcon, htOnItem, htOnLabel, htOnStateIcon] then

begin

selectedItem := ListView1.Selected; //do something with the double clicked item!

Caption := Format('DblClcked : %s',[selectedItem.Caption]) ;

end;

end;

In the OnDblClick (or OnClick) event handler, read the GetHitTestInfoAt function by providing it with the location of the mouse "inside" the control. To get the loction of the mouse related to the list view, the ScreenToClient function is used to convert a point (mouse X and Y) in screen coordinates to local, or client area, coordinates.

The GetHitTestInfoAt return a value of THitTests type. The THitTests is a set of THitTest enumerated values.

The THitTest enumeration values, with their description, are:

htAbove - above the client area.
htBelow - below the client area.
htNowhere - inside the control, but not on an item.
htOnItem - on an item, its text, or its bitmap.
htOnButton - on a button.
htOnIcon - on an icon.
htOnIndent - on the indented area of an item.
htOnLabel - on a label.
htOnRight - on the right side of an item.
htOnStateIcon - on a state icon or bitmap associated with an item.
htToLeft - to the left of the client area.
htToRight - to the right of the client area.
If the result of the call to GetHitTestInfoAt is a subset (Delphi sets!) of [htOnIcon, htOnItem, htOnLabel, htOnStateIcon] you can be sure the user clicked on the item (or on its icon / state icon).

Finally, if the above is true, read the Selected property of the list view, it returns the first selected item (if multiple can be selected) in the list view. Do something with the clicked / double clicked / selected item ...

e sure to download the full source code to explore the code and learn by adopting it :)

Implementing On Item Click / Double Click for TListView的更多相关文章

  1. Text selection in div(contenteditable) when double click

    背景: 在最近项目中,碰到一个问题:有一个可编辑的div需要双击时可编辑,blur或者回车时将编辑结果保存.你可能注意到双击时,文字会被选中,可编辑区域不会focus到光标位置.考虑到兼容性问题,写了 ...

  2. [Python學習筆記] 使用 selenium 抓取網頁並且雙擊滑鼠 (double click)

    一開始使用的時候 看官方文件 以為使用 double_click()即可 但後來出現錯誤 AttributeError: 'WebElement' object has no attribute 'd ...

  3. gulp die('click').live('click' composer

    gulp  die('click').live('click' composer packagist.org https://getcomposer.org/ 下载后 php composer.pha ...

  4. Implementing the On Item Checked Event for the TListView Control

    The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer d ...

  5. 前端防抖,double click 克星

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  6. 陈年佳酿之 - Winform ListView 控件 double click 事件中获取选中的row与column

    背景 最近收到了一个关于以前项目的维护请求,那时的楼主还是刚刚工作的小青年~~~ 项目之前使用的是.net/winform.今天重新打开代码,看着之前在FrameWork2.0下面的代码, 满满的回忆 ...

  7. win10家庭版,双击bat文件无法运行(double click bat file does not execute)

    win10家庭版,双击bat文件无法运行,弹出文件打开方式选择框. 在网上搜索处理办法,试了以下方法1-5都没有成功,用方法6规避. 方法1:打开一个驱动器,点“工具-文件夹选项→文件类型→新建→扩展 ...

  8. trigger click 和 click 的区别??

    trigger click 和 user click 有什么区别吗? 好像没有的.直到发现了这样一段代码. <button class="btn1">Button< ...

  9. (WPF) 基本题

    What is WPF? WPF (Windows Presentation foundation) is a graphical subsystem for displaying user inte ...

随机推荐

  1. 49个你应该了解的Android Studio技巧、插件与资源 http://www.apkbus.com/blog-822721-72630.html (出处: 安卓巴士 - 安卓开发 - Android开发 - 安卓 - 移动互联网门户)

    49个你应该了解的Android Studio技巧.插件与资源http://www.apkbus.com/blog-822721-72630.html(出处: 安卓巴士 - 安卓开发 - Androi ...

  2. golang channel 的使用

    本文对channel使用中的几个疑惑,以例子的形式加以说明. 普通channel 缺省情况下,发送和接收会一直阻塞着,直到另一方准备好. 例如: package main import ( " ...

  3. iText使用经历

    最近用iText搞了下PDF,虽然一路趟坑而过,毕竟是搞出来了,所以iText还是不错的开源PDF生成软件的.先看搞出来的PDF长啥样: 然后看一路趟过来的坑: 1.表格怎么生成,具体是行怎么生成?列 ...

  4. 好强大的vim配置文件

    原文链接 http://www.cnblogs.com/ma6174/archive/2011/12/10/2283393.html

  5. Gradle Build速度加快终极方法(android studio)

    Android Studio用起来越来越顺手,但是却发现Build的速度实在不敢恭维,在google和度娘了几把(....)之后,大体就是分配更高的内存,步骤:Setting-->搜索gradl ...

  6. 杂项:CDN

    ylbtech-杂项:CDN CDN的全称是Content Delivery Network,即内容分发网络.其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快 ...

  7. sentinel服务器出现大量的连接问题【转载】

    一.问题现象 redis服务端的sentinel模块存在大量的established状态的连接,并且这些连接一直不被释放,而客户端的连接数正常. 二.问题排查过程 1.根据连接状态进行推断 服务端存在 ...

  8. [UE4]C++中extern关键字浅谈

    变量声明和变量是有区别的 extern int i; //只是声明i而非定义i int j; //声明而且还定义了j 任何一个显式初始化的声明都将成为定义,而不管有没有extern,extern语句一 ...

  9. SimpleXML概述

    要处理XML 文件,有两种传统的处理思路:SAX 和DOM.SAX 基于事件触发机制,对XML 文件进行一次扫描,完成要进行的处理:DOM 则将整个XML 文件构造为一棵DOM树,通过对DOM 树的遍 ...

  10. vmware12共享windows的文件给虚拟的linux

    1:首先我的vmware的版本是12的 点击vmware的虚拟机---------------------->设置------------------------>选项---------- ...