zoukankan      html  css  js  c++  java
  • Implementing On Item Click / Double Click for TListView

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

  • 相关阅读:
    Windows 命令提示符
    力扣 ——Linked List Cycle II(环形链表 II) python实现
    力扣——Linked List Cycle(环形链表) python实现
    力扣——Copy List with Random Pointer(复制带随机指针的链表) python实现
    力扣——Reverse Nodes in k-Group(K 个一组翻转链表) python实现
    剑指offer-链表中倒数第k个结点
    剑指offer-调整数组顺序使奇数位于偶数前面
    剑指offer-数值的整数方
    剑指offer-二进制中1的个数
    剑指offer-矩形覆盖
  • 原文地址:https://www.cnblogs.com/yzryc/p/6337232.html
Copyright © 2011-2022 走看看