Форум: "Основная";
Текущий архив: 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