Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "Основная";
Текущий архив: 2004.05.30;
Скачать: [xml.tar.bz2];

Вниз

TreeView   Найти похожие ветки 

 
Sergo ©   (2004-05-17 10:26) [0]

Поставил CheckBox"s на ветки в TreeView при помощи
SetWindowLong(TreeView1.handle,GWL_STYLE,GetWindowLong(TreeView1.handle,GWL_STYLE) or TVS_CHECKBOXES);
И возник вопрос, как сделать обработчик на эти CheckBox"s?


 
Mim1 ©   (2004-05-17 11:00) [1]

Я в свое время оформил маленький компонент


unit TreeViewFQ;

interface

uses
 SysUtils, Classes, Controls, ComCtrls, Windows, Messages, CommCtrl;

type
 TTVAutoCheck = (acMarkUp,acMarkDown,acUnMarkUp,acUnmarkDown);
 TTVAutoCheckSet = set of TTVAutoCheck;
 TTreeViewCheckedChangeEvent = procedure (Sender: TObject; Node:TTreeNode; NewState:Boolean) of object;
 TTreeViewFQ = class(TTreeView)
 private
   FNodeCheckedChanged : TTreeViewCheckedChangeEvent;
   FAutoCheck : TTVAutoCheckSet;
   Procedure DoNodeCheckedChanged(Node:TTreeNode; NewState:Boolean);
   Procedure ReCheck(Node:TTreeNode);
 protected
   Procedure WMMD(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
   procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
   procedure CreateParams(var Params: TCreateParams); override;
 public
   Function GetChecked(node : TTreeNode):boolean;
   Procedure SetChecked(Node:TTreeNode; value : Boolean);
   constructor Create(AOwner: TComponent); override;
 published
   property OnNodeCheckedChanged : TTreeViewCheckedChangeEvent read FNodeCheckedChanged write FNodeCheckedChanged;
   property AutoCheck : TTVAutoCheckSet read FAutoCheck write FAutoCheck;
 end;

procedure Register;

implementation

const
 IIL_UNCHECKED = 1;
 IIL_CHECKED   = 2;

procedure Register;
begin
 RegisterComponents("Standard", [TTreeViewFQ]);
end;

{ TTreeViewFQ }

constructor TTreeViewFQ.Create(AOwner: TComponent);
begin
 inherited;
 FAutoCheck := [];
end;

procedure TTreeViewFQ.CreateParams(var Params: TCreateParams);
begin
 inherited;
 with Params do Style := Style or TVS_CHECKBOXES;
end;

procedure TTreeViewFQ.DoNodeCheckedChanged(Node: TTreeNode;
 NewState: Boolean);
begin
 ReCheck(Node);  
 if Assigned(FNodeCheckedChanged) then
   FNodeCheckedChanged(Self,Node,NewState);
end;

function TTreeViewFQ.GetChecked(node : TTreeNode): boolean;
var
 Item:TTvItem;
begin
 Item.hItem := Node.ItemId;
 Item.Mask := TVIF_STATE;
 Item.StateMask := TVIS_STATEIMAGEMASK;
 if not TreeView_GetItem(self.Handle,Item) then Abort;
 Result := ((integer(Item.State) and INDEXTOSTATEIMAGEMASK(IIL_CHECKED)) =
   INDEXTOSTATEIMAGEMASK(IIL_CHECKED));
end;

procedure TTreeViewFQ.ReCheck(Node: TTreeNode);
var curstate, b : Boolean;
   i:integer;
begin
 curstate := GetChecked(Node);

 if (acMarkUp in FAutoCheck) and curstate then
   if Node.Parent <> nil then
     SetChecked(Node.Parent,true);

 if (acMarkDown in FAutoCheck) and curstate then
   for i:= 0 to Node.Count -1 do
     SetChecked(node[i],true);

 if (acUnMarkUp in FAutoCheck) and not curstate then
   if Node.Parent <> nil then
     begin
       b := true;
       for i:= 0 to Node.Parent.Count -1 do
         if GetChecked(Node.Parent[i]) then
           begin
             b := false;
             break;
           end;
       if b then SetChecked(Node.Parent,false);
     end;

 if (acUnMarkDown in FAutoCheck) and not curstate then
   for i:= 0 to Node.Count -1 do
     SetChecked(node[i],false);
end;

procedure TTreeViewFQ.SetChecked(Node: TTreeNode; value: Boolean);
var
 Item:TTvItem;
begin
 if value = GetChecked(Node) then exit;
 
 Item.hItem := Node.ItemId;
 Item.Mask := TVIF_STATE;
 Item.StateMask := TVIS_STATEIMAGEMASK;
 if not TreeView_GetItem(self.Handle, Item) then Exit;
 if value then
   Item.State := INDEXTOSTATEIMAGEMASK(IIL_CHECKED)
 else
   Item.State := INDEXTOSTATEIMAGEMASK(IIL_UNCHECKED);
 TreeView_SetItem(self.Handle,Item);

 ReCheck(Node);
end;

procedure TTreeViewFQ.WMKeyDown(var Message: TWMKeyDown);
var tn1,tn2:TTreeNode;
   b:Boolean;
begin
 if Message.CharCode <> VK_SPACE then
   begin
     inherited;
     exit;
   end;
 b:=false;
 tn1 := Selected;
 if tn1 <> nil then
   b := GetChecked(tn1);
 inherited;
 tn2 := Selected;
 if (tn1 <> nil) and (tn2 <> nil) and (tn1 = tn2) and (b <> GetChecked(tn2)) then
   DoNodeCheckedChanged(tn2,not b);
end;

procedure TTreeViewFQ.WMMD(var Message: TWMLButtonDown);
var tn1,tn2:TTreeNode;
   b:Boolean;
begin
 b:=false;
 tn1 := GetNodeAt(Message.XPos,Message.YPos);
 if tn1 <> nil then
   b := GetChecked(tn1);
 inherited;
 tn2 := GetNodeAt(Message.XPos,Message.YPos);
 if (tn1 <> nil) and (tn2 <> nil) and (tn1 = tn2) and (b <> GetChecked(tn2)) then
   DoNodeCheckedChanged(tn2,not b);
end;

end.


 
Sergo ©   (2004-05-17 13:05) [2]

Спасибо



Страницы: 1 вся ветка

Форум: "Основная";
Текущий архив: 2004.05.30;
Скачать: [xml.tar.bz2];

Наверх




Память: 0.46 MB
Время: 0.037 c
1-1084538505
Fishka
2004-05-14 16:41
2004.05.30
ComboBox и свой Hint для каждого Item


3-1084096410
Sem
2004-05-09 13:53
2004.05.30
многие ко многим


3-1084349034
dim-
2004-05-12 12:03
2004.05.30
Добавить/Удалить пользователя


3-1084034065
AVKAR
2004-05-08 20:34
2004.05.30
Lookup-поле


14-1084529163
Dimman
2004-05-14 14:06
2004.05.30
Простой ворос





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский