Tip of the day implementation

Often when we start a Windows application once the application window is displayed a Tip of The Day dialog gets displayed inside the window. If you observe the dialog box in Figure, you can locate a small check box at bottom left corner. When the 'Tip of The Day' dialog appears for the first time the checkbox stands checked. If you want that next time you start the application the dialog should not appear then you can uncheck the check box before dismissing the dialog. But how would the application come to know that second time around it should not display the dialog. This is achieved by storing the status of the check box in what is known as Windows Registry. As said earlier, if the check box is unchecked when the dialog appears for the first time then the dialog would not be displayed when you run the application for the second time. However, if you still wish to pop the dialog you can do so by selecting the 'Tip' menu item.
Reading/Writing from/to Registry
The Windows registry is a hierarchical database that stores application-specific information. Data needed by Windows to properly set its configuration and run applications (and itself) are all stored in this central data storage warehouse. Each node in the Registry tree is called a key; each key contains both child keys (called subkeys) and data entries (called values). A key can have any number of values, and these values can be in any form you specify. Before an application can add data to (or read data from) the registry, it must have a handle to an open key. To open a key and get a handle to the key (HKEY), the handle of a key that's already open must be supplied. Windows defines four standard key handles that are always open: HKEY_LOCAL_MACHINE, HKEY_USERS, HKEY_CLASSES_ROOT and HKEY_CURRENT_USER. The keys that represent these handles are used as entry points to the registry when creating or opening all other keys. The predefined keys are used to navigate within the registry; applications that use the registry must always work within this hierarchy of predefined keys. The only keys you typically need to be concerned with are the HKEY_CURRENT_USER and HKEY_LOCAL_MACHINE keys because this is where application specific data is written. To write the status of the check box into the registry we must first open a registry key. In our program this has been achieved by the statement,
SetRegistryKey ( "Our Tip Of The Day" ) ;
This call creates the following entry in the Windows' Registry:
Thus, another branch titled 'Our Tip of The Day' gets created inside the 'HKEY_CURRENT_USER\Software' branch. This branch has one more sub-branch called 'TIPOFTHEDAY', which happens to be the name of our application. Once this branch is created we can add an entry to the registry through a statement like,
WriteProfileInt ( "Config", "SHOWTIP", m_showtip ) ;
Here the value of m_showtip variable is being written against a SHOWTIP value under the 'Config' branch of 'HKEY_CURRE-NT_USER\Software\Our Tip of The Day\TIPOFTHEDAY' key. If we are to read this entry from registry we can do so using the statement,
int showtip = GetProfileInt ( "Config", "SHOWTIP", 1 ) ;
Here the last parameter is the value that would be returned by GetProfileInt( ) if it fails to find a SHOWTIP entry in the registry . The program has three classes, an application class called myapp, a frame window class called myframe and a dialog class called tipdialog. The tipdialog class has been derived from CDialog and is reponsible for management of the tip dialog. This management includes displaying a bulb bitmap, managing the check box, writing the status of the check box into the registry and reading the tip from a file and displaying it in the dialog. When the control reaches IntInstance( ) firstly the registry key is created. Next the frame window is built and using the function GetProfileInt( ) the value against SHOWTIP is read from the registry. If the value turns out to be 1 then the tip dialog is popped up. When the dialog is popped up the 'Show Tips on StartUp' check box should stand checked. To ensure this, the value of the showtip variable is passed to the tip dialog constructor. When the tip dialog is displayed the tipdialog::OnInitDialog( ) function gets called. In this function we have opened the file 'tips.txt' and then called the tipdialog::nexttip( ) function to display the tip in the dialog box.
A Tip About the Tip Dialog
Normally all the controls present in the dialog box are placed there while creating the dialog in the Resource Editor. This also includes a bitmap that you may wish to place in the dialog box. However, in this program we have achieved the same through another technique. We have placed the 'Bulb' bitmap and written the text 'Did you know' in the dialog through tipdialog's OnPaint( ) handler.
Controlling The Control's Color
When we include a control in the dialog box in the Resource Editor, we have no control over the over the color of the control. By default the control is always displayed in gray color. If we are to change the color of the control then we can do so by writing a OnCtlColor( ) handler in the tipdialog class. Note that when the control needs to be painted a WM_CTLCOLOR message is passed to the parent window of the control. In our program since we have created the controls in the tip dialog, when it's time to paint the controls the WM_CTLCOLOR is passed to the tipdialog class. Had the controls been placed in the frame window the WM_CTLCOLOR would have been passed to the frame window class. When the OnCtlColor( ) handler gets called a pointer to the control being painted is passed to it. Using this pointer the id of the control can be obtained by calling CWnd::GetDlgCtrlID( ). In our program we have checked whether this id happens to be the id of the static control into which the tip string is to be displayed. If it is, then we have returned a handle to the stock white brush. This ensures that the static control gets painted in white color. If the id happens to be of some other control in the dialog we simply call the base class implementation of OnCtlColor ( ).
One Last Tip
While reading the tip from the 'tip.txt' file how would one tip get distinguished from another? Simple. While creating the file make sure that in one line there is only one tip. If the length of the tip is more than that of the control in the dialog box which is going to house it, then it is automatically wrapped around to the next line. If in the file if a tip begins with a space, a tab or a semicolon it is treated as a comment and doesn't get displayed in the dialog box.

No comments: