Practical Learning Logo

Keeping Values in the Registry

 

Introduction

The registry is a huge database that holds all necessary the information that the operating system needs to function and communicate with anything that resides in the computer. It is organized as a tree view and functions like Windows Explorer. Although it is highly used by applications installed in the computer, you can use it to store information related to your application and retrieve that information when necessary.

Most of the information in the registration is organized like a dictionary, as a combination of keys and values. In this exercise, we will create an application with a form that the user can move while using the application. When the user closes the application, we will save the location of the form in the registry. The next time the user opens the application, we will retrieve the previous location and apply it to the form.

 

Practical Learning: Creating the Application

  1. Start Borland C++ Builder with its default form
  2. Change the form's Caption to Remember my location
  3. Save the project in a new folder named Remember1
  4. Save the unit and the project with their default names
  5. In the Object Inspector, click the Events tab and double-click OnClose
  6. In the top section of the file, include the Registry.h library
     
    //---------------------------------------------------------------------------
    #include <vcl.h>
    #include <Registry.hpp>
    #pragma hdrstop
  7. Implement the Close event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
    {
        TRegistry *regKey = new TRegistry;
    
        regKey->RootKey = HKEY_CURRENT_USER;
        regKey->OpenKey("NoticeMe", true);
        regKey->WriteInteger("Left", Left);
        regKey->WriteInteger("Top", Top);
    
        regKey->CloseKey();
    }
    //---------------------------------------------------------------------------
  8. Execute the application
  9. Move the form and close it
  10. Return to the form and double-click its body
  11. In the Event section of the Object Inspector, double-click OnCreate and implement its event as follows:
     
    //---------------------------------------------------------------------------
    void __fastcall TForm1::FormCreate(TObject *Sender)
    {
        TRegistry *regKey = new TRegistry;
    
        regKey->RootKey = HKEY_CURRENT_USER;
        regKey->OpenKey("NoticeMe", true);
    
        Left = regKey->ReadInteger("Left");
        Top  = regKey->ReadInteger("Top");
    
        regKey->CloseKey();
    }
    //---------------------------------------------------------------------------
  12. Execute the application again and notice that it remembers where the form was last positioned
  13. Open the registry and examine the HKEY_CURRENT_USER node. Notice that it has a new node named NoticeMe
  14. Close the registry
 

Home Copyright 2005 FunctionX, Inc.