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

  1. procedure TForm.ListView1DblClick(Sender: TObject) ;
  2.  
  3. var
  4.  
  5. hts : THitTests;
  6.  
  7. ht : THitTest;
  8.  
  9. sht : string;
  10.  
  11. ListViewCursosPos : TPoint;
  12.  
  13. selectedItem : TListItem;
  14.  
  15. begin
  16.  
  17. //position of the mouse cursor related to ListView
  18.  
  19. ListViewCursosPos := ListView1.ScreenToClient(Mouse.CursorPos) ; //double click where?
  20.  
  21. hts := ListView1.GetHitTestInfoAt(ListViewCursosPos.X, ListViewCursosPos.Y) ; //"debug" hit test
  22.  
  23. Caption := '';
  24.  
  25. for ht in hts do begin
  26.  
  27. sht := GetEnumName(TypeInfo(THitTest), Integer(ht)) ;
  28.  
  29. Caption := Format('%s %s | ',[Caption, sht]) ;
  30.  
  31. end;
  32.  
  33. //locate the double-clicked item
  34.  
  35. if hts <= [htOnIcon, htOnItem, htOnLabel, htOnStateIcon] then
  36.  
  37. begin
  38.  
  39. selectedItem := ListView1.Selected; //do something with the double clicked item!
  40.  
  41. Caption := Format('DblClcked : %s',[selectedItem.Caption]) ;
  42.  
  43. end;
  44.  
  45. 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. python3 获取int最大值

    python2 中获取int最大值 import sys i = sys.maxint print i 但是在python3中,报错: AttributeError: module 'sys' has ...

  2. PHP安全相关的配置(2)

    php用越来越多!安全问题更为重要!这里讲解如果安全配置php.ini 安全配置一 (1) 打开php的安全模式 php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如syst ...

  3. 豆瓣源安装python包

    例如安装scrapy: pip install -i https://pypi.douban.com/simple/ scrapy

  4. Mycat 数据库分库分表中间件

    http://www.mycat.io/ Mycat 国内最活跃的.性能最好的开源数据库中间件! 我们致力于开发高性能的开源中间件而努力! 实体书Mycat权威指南 »开源投票支持Mycat下载 »s ...

  5. vue 之radio绑定v-model

    示例: 单选radio <label ><input type="radio" value="0" v-model="branch& ...

  6. 渐变(Gradients)

    渐变是一种可以在两个或两个以上颜色之间实现平稳过渡的效果,分为线性渐变(Linear Gradients)和径向渐变(Radial Gradients). 在演示之前,先创建一个div,并添加基础样式 ...

  7. eclipse JDK 下载 and 安装 and 环境配置

    eclipse和JDK软件下载 链接:https://pan.baidu.com/s/1bpRHVIhNtK9_FMVbi34YUQ 密码:y3xr eclipse和JDK这两个软件是配套使用的,适用 ...

  8. vertical-align表单元素垂直对齐

    原文地址:http://www.blueidea.com/tech/web/2009/6910.asp 最近的项目涉及到很多表单的制作,特别是复选框(checkbox)和单选框(radio).但是在前 ...

  9. crunch字典生成

    密码破解基本有三种方法:第一种是人工猜解(垃圾桶工程和被动信息收集): 第二种是基于字典暴力破解(主流) 在kali里,是默认自带了字典的,分别放在下面三个文件中:/usr/share/wordlis ...

  10. Oracle 11g trace events

    oracle的events,是我们在做自己的软件系统时可以借鉴的 Oracle 11g trace eventsORA-10001: control file crash event1ORA-1000 ...