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 Microsoft Visual C++ or Visual Studio
  2. Create a Dialog-Based application named Remember1
  3. Delete the OK button and the TODO line
  4. Change the Caption of the Cancel button to Close
  5. Change the dialog's Border property to Resizing 
  6. Generate the WM_DESTROY message of the dialog box
  7. Implement its OnDestroy event as follows:
     
    void CRemember1Dlg::OnDestroy()
    {
    	CDialog::OnDestroy();
    
    	// TODO: Add your message handler code here
    	CRect rctPosition;
    	HKEY hk;
    	DWORD dwDisp;
    	TCHAR dwData[40];
    
    	// Retrieve the position and size of the dialog box
    	GetWindowRect(&rctPosition);
    
    	// Check the HKEY_CURRENT_USER key and look
    	// for the RemDlgPos node under
    	// If you don't find it, then create it
    	RegCreateKeyEx(HKEY_CURRENT_USER,
    		           "RemDlgPos",
    				   0,
    				   NULL,
    				   REG_OPTION_NON_VOLATILE,
    				   KEY_WRITE,
    				   NULL,
    				   &hk,
    				   &dwDisp);
    
    	// Retrieve the value of the x coordinate of the dialog box...
    	sprintf(dwData, "%d", rctPosition.left);
    	// ... and store it in a key named Left
    	RegSetValueEx(hk,
    		          "Left",
    				  0,
    				  REG_DWORD,
    				  (PBYTE)&dwData,
    				  sizeof(PDWORD));
    	
    	// Retrieve the value of the y coordinate of the dialog box...
    	sprintf(dwData, "%d", rctPosition.top);
    	// ... and store it in a key named Top
    	RegSetValueEx(hk,
    		          "Top",
    				  0,
    				  REG_DWORD,
    				  (PBYTE)&dwData,
    				  sizeof(PDWORD));
    
    	// Retrieve the width of the dialog box...
    	sprintf(dwData, "%d", rctPosition.Width());
    	// ... and store it in a key named Width
    	RegSetValueEx(hk,
    		          "Width",
    				  0,
    				  REG_DWORD,
    				  (PBYTE)&dwData,
    				  sizeof(PDWORD));
    
    	// Retrieve the height of the dialog box...
    	sprintf(dwData, "%d", rctPosition.Height());
    	// ... and store it in a key named Height
    	RegSetValueEx(hk,
    		          "Height",
    				  0,
    				  REG_DWORD,
    				  (PBYTE)&dwData,
    				  sizeof(PDWORD));
    
    	// We have finished with the registry,
    	// so liberate the resources we were using
    	RegCloseKey(hk);
    }
  8. Generate the WM_CREATE message of the dialog box
  9. Implement its OnCreate event as follows:
     
    int CRemember1Dlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
    	if (CDialog::OnCreate(lpCreateStruct) == -1)
    		return -1;
    
    	// TODO:  Add your specialized creation code here
    	// This variable will allow us to keep track of the KEY
    	HKEY hk;
    	// This variable will be used to hold the type of value
    	DWORD dwType;
    	// This variable will represent the size of the value
    	DWORD dwLength;
    	// These variables represent the position
    	// and the size of the dialol box
    	TCHAR strValueLeft[20], strValueTop[20],
    		  strValueWidth[20], strValueHeight[20]; 
    
    	// Check the HKEY_CURRENT_USER node and look for 
    	// the RemDlgPos node under it
    	// If it exists, open it
    	RegOpenKeyEx(HKEY_CURRENT_USER,
    		           "RemDlgPos",
    				   0,
    				   KEY_QUERY_VALUE,
    				   &hk);
    	
    	// Retrieve the value of the Left key
    	RegQueryValueEx(hk,
    		             "Left",
    		             NULL,
    		             NULL,
    		             (LPBYTE)strValueLeft,
    		             &dwLength);
    	
    	// Retrieve the value of the Top key
    	RegQueryValueEx(hk,
    		        "Top",
    				NULL,
    				&dwType,
    				(LPBYTE)strValueTop,
    				&dwLength);
    	
    	// Retrieve the value of the Width key
    	RegQueryValueEx(hk,
    		        "Width",
    				NULL,
    				&dwType,
    				(LPBYTE)strValueWidth,
    				&dwLength);
    	
    	// Retrieve the value of the Height key
    	RegQueryValueEx(hk,
    		        "Height",
    				NULL,
    				&dwType,
    				(LPBYTE)strValueHeight,
    				&dwLength);
    
    	// We have finished reading the registry,
    	// so free the resources we were using
    	RegCloseKey(hk);
    
    	// Use the values retrieved from the retrieve to place the dialog box
    	// where it was the previous time
    	this->SetWindowPos(&wndTop, atoi(strValueLeft), atoi(strValueTop),
    		               atoi(strValueWidth), atoi(strValueHeight), SWP_SHOWWINDOW);
    
    	return 0;
    }
  10. Execute the application
  11. Resize the move the dialog box around
  12. Close the dialog box
  13. Execute the application again and notice that it remembers where the dialog box was last positioned
  14. Open the registry and examine the HKEY_CURRENT_USER node. Notice that it has a new node named RegDlgPos
  15. Close the registry
 

Home Copyright © 2005-2016, FunctionX