Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to create a TTreeView with a three state checkbox Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
07-Nov-02
Category
VCL-General
Language
Delphi 4.x
Views
126
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

I tried many combinations of GW_STYLE with TVS_CHECKBOXES or BS_AUTO3STATE and I 
can't get a three state checkbox. All I have is a plain 2 state box. Any ideas?

Answer:

Actually, you can have any number of checkbox states you like. The number of the 
images in the state image list determines the number of the states. By default, the 
image list has two bitmaps: checked and unchecked. But you are always able to add 
yours for a third (forth ...) state. The code below shows a TTreeView with 
checkboxes and a third state. I've tested it on D4 and it seemed to work alright. 
You can set the third state to the tree node by setting 3 to the StateIndex 
property in the form's OnCreate event or in any other suitable place:

1   MyTreeView1.Items[0].StateIndex := 3;
2   
3   { ... }
4   type
5     TMyTreeView = class(TTreeView)
6     protected
7       procedure CNNotify(var message: TWMNotify); message CN_NOTIFY;
8       procedure CreateParams(var Params: TCreateParams); override;
9     public
10      procedure AddNewStateImage;
11    end;
12  
13    { ... }
14  
15  procedure TMyTreeView.CreateParams(var Params: TCreateParams);
16  begin
17    inherited CreateParams(Params);
18    Params.Style := Params.Style or TVS_CHECKBOXES;
19  end;
20  
21  procedure TMyTreeView.CNNotify(var message: TWMNotify);
22  begin
23    with message do
24      if NMHdr^.code = NM_CUSTOMDRAW then
25        AddNewStateImage;
26    inherited;
27  end;
28  
29  procedure TMyTreeView.AddNewStateImage;
30  var
31    XImageList: TImageList;
32    XImage: HIMAGELIST;
33    XBitMap: TBitMap;
34    i: integer;
35  begin
36    XImage := TreeView_GetImageList(Handle, TVSIL_STATE);
37    if (XImage <> 0) and (ImageList_GetImageCount(XImage) < 4) then
38    begin
39      XImageList := TImageList.Create(Self);
40      XBitMap := TBitMap.Create;
41      try
42        XImageList.ShareImages := true;
43        XImageList.Handle := XImage;
44        XBitMap.Width := XImageList.Width;
45        XBitMap.Height := XImageList.Height;
46        XImageList.Draw(XBitMap.Canvas, 0, 0, 2, false);
47        XImageList.Add(XBitMap, nil);
48      finally
49        XImageList.Free;
50        XBitMap.Free;
51      end;
52      for i := 0 to Items.Count - 1 do
53        if Items[i].StateIndex > 0 then
54          Items[i].StateIndex := Items[i].StateIndex;
55    end;
56  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC