c++| Registration Form
First, introduce the properties and methods of Tregistry:
There are four properties in the Tregistry class.
Property |
Type |
Describe |
Currentkey |
Int |
Contains the value of the current key value, but it does not mean too understood, so it is less common |
Rootkey |
Int |
The root key for the current key value, BCB defines enumerated constants: HKEY_CURRENT_USER, HKEY_CLASSES_ROOT, HKEY_LOCAL_MACHINE, HKEY_USERS, Hkey_current_config, HKEY_ Dyn_data, HKEY_USERS. Corresponds to the corresponding primary key in the registry respectively. Rootkye default value is HKEY_CURRENT_USER when generating an instance of a Tregistry class |
Currentpath |
Ansistring |
The text description of the current key value, \hkey_current_user\software\borland's currentpath= "Software\borland", and Rootkey=hkey_current_user |
Lazywrite |
bool |
True: Returns the application immediately as soon as a key is closed. Value is false: The registry must be completed before returning. The default value is true. |
The Tregistry class method is quite many, choose its commonly used brief description as follows:
Method |
Describe |
Closekey () |
Closes a key value and writes the data to the key value, you should close it once the key value operation is complete, but you may not have to call this method because the Tregistry destructor will call it automatically. |
DeleteKey (ansistring) |
Delete? , the parameter is the key value to be deleted, and if the argument is an empty string, the current key value is deleted |
Openkey (Ansistring,bool) |
Opens a key value, and if the key value does not exist, the second argument determines whether it is automatically established. |
Loadkey (ansistring,ansistring) |
Loads the key value from the file, the first parameter is the key value, and the second parameter is the file name |
Keyexists (ansistring) |
Detects whether a key value exists. |
Savekey (ansistring,ansistring) |
Save a key value to a file, parameter description see Loadkey () |
Readinteger (ansistring) |
read out a key value of an integer, if the failure will throw an exception. Similar to this function are Readbool (), ReadString (), Readfloat (), Readdatetome (), Readbinarydata (), and so on, to read out different types of key values. |
Valueexists (ansistring) |
Detects whether a value exists. |
Writeinteger (Ansistring,int) |
Writes a key value of an integer, if the failure throws an exception. Similar to this function are Writebool (), WriteString (), Writefloat (), Writedatetome (), Writebinarydata (), and so on, to write different types of key values. |
The use of Tregistry is easy, in general, with the following four steps:
1) Establish Tregistry class
2) using the Openkey () method to open a key value
3) Read-write key values with Readtype () and Writetype ()
4) Call Closekey () to close a key value.
It is important to note that when using the Tregistry class, you must include the header file for this class: #include $#@60; RGISTRY.HPP$#@62, otherwise what will happen, as long as a little C language Foundation will understand.
OK, let me give you an example to illustrate the use of the Tregistry class with my "Winnt automatic login."
Winnt users know that Winnt needs to log on Ctrl+alt+del at startup, even without a password. Although Winnt does not like Win9x as often as the panic, but each boot to this is a bit annoying. Can you let Winnt automatically log in every time you start? In fact, as long as the registry of a property AutoAdminLogon set to "1" can be, unfortunately this can only be done once, each time the Winnt will automatically change its value to "0" (damned bill!). If you want to change the registry every time, you might as well log in every time! Write a program to automatically modify this value after startup OK.
First create a new project, put 4 text boxes in the form, named: Duser, EDPASSWD, EDREPASSWD, Eddomain. Used to enter the username, password, password, and domain, the edpasswd and edrepasswd PasswordChar properties to "*" (password method input). Add 2 checkboxes to select whether to log on automatically (Chkauto) and Autorun (Chkautorun), a timer (Timer1), 2 buttons (Bbtok, Bbtcancel), and some label.
Open the Code window and enter the following code in the header:
#includeTRegistry ®key=* new Tregistry ();//define Global variables: Registry class. int delay=0;//defines global variables, delay time
Then select the Oncreat event for the form and enter the following code:
RegKey. rootkey=hkey_local_machine;//sets the root key//Opens the key
RegKey where the automatic login resides. Openkey ("Software\\microsoft\\windows NT\\CurrentVersion\\Winlogon", true);
//3 opens the key value for Windows Autorun. Tregistry &autorun=* new Tregistry (); Autorun.rootkey=hkey_local_machine;
Autorun.openkey ("Software\\microsoft\\windows\\currentversion\\run", true);
if (strlen (cmdline) >application->exename.length () +3)//If you are running with a parameter, you want to uninstall
{
Autorun.deletevalue ("Windows NT Auto Logon");//delete properties you have added
RegKey. DeleteValue ("Windows NT Auto Logon"); Delete &AutoRun;
Application->terminate ();//terminate application return; ///write this program to Autorun.
Autorun.writestring ("Windows NT Auto Logon", application->exename);
Delete &AutoRun;
Eddomain->text=regkey. ReadString ("DefaultDomainName"); read out the domain name
Eduser->text=regkey. ReadString ("DefaultUserName"); read out user name
chkauto->checked=true;
Chkautorun->checked=regkey. ReadString ("Windows NT Auto Logon") = = "1"? True:false; Does this program run automatically
Edpasswd->text=regkey. ReadString ("defaultpasswd"); Read the password
Setcontrol (!chkautorun->checked); If the auto run state puts other masks, and vice versa
if (chkautorun->checked)
{
timer1->enabled=true; Auto Run start timer edrepasswd->text=edpasswd->text;
}
The function of Setcontrol () is to mask several components, defined as follows:
First in the form the header file Form class definition is added void Setcontrol (BOOL);
The function body is as follows (the form is named Fmmain):
void Tfmmain::setcontrol (bool t)
{
Eduser->enabled= T;
Edrepasswd->enabled= T;
Edpasswd->enabled= T;
Eddomain->enabled= T;
Chkauto->enabled= T;
}
To determine whether to start the timer in the Chkautorun onclick event, depending on whether the autorun is selected
Setcontrol (!chkautorun->checked);//If the Autorun state puts other masks, or vice versa (!chkautorun->checked)
{timer1->enabled=false;//If not run automatically, timer fails delay=0;
}
Else
timer1->enabled=true;//Otherwise the timer works
Enter the following code in the button Bbtok onclick event, which is written to the registry.
if (edrepasswd->text==edpasswd->text)//If the password is correct
{
RegKey. WriteString ("DefaultDomainName", eddomain->text);//write domain name
RegKey. WriteString ("DefaultUserName", eduser->text);//write User name
RegKey. WriteString ("AutoAdminLogon", chkauto->checked?) 1 ": 0")/write whether to log in automatically
RegKey. WriteString ("Windows NT Auto Logon", chkautorun->checked?) 1 ": 0");/write is automatically run
RegKey. WriteString ("defaultpasswd", edpasswd->text);//write password
if (!chkautorun->checked)//If not run automatically
{
Tregistry &autorun=* new Tregistry ();
Autorun.rootkey=hkey_local_machine; Find Auto Start
Autorun.openkey ("Software\\microsoft\\windows\\currentversion\\run", true);
Autorun.deletevalue ("Windows NT Auto Logon");//delete properties you have added
Delete &AutoRun;
Application->terminate ();//program terminated.
Else
{showmessage ("Wrong password!"); edpasswd->text= "";
}
When the program is run automatically, the delay is written to the registry after 3 seconds, and the following code is written in the Timer1 OnTimer event:
delay++; if (delay>=3)//delay three seconds to bbtok->click ()//click "OK" button to write the registry
Then add the terminating program code in Bbtcancel: Application->terminate (); Our program is finished, is it really over? No! We started with new regkey and should delete it with delete, so adding delete ®key to the form's OnClose event is OK.
If you choose to run automatically, the program can delay 3 seconds each time it starts, and then quit after modifying the corresponding entry in the registry to release system resources.