IE Advanced Options-like Tree View

.(Beijing, P.R.China)

When I am programming, I find a boring work is to
implement many options. Normally I have to do the following
works:

1.Declare many variants to identify the options.

2.Implement user interface to set the options.
3.Load the value from registry for each option.
4.Save the value to registry for each option.

I had to write lots of codes to do the work and
the codes looks like the same. so I build two classes to
implement the works in a simple way.

The class COption hold all the options, and the
class COptionsTreeCtrl will supply a TreeCtrl to show and modify
the options. e.g.

There are two type of options: Radio and Check.
To a Radio option, it can be set to the value of 0, 1, …, to a
Check Option, it can be set to the value of
COptions::CheckTrue(-1) or COptions::CheckFalse(0).

Each option can have three appearence properties:
text ,true image and false image. the properties will be used in
COptionsTreeCtrl to show the option item.

There are two functions to load and save all the
options from the registry.

Usage:

1.Declare a member of COptions to hold all
options in your porgram.

COptions m_Options;

2.Initialize a option in the member.

m_Options.SetOption("RadioOption1",
0);

The string
"RadioOption1" is the identifier, and will be used in
two way:
a)Get
the option value:

m_Options["RadioOption1"]

b)As the key string when save it in registry.
The second parameter is the default value, before loading from
registry, the option will be the value of second parameter. To a
Check Option, it can be COptions::CheckTrue or
COptions::CheckFalse. To a Radio Option, it can be 0, 1, …

3.Initialize a option
appearence in the COptionTreeCtrl.

m_Options.SetOptionItem("RadioOption1",
"\tSelection 0", 0, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOptionItem("RadioOption1",
"\tSelection 1", 1, IDB_RADIOON, IDB_RADIOOFF);

a)The first parameter is the ID of the Option.
b)the
second parameter is the text which will be shown in the tree. the
char ‘\t’ is the indent mark, one ‘\t’ means the item will be
child of the root. and two ‘\t’ (‘\t\t’) means the item will be
the child of the child of the root. Normally before the ‘\t\t’
item, there must be a ‘\t’ item, and so on.
c)the
third parameter is the true value of this item. A Radio option
normally has many selections, one selection should have a
appearence, so one selection should have a code line like the
forgoing. each line will have its own true value. When the value
of the option equal to the true value of this line, the Image of
the item in the COptionsTreeCtrl will be the bitmap resource
identified by parameter fourth, otherwise, the item will be shown
in bitmap resource identified by the fifth parameter.
d)To
Initialize a item which doesn’t been associated with a option,
(e.g. the first line in the front picture) you can use the
following line:

m_Options.SetOptionItem("Radio
Options", IDB_ITEMS, IDB_ITEMS);

The parameters are the ID
,image, selected image.

4.Load and save.

m_Options.RegLoad("Settings");
m_Options.RegSave("Settings");

The two functions will load
or save all the options in the class COption, each option will
use the ID as its key name.The parameter is the section name of
all options.

5.Show and Modify.
The Options can be shown in a COptionsTreeCtrl and modified
automaticly when click the image of a item/Option.
The following is a sample. A COptionsTreeCtrl is placed in a
dialog(COptionsDlg):

COptionsDlg dlg;
dlg.m_OptionsTreeCtrl.m_pOptions = &m_Options;
m_Options.BeginTrans();
if (dlg.DoModal() == IDOK)
m_Options.CommitTrans();
else
m_Options.RollbackTrans();

In the COptionsDlg
Initialize Message handler, you must add the following line:

m_OptionsTreeCtrl.Initialize(NULL);

The item text in the
COptionsTreeCtrl equal to the string in the parameter will be
expanded. Null to expand all the items in the COptionsTreeCtrl.

6.Sample:
The options in the front picture will be generated by the
following codes:

m_Options.SetOption("RadioOption1",
0);
m_Options.SetOption("RadioOption2", 1);
m_Options.SetOptionItem("Radio Options", IDB_ITEMS,
IDB_ITEMS);
m_Options.SetOptionItem("RadioOption1",
"\tSelection 0", 0, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOptionItem("RadioOption1",
"\tSelection 1", 1, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOptionItem("RadioOption1",
"\tSelection 2", 2, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOptionItem("RadioOption2",
"\t\tSelection 0", 0, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOptionItem("RadioOption2",
"\t\tSelection 1", 1, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOptionItem("RadioOption1",
"\tSelection 3", 3, IDB_RADIOON, IDB_RADIOOFF);

m_Options.SetOptionItem("Check Options", IDB_ITEMS,
IDB_ITEMS);
m_Options.SetOption("CheckOption1",
COptions::CheckFalse);
m_Options.SetOptionItem("CheckOption1", "\tCheck
1", COptions::CheckTrue, IDB_CHECKON, IDB_CHECKOFF);
m_Options.SetOption("RadioOption3", 1);
m_Options.SetOptionItem("RadioOption3",
"\t\tSelection 0", 0, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOptionItem("RadioOption3",
"\t\tSelection 1", 1, IDB_RADIOON, IDB_RADIOOFF);
m_Options.SetOption("CheckOption2",
COptions::CheckTrue);
m_Options.SetOptionItem("CheckOption2", "\tCheck
2", COptions::CheckTrue, IDB_CHECKON, IDB_CHECKOFF);

Downloads

Download demo project – 36 Kb
Download source – 6 Kb

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read