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. I2C子系统驱动框架及应用 (转)

    I2C子系统驱动框架:     应用程序层(app层) ——————————————————————————————————– i2c driver层: 从设备驱动层(TS  Sensor等) 1. ...

  2. MySQL性能指标及计算方法(go)

    绝大多数MySQL性能指标可以通过以下两种方式获取: (1)mysqladmin 使用mysqladmin extended-status命令获得的MySQL的性能指标,默认为累计值.如果想了解当前状 ...

  3. 解决EF一对一或多对一的删除

    people 类中有 zhengshu类 且是一对一,现在要删除people类中的zhengshu 网上看了N多办法,什么更新外键什么滴. 其实方法简单极了 using (KJExamEntity c ...

  4. virtualbox centos 网络配置

    https://www.centos.bz/2017/08/virtualbox-centos7-nat-bridge/

  5. PyCharm里的五个地方utf-8有什么关系和联系?

    IDE Encoding:ide 的编码Project Encoding:项目的编码File or Director Encoding:各个文件或者目录的编码Property File Encodin ...

  6. 超链接中 utm_source, utm_medium 等参数的含义是什么?

    作者:张溪梦 Simon链接:https://www.zhihu.com/question/48724061/answer/122730629来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非 ...

  7. 图片尺寸批量resize的matlab并行代码

    在caffe ImageNet例子中有对图片进行resize的部分,文中使用的是linux shell脚本命令: for name in /path/to/imagenet/val/*.JPEG; d ...

  8. Mybatis 接口绑定

    MyBatis的接口绑定: 参考链接:http://blog.csdn.net/chris_mao/article/details/48836039 接口映射就是在IBatis中任意定义接口,然后把接 ...

  9. Python的collections模块中namedtuple结构使用示例

      namedtuple顾名思义,就是名字+元组的数据结构,下面就来看一下Python的collections模块中namedtuple结构使用示例 namedtuple 就是命名的 tuple,比较 ...

  10. 基于Linux的Samba开源共享解决方案测试(二)

    单NAS网关50Mb码率视音频文件的稳定读测试结果如下: 50Mb/s负载性能记录 NAS网关资源占用 稳定读 稳定读 CPU空闲 内存空闲 网卡占用 13个稳定流 96.70% 10G 104MB/ ...