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

  • 相关阅读:
    MySQL-DQL查询语句的其他方法查询
    MySQL-SQL的分类以及基本的操作
    1-1-4-2 Java se-常量&变量-作用域
    1-1-4-1 java se-常量&变量-基本数据类型
    1-1-2-1 java se-helloworld原理—java代码的编译和运行逻辑
    1-1-1-2 Java se-java运行环境-jvm虚拟机的跨平台性以及和jdk,jre之间的关系
    1-1-1-1 java se-java运行环境-基本单位bit和Byte
    递归
    Ultra-QuickSort (POJ 2299)树状数组+离散化
    Color the ball(HDU1556)树状数组
  • 原文地址:https://www.cnblogs.com/yzryc/p/6337232.html
Copyright © 2020-2023  润新知