为TListView实现项目单击/双击

Delphi's TListView control displays a list of items in columns with column headers and sub-items, or vertically or horizontally, with small or large icons.

Delphi的 TListView控件在具有列标题和子项目的列中显示项目列表,或者在垂直或水平方向上显示带有小图标或大图标的项目列表。

As do most Delphi controls, the TListView exposes the OnClick and OnDblClick (OnDoubleClick) events.

与大多数Delphi控件一样,TListView公开OnClickOnDblClick (OnDoubleClick)事件。

Unfortunately, if you need to know what item was clicked or double clicked you cannot simply handle the OnClick / OnDblClick events to get the clicked item.

不幸的是,如果您需要知道单击或双击了哪个项目,则不能简单地处理OnClick / OnDblClick事件来获取被单击的项目。

The OnClick (OnDblClick) event for the TListView is fired whenever the user clicks the control - that is whenever the "click" occurs somewhere inside the client area of the control.

每当用户单击控件时,即在控件的客户区域内某处发生“单击”时,都会触发TListView的OnClick(OnDblClick)事件。

The user can click inside the list view, BUT "miss" any of the items. What's more, since list view can change its display depending on the ViewStyle property, the user might have clicked on an item, on an item caption, on an item icon, "nowhere", on an item state icon, etc.

用户可以在列表视图中单击,但是“错过”任何项目。 此外,由于列表视图可以根据ViewStyle属性更改其显示,因此用户可能已经单击了某个项目,一个项目标题,一个项目图标,“无处”,一个项目状态图标等。

Note: the ViewStyle property determines how items are displayed in the list view: the items can be displayed as a set of movable icons, or as columns of text.

注意:ViewStyle属性确定项目在列表视图中的显示方式:这些项目可以显示为一组可移动图标或文本列。

ListView.On项目单击和ListView.On项目双击 ( 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".

为了在触发列表视图的OnClick事件时能够找到被单击(如果有)的项目,您需要确定列表视图的哪些元素位于X和Y参数指定的点下-即“单击”时鼠标的位置。

The TListiew's GetHitTestInfoAt function returns information about the specified point in the list view’s client area.

TListiew的GetHitTestInfoAtAt函数返回有关列表视图的客户区域中指定点的信息。

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.

为了确保单击(或双击)该项目,您需要调用GetHitTestInfoAt并仅在实际项目上发生click事件时做出React。

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

这是ListView1的OnDblClick事件的示例实现:

 //handles ListView1's On Double Clickprocedure 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 dobegin
    sht := GetEnumName(TypeInfo(THitTest), Integer(ht)) ;
    Caption := Format('%s %s | ',[Caption, sht]) ;end;//locate the double-clicked itemif hts <= [htOnIcon, htOnItem, htOnLabel, htOnStateIcon] thenbegin
    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 location 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.

在OnDblClick(或OnClick)事件处理程序中,通过在控件“内部”提供鼠标位置来读取GetHitTestInfoAtAt函数。 为了获得与列表视图相关的鼠标的位置,ScreenToClient函数用于将屏幕坐标中的一个点(鼠标X和Y)转换为局部坐标或客户区域坐标。

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

GetHitTestInfoAt返回THitTests类型的值。 THitTests是一组THitTest枚举值。

The THitTest enumeration values, with their description, are:

THitTest 枚举值及其说明为:

  • htAbove - above the client area.

    htAbove-客户区域上方。

  • htBelow - below the client area.

    htBelow-客户区域下方。

  • htNowhere - inside the control, but not on an item.

    htNowhere-控件内部,但不在项目上。

  • htOnItem - on an item, its text, or its bitmap.

    htOnItem-在项目,其文本或位图上。

  • htOnButton - on a button.

    htOnButton-在按钮上。

  • htOnIcon - on an icon.

    htOnIcon-在图标上。

  • htOnIndent - on the indented area of an item.

    htOnIndent-在项目的缩进区域。

  • htOnLabel - on a label.

    htOnLabel-在标签上。

  • htOnRight - on the right side of an item.

    htOnRight-在项目的右侧。

  • htOnStateIcon - on a state icon or bitmap associated with an item.

    htOnStateIcon-与项目关联的状态图标或位图上。

  • htToLeft - to the left of the client area.

    htToLeft-客户区域的左侧。

  • htToRight - to the right of the client area.

    htToRight-客户区域的右侧。

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).

如果调用GetHitTestInfoAt的结果是[htOnIcon,htOnItem,htOnLabel,htOnStateIcon]的子集(Delphi集!) ,则可以确保用户单击了该项目(或其图标/状态图标)。

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 ...

最后,如果上述条件成立,则读取列表视图的Selected属性,它将返回列表视图中的第一个选定项目(如果可以选择多个)。 对单击的项目/双击的项目/选定的项目进行操作...

Be sure to download the full source code to explore the code and learn by adopting it.

确保下载完整的源代码以探索代码并通过采用来学习。

翻译自: https://www.thoughtco.com/implementing-on-item-1058394

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:编程工作室 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值