* * *

Author Topic: Opening the desired key in TreeView  (Read 418 times)

mrkaban

  • New member
  • *
  • Posts: 40
    • КонтинентСвободы
Opening the desired key in TreeView
« on: May 06, 2017, 05:32:06 pm »
Hello Dear Members! At once I apologize for the stupid questions, since I'm new! I am also grateful to you for helping me solve my previous questions!

I want to make a module for the program that will open the transferred registry key when it starts. Conventionally, in the main window, the person selected the key and clicked open, and then opens the registry viewer with the public key you need.

I found a search algorithm in TreeView of a certain text with a selection
Code: Pascal  [Select]
  1. for i := 0 to keys.Items.Count - 1 do
  2. if keys.Items[i].Text = 'HKEY_LOCAL_MACHINE' then
  3.   begin
  4.    keys.Selected:= keys.Items[i];
  5.    keys.Items[i].Expand(true);
  6.    break;
  7.   end;

But for example, to open, for example, "HKEY_LOCAL_MACHINE\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Aspia\" will have to be tricky with the individual opening of each item.

Can as that it is possible to make opening of the necessary element in TreeView easier? I will attach the code of the registry viewer. Or tell me please how best to organize a search with a subsequent selection. Thank you in advance!

Code: Pascal  [Select]
  1. // Function the path of the registry key corresponding to the specified item (item)
  2. // the tree. The path does not include the name of the root key
  3. function GetKeyPath(item: TTreeNode): String;
  4. var
  5.   temp:TTreeNode;
  6.   path: String;
  7. begin
  8.   temp := item;
  9.   while temp.Parent <> nil do
  10.   begin
  11.     path := temp.Text + '\' + path;
  12.     temp := temp.Parent;
  13.   end;
  14.   GetKeyPath := path;
  15. end;
  16.  
  17. // The function returns the handle of the registry root key for the branch,
  18. // which belongs to the item tree element
  19. function GetRootkey(item: TTreeNode): HKEY;
  20. var
  21.   temp: TTreeNode;
  22. begin
  23.   temp := item;
  24.   while temp.Parent <> nil do
  25.     temp := temp.Parent;
  26.   GetRootKey :=HKEY(temp.Data);
  27. end;
  28.  
  29. procedure TForm1.CheckSubKeys(item: TTreeNode);
  30. var
  31.   reg: TRegistry;
  32. begin
  33.   reg:= TRegistry.Create();
  34.   // Check if there are sub-sections in the registry subkey
  35.   reg.RootKey := GetRootKey(item);
  36.   if reg.OpenKeyReadOnly(GetKeyPath(item)) then
  37.   begin
  38.     if reg.HasSubKeys() then
  39.     begin
  40.      // add a dummy element (to show "+" for
  41.        // expand the partition). Simultaneously, we mark the dummy element
  42.       keys.Items.AddChild(item, '').Data := Pointer(-1);
  43.     end;
  44.     reg.CloseKey();
  45.   end;
  46.   reg.Free();
  47. end;
  48.  
  49. procedure TForm1.FormCreate(Sender: TObject);
  50. var
  51.   item: TTreeNode;
  52.   // need to open the desired key
  53.   reg: TRegistry;
  54.   valueItem: TListItem;
  55.   valueNames: TStringListUTF8;        //TStrings
  56.   i: Integer;
  57. begin
  58.   // Create a list of root registry keys
  59.   item := keys.Items.AddChild(nil, 'HKEY_CLASSES_ROOT');
  60.   item.Data := Pointer(HKEY_CLASSES_ROOT);
  61.   CheckSubKeys(item);
  62.   item := keys.Items.AddChild(nil, 'HKEY_CURRENT_USER');
  63.   item.Data := Pointer(HKEY_CURRENT_USER);
  64.   CheckSubKeys(item);
  65.   item := keys.Items.AddChild(nil, 'HKEY_LOCAL_MACHINE');
  66.   item.Data := Pointer(HKEY_LOCAL_MACHINE);
  67.   CheckSubKeys(item);
  68.   item := keys.Items.AddChild(nil, 'HKEY_USERS');
  69.   item.Data := Pointer(HKEY_USERS);
  70.   CheckSubKeys(item);
  71.   item := keys.Items.AddChild(nil, 'HKEY_CURRENT_CONFIG');
  72.   item.Data := Pointer(HKEY_CURRENT_CONFIG);
  73.   CheckSubkeys(item);
  74.  
  75.  // I try to open the required key
  76.  
  77. for i := 0 to keys.Items.Count - 1 do
  78. if keys.Items[i].Text = 'HKEY_LOCAL_MACHINE' then
  79.   begin
  80.    item:=keys.Items[i];
  81.    keys.Selected:= keys.Items[i];
  82.    keys.Items[i].Expand(true);
  83.    break;
  84.   end;
  85. end;
  86.  
  87. procedure TForm1.keysChange(Sender: TObject; Node: TTreeNode);
  88. var
  89.   reg: TRegistry;
  90.   valueItem: TListItem;
  91.   item: TTreenode;
  92.   valueNames: TStringListUTF8;        //TStrings
  93.   i: Integer;
  94. begin
  95.   item := keys.Selected;
  96.   if item <> nil then
  97.   begin
  98.     // read the contents of the selected section in the ListView (values)
  99.     values.Clear;
  100.     reg:=TRegistry.Create();
  101.     reg.RootKey := GetRootKey(item);
  102.     if reg.OpenKeyReadOnly(GetKeyPath(item)) then
  103.     begin
  104.       valueNames := TStringListUTF8.Create();
  105.       // get the list of parameter names
  106.       reg.GetValueNames(valueNames);
  107.       // add each parameter to the list
  108.       for i := 0 to valueNames.Count - 1 do
  109.       begin
  110.         valueItem := values.Items.Add();
  111.         if valueNames[i] = '' then
  112.           valueItem.Caption := '<Default>'
  113.           else
  114.             valueItem.Caption := valueNames[i];
  115. // Get the type and value of the parameter
  116.         case reg.GetDataType(valueNames[i]) of
  117.         rdUnknown:
  118.           valueItem.SubItems.Add('Unknown');
  119.         rdString, rdExpandString:
  120.           begin
  121.             valueItem.SubItems.Add('Line');
  122.             valueItem.SubItems.Add(reg.ReadString(valueNames[i]));
  123.           end;
  124.         rdBinary:
  125.           valueItem.SubItems.Add('Binary data');
  126.         end;
  127.       end;
  128.       valueNames.Free();
  129.       reg.CloseKey();
  130.     end;
  131.     reg.Free();
  132.   end;
  133. end;
  134.  
  135. procedure TForm1.keysExpanding(Sender: TObject; Node: TTreeNode;
  136.   var AllowExpansion: Boolean);
  137. var
  138.   reg: TRegistry;
  139.   subkeys: TStringListUTF8;
  140.   i: Integer;
  141. begin
  142.   if Integer(Node.getFirstChild.Data) <> -1 then
  143.     // the list of subsections has been read before
  144.     Exit;
  145.   Node.DeleteChildren(); // remove the dummy tree element
  146.   reg:=Tregistry.Create();
  147.   // load the list of subsections of the selected section
  148.   reg.Rootkey := GetRootKey(Node);
  149.   if reg.OpenKey(GetKeyPath(Node), False) then
  150.   begin
  151.     // get the list of subsections
  152.     subkeys := TStringListUTF8.Create();
  153.     reg.GetKeyNames(subkeys);
  154.     for i := 0 to subkeys.Count - 1 do
  155.     begin
  156.       // add an element for the child section (do not forget
  157.        // check the subsections of each child section)
  158.       CheckSubKeys(keys.Items.AddChild(Node, subkeys[i]));
  159.     end;
  160.     subkeys.Free();
  161.     reg.CloseKey();
  162.   end;
  163.   reg.Free();
  164. end;

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus