Advertisement
TLama

Untitled

Apr 12th, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.23 KB | None | 0 0
  1. unit Unit1;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, ComCtrls, CommCtrl;
  8.  
  9. type
  10.   TLVCheckStateChangingEvent = procedure(Sender: TObject; Item: TListItem; ToBeChecked: Boolean;
  11.     var AllowChange: Boolean) of object;
  12.  
  13.   TListView = class(ComCtrls.TListView)
  14.   private
  15.     FOnCheckStateChanging: TLVCheckStateChangingEvent;
  16.     procedure CNNotify(var AMessage: TWMNotifyLV); message CN_NOTIFY;
  17.   public
  18.     property OnCheckStateChanging: TLVCheckStateChangingEvent read FOnCheckStateChanging write
  19.       FOnCheckStateChanging;
  20.   end;
  21.  
  22.  
  23. type
  24.   TForm1 = class(TForm)
  25.     ListView1: TListView;
  26.     procedure FormCreate(Sender: TObject);
  27.   private
  28.     FCheckIndex: Integer;
  29.     procedure ListViewCheckStateChanging(Sender: TObject; Item: TListItem; ToBeChecked: Boolean;
  30.       var AllowChange: Boolean);
  31.   end;
  32.  
  33. var
  34.   Form1: TForm1;
  35.  
  36. implementation
  37.  
  38. {$R *.dfm}
  39.  
  40. procedure TForm1.FormCreate(Sender: TObject);
  41. var
  42.   I: Integer;
  43. begin
  44.   FCheckIndex := -1;
  45.  
  46.   ListView1.Columns.Add;
  47.   ListView1.Checkboxes := True;
  48.   ListView1.ViewStyle := vsReport;
  49.   ListView1.OnCheckStateChanging := ListViewCheckStateChanging;
  50.  
  51.   for I := 1 to 10 do
  52.     ListView1.AddItem('Item ' + IntToStr(I), nil);
  53.  
  54.   ListView1.Items[5].Checked := True;
  55. end;
  56.  
  57. { TListView }
  58.  
  59. function IsChecked(State: UINT): Boolean; inline;
  60. begin
  61.   Result := ((State and LVIS_STATEIMAGEMASK) and $2000) <> 0;
  62. end;
  63.  
  64. function IsUnchecked(State: UINT): Boolean; inline;
  65. begin
  66.   Result := ((State and LVIS_STATEIMAGEMASK) and $1000) <> 0;
  67. end;
  68.  
  69. function CheckChanged(OldState, NewState, Changed: UINT): Boolean; inline;
  70. begin
  71.   Result := (Changed = LVIF_STATE) and (((OldState and LVIS_STATEIMAGEMASK) and $3000) <>
  72.     ((NewState and LVIS_STATEIMAGEMASK) and $3000));
  73. end;
  74.  
  75. procedure TListView.CNNotify(var AMessage: TWMNotifyLV);
  76. var
  77.   AllowChange: Boolean;
  78. begin
  79.   if  AMessage.NMHdr.code = LVN_ITEMCHANGING then
  80.   begin
  81.     with AMessage.NMListView^ do
  82.     begin
  83.       if CheckChanged(uOldState, uNewState, uChanged) then
  84.       begin
  85.         AllowChange := True;
  86.  
  87.         if Assigned(FOnCheckStateChanging) then
  88.           FOnCheckStateChanging(Self, Items[iItem], IsChecked(uNewState), AllowChange);
  89.  
  90.         if not AllowChange then
  91.           AMessage.Result := 1;
  92.       end
  93.       else
  94.         inherited;
  95.     end;
  96.   end
  97.   else
  98.     inherited;
  99. end;
  100.  
  101. procedure TForm1.ListViewCheckStateChanging(Sender: TObject; Item: TListItem; ToBeChecked: Boolean;
  102.   var AllowChange: Boolean);
  103. var
  104.   OldCheck: Integer;
  105. begin
  106.   AllowChange := ((not ToBeChecked) and (Item.Index <> FCheckIndex)) or ToBeChecked;
  107.   if AllowChange and ToBeChecked then
  108.   begin
  109.     // store the check index of the item to be unchecked
  110.     OldCheck := FCheckIndex;
  111.     // remember the index of the item that will be checked
  112.     FCheckIndex := Item.Index;
  113.     // and uncheck the previously checked item if there was any; this can fail
  114.     // with an access to a not existing item; there must be added other things
  115.     // anyway, like e.g. updating the FCheckIndex field whenever the items are
  116.     // reordered, deleted etc.
  117.     if OldCheck <> -1 then
  118.       ListView1.Items[OldCheck].Checked := False;
  119.   end;
  120. end;
  121.  
  122. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement