May 26, 2019
Hot Topics:

Responding to Tree Control Notification Messages

  • August 31, 2004
  • By Nancy Nicolaisen
  • Send Email »
  • More Articles »

Handling A Change of Item Selection

We chose to handle the TVN_SELCHANGED notification because it is sent after the user has selected a new Registry key from the tree control pane. At the least, this means that we have to clear the list control, because we only display values for one key at a time. If the newly selected Registry key has values, we also have to update the list view with the key's values.

The first step is to get a pointer to the tree view control object. We get a pointer to the treeview by casting the NMHDR structure pointer to an NM_TREEVIEW* and assigning the pointer to pNMTreeView.

void CWalkReg::OnSelchangedRegTree(NMHDR* pNMHDR, LRESULT* pResult)

Recall that when the item was created, we stored the handle of the registry key it represents in the lParam member of TVITEM. To access the key, we get its handle from itemNew.lParam:

// get key handle from tree item
HKEY hRegKey = (HKEY)pNMTreeView->itemNew.lParam;

We clear the list control with the ClistBox member ResetContent().

// clear list control

Now, we'll use the handle to the Registry key to see if the key has any values. We use the function CeRegEnumValue() to do this. The parameters, in the order shown, are the handle to a Registry key, an index that indicates which value to return, the address of a Unicode string buffer in which to return the name of the key, the size of the name buffer in Unicode characters, a NULL placeholder, the address of a DWORD variable in which to return the registry data type for this value, the address of a buffer in which to return the key data, and the size of the data buffer in bytes.

//does this key have values?
int iValueIndex  = 0;
WCHAR wszValueName[MAX_PATH];
BYTE  bValueData[1024];
DWORD dwValueNameSize = dim(wszValueName);
DWORD dwValueDataSize = dim(bValueData);
DWORD dwRegDataType;

memset( bValueData, 0x0, sizeof(bValueData));
LONG rc = CeRegEnumValue (hRegKey, iValueIndex,
                          wszValueName, &dwValueNameSize,
                          NULL, &dwRegDataType,
                          (PBYTE)&bValueData, &dwValueDataSize);

If CeRegEnumValue() returns without finding a value, we insert a message in the list control and return from OnSelchangedRegTree().

if( rc != ERROR_SUCCESS )
   pWalkRegPage->m_RegList.InsertString( -1, "No values exist for
                                              this key");
   *pResult = 0;

If there are key values found, we pass the value name, data, data size, and Registry data type to the CWalkReg member InterpretKeyValue(). This function formats the key data according to its Registry data type and inserts it in the list. (We'll explore Registry data types exhaustively when we examine InterpretKeyValue().)

Notice that before making the next call to CeRegEnumValue(), increment the value index, reinitialize dwValueNameSize and dwValueDataSize, and initialize the value data buffer. These steps are critical to iterating through the values. The index parameter moves the scan through the key's list of values. The name size and data size parameters are used in two ways: When passed in to the function, they establish the size of the buffers. When passed back, they report the size of the data that was actually copied to the buffers.

We continue to loop, testing for ERROR_SUCCESS. When the test fails, there are no more keys.

   while (rc == ERROR_SUCCESS) 
      InterpretKeyValue(  wszValueName, (PBYTE)&bValueData,
                          dwValueDataSize, dwRegDataType);

      dwValueNameSize = dim(wszValueName);
      dwValueDataSize = dim(bValueData);
      memset( bValueData, 0x0, sizeof(bValueData));

      rc = CeRegEnumValue (hRegKey, iValueIndex,
                           wszValueName, &dwValueNameSize,
                           NULL, &dwRegDataType,
                           (PBYTE)&bValueData, &dwValueDataSize);

   *pResult = 0;

Looking Ahead

Notification messages are useful because they allow great flexibility and sophistication in communication between objects, while retaining the standard format of windows messaging. In the next installment, we'll see how to insert values in the tree control.

About the Author

Nancy Nicolaisen is a software engineer who has designed and implemented highly modular Windows CE products that include features such as full remote diagnostics, CE-side data compression, dynamically constructed user interface, automatic screen size detection, and entry time data validation.

In addition to writing for Developer.com, she has written several books, including Making Win 32 Applications Mobile.

# # #

Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date