zoukankan      html  css  js  c++  java
  • 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 displays files and folders.

    ViewStyle := Report; CheckBoxes := True;

    The WindowProc is a special procedure every TControl uses to respond to messages sent to the control.

    Here's how to get notified when an item in the list view is checked or un-checked:

    1. Drop a TListView (name "ListView1") on a Delphi form. Add some items to the list view.
    2. Set ViewStyle to vsReport,
    3. Set CheckBoxes to true,
    4. In the form's OnCreate event hijack the ListView1's WindowProc.
    5. If the message being processed in CN_Notify (Delphi extension to the WM_NOTIFY message) and if the notification message is "LVN_ITEMCHANGED",
    6. Read the tagNMLISTVIEW record to grab additional data.
    7. If this is a state change (LVIF_STATE) and if the state of an item changes (LVIS_STATEIMAGEMASK) grab the changed item, read it's Checked property.
    uses CommCtrl;  
    
    procedure TForm1.FormCreate(Sender: TObject) ; 
    begin 
      OriginalListViewWindowProc := ListView1.WindowProc;  
       ListView1.WindowProc := ListViewWindowProcEx; 
      end;  
      
      procedure TForm1.ListViewWindowProcEx(var Message: TMessage) ; 
      var   listItem : TListItem; 
    begin   if Message.Msg = CN_NOTIFY then   
    begin   
      if PNMHdr(Message.LParam)^.Code = LVN_ITEMCHANGED then   
        begin       
        with PNMListView(Message.LParam)^ do      
       begin        
        if (uChanged and LVIF_STATE) <> 0 then      
          begin      
               if ((uNewState and LVIS_STATEIMAGEMASK) shr 12) <> ((uOldState and LVIS_STATEIMAGEMASK) shr 12) then         
                 begin        
                      listItem := listView1.Items[iItem];             
                      memo1.Lines.Add(Format('%s checked:%s', [listItem.Caption, BoolToStr(listItem.Checked, True)])) ;           
                 end;        
                  end;       
                  end;   
                    end;   
                    end;   //original ListView message handling   
                    
                    OriginalListViewWindowProc(Message) ; 
                    end;  
                    procedure TForm1.GetCheckedButtonClick(Sender: TObject) ; 
                    var   li : TListItem; 
                 begin   
                 memo1.Lines.Clear;   memo1.Lines.Add('Checked Items:') ;   
                 for li in listView1.Items do   
                 begin   
                   if li.Checked then     
                 begin     
                   memo1.Lines.Add(Format('%s %s %s', [li.Caption, li.SubItems[0], li.SubItems[1]])) ;     
                 end;  
                end; 
               end; 

    Note: Reading the description of the tagNMLISTVIEW record in the Windows API help, reveals that "uChanged" field notifies that the item attributes have changed. This field is zero for notifications that do not use it. Otherwise, it can have the same values as the mask member of the LVITEM record. Bits 12 through 15 of the "state" member specify the state image index. To isolate these bits, use the LVIS_STATEIMAGEMASK.

  • 相关阅读:
    20155206 2017-2018-1 《信息安全系统设计基础》第3周学习总结
    20155206 第三周随堂测试补交
    20155206 2017-2018-1 《信息安全系统设计基础》第1周学习总结
    20155206 实验五 网络编程与安全
    20155206 2016-2017-2《Java程序设计》课程总结
    20155206 《Java程序设计》实验四实验报告
    第十二周课堂练习
    J-5 Java语言基础
    C-2 方法重载,比较大小
    C-1 九九乘法表
  • 原文地址:https://www.cnblogs.com/yzryc/p/6337240.html
Copyright © 2011-2022 走看看