source: Registry/Demo/UFormMain.pas

Last change on this file was 539, checked in by chronos, 5 years ago
  • Added: Allow to add, edit and delete registry values.
File size: 7.3 KB
Line 
1unit UFormMain;
2
3{$mode delphi}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus,
9 ActnList, ComCtrls, ExtCtrls, UGeneralRegistry;
10
11type
12
13 { TFormMain }
14
15 TFormMain = class(TForm)
16 AConnectionAdd: TAction;
17 AConnectionDelete: TAction;
18 AConnectionModify: TAction;
19 AValueAdd: TAction;
20 AValueEdit: TAction;
21 AValueDelete: TAction;
22 AImport: TAction;
23 AExport: TAction;
24 ActionList1: TActionList;
25 ListView1: TListView;
26 MainMenu1: TMainMenu;
27 MenuItem1: TMenuItem;
28 MenuItem2: TMenuItem;
29 MenuItem3: TMenuItem;
30 MenuItem4: TMenuItem;
31 MenuItem5: TMenuItem;
32 MenuItem6: TMenuItem;
33 PopupMenuValue: TPopupMenu;
34 Splitter1: TSplitter;
35 StatusBar1: TStatusBar;
36 TreeView1: TTreeView;
37 procedure AConnectionAddExecute(Sender: TObject);
38 procedure AValueAddExecute(Sender: TObject);
39 procedure AValueDeleteExecute(Sender: TObject);
40 procedure AValueEditExecute(Sender: TObject);
41 procedure FormShow(Sender: TObject);
42 procedure ListView1Data(Sender: TObject; Item: TListItem);
43 procedure ListView1DblClick(Sender: TObject);
44 private
45 function RegValueToString(ValueName: string): string;
46 procedure LoadNode(Node: TTreeNode; Key: TRegKey);
47 procedure ReloadTreeNode(Node: TTreeNode; Reg: TGeneralRegistry);
48 public
49 procedure ReloadValues;
50 procedure ReloadKeys;
51 end;
52
53var
54 FormMain: TFormMain;
55
56
57implementation
58
59uses
60 UCore, UFormValue;
61
62{$R *.lfm}
63
64{ TFormMain }
65
66procedure TFormMain.ListView1Data(Sender: TObject; Item: TListItem);
67var
68 KeyInfo: TRegKeyInfo;
69 ValueInfo: TRegValueInfo;
70 ValueNames: TStringList;
71begin
72 try
73 ValueNames := TStringList.Create;
74 Core.ActiveRegistry.GetKeyInfo(KeyInfo);
75 if (Item.Index >= 0) and (Item.Index < KeyInfo.NumberValues) then begin
76 Core.ActiveRegistry.GetValueNames(ValueNames);
77 Item.Caption := ValueNames[Item.Index];
78 Core.ActiveRegistry.GetValueInfo(ValueNames[Item.Index], ValueInfo);
79 Item.SubItems.Add(RegValueTypeName[ValueInfo.ValueType]);
80 Item.SubItems.Add(RegValueToString(ValueNames[Item.Index]));
81 end;
82 finally
83 ValueNames.Free;
84 end;
85end;
86
87procedure TFormMain.ListView1DblClick(Sender: TObject);
88begin
89 AValueEdit.Execute;
90end;
91
92procedure TFormMain.FormShow(Sender: TObject);
93begin
94 ReloadKeys;
95 ReloadValues;
96end;
97
98
99procedure TFormMain.AConnectionAddExecute(Sender: TObject);
100begin
101end;
102
103procedure TFormMain.AValueDeleteExecute(Sender: TObject);
104var
105 I: Integer;
106begin
107 if MessageDlg('Delete value', 'Do you want to delete value?', mtConfirmation,
108 [mbCancel, mbOk], 0) = mrOk then begin
109 for I := ListView1.Items.Count - 1 downto 0 do
110 if ListView1.Items[I].Selected then begin
111 ListView1.Items[I].Selected := False;
112 Core.ActiveRegistry.DeleteValue(ListView1.Items[I].Caption);
113 end;
114 ReloadValues;
115 end;
116end;
117
118procedure TFormMain.AValueAddExecute(Sender: TObject);
119var
120 FormValue: TFormValue;
121begin
122 FormValue := TFormValue.Create(nil);
123 FormValue.EditName.Text := 'New value';
124 FormValue.EditName.Enabled := True;
125 FormValue.SetValueType(vtString);
126 if FormValue.ShowModal = mrOk then begin
127 if FormValue.GetValueType = vtString then
128 Core.ActiveRegistry.WriteString(FormValue.EditName.Text, FormValue.EditValue.Text)
129 else if FormValue.GetValueType = vtInteger then
130 Core.ActiveRegistry.WriteInteger(FormValue.EditName.Text, FormValue.SpinEditValue.Value)
131 else if FormValue.GetValueType = vtBoolean then
132 Core.ActiveRegistry.WriteBool(FormValue.EditName.Text, FormValue.CheckBoxValue.Checked)
133 else raise Exception.Create('Unsupported type ' + RegValueTypeName[TRegValueType(FormValue.ComboBoxType.ItemIndex)]);
134 ReloadValues;
135 end;
136 FormValue.Free;
137end;
138
139procedure TFormMain.AValueEditExecute(Sender: TObject);
140var
141 FormValue: TFormValue;
142 ValueInfo: TRegValueInfo;
143 PrevValueType: TRegValueType;
144begin
145 FormValue := TFormValue.Create(nil);
146 FormValue.EditName.Text := ListView1.Selected.Caption;
147 FormValue.EditName.Enabled := False;
148 Core.ActiveRegistry.GetValueInfo(ListView1.Selected.Caption, ValueInfo);
149 PrevValueType := ValueInfo.ValueType;
150 FormValue.SetValueType(ValueInfo.ValueType);
151 if PrevValueType = vtString then
152 FormValue.EditValue.Text := Core.ActiveRegistry.ReadString(ListView1.Selected.Caption)
153 else if PrevValueType = vtInteger then
154 FormValue.SpinEditValue.Value := Core.ActiveRegistry.ReadInteger(ListView1.Selected.Caption)
155 else if PrevValueType = vtBoolean then
156 FormValue.CheckBoxValue.Checked := Core.ActiveRegistry.ReadBool(ListView1.Selected.Caption);
157 if FormValue.ShowModal = mrOk then begin
158 if FormValue.GetValueType <> PrevValueType then
159 Core.ActiveRegistry.DeleteValue(FormValue.EditName.Text);
160 if FormValue.GetValueType = vtString then
161 Core.ActiveRegistry.WriteString(FormValue.EditName.Text, FormValue.EditValue.Text)
162 else if FormValue.GetValueType = vtInteger then
163 Core.ActiveRegistry.WriteInteger(FormValue.EditName.Text, FormValue.SpinEditValue.Value)
164 else if FormValue.GetValueType = vtBoolean then
165 Core.ActiveRegistry.WriteBool(FormValue.EditName.Text, FormValue.CheckBoxValue.Checked)
166 else raise Exception.Create('Unsupported type ' + RegValueTypeName[TRegValueType(FormValue.ComboBoxType.ItemIndex)]);
167 ReloadValues;
168 end;
169 FormValue.Free;
170end;
171
172function TFormMain.RegValueToString(ValueName: string): string;
173var
174 ValueType: TRegValueType;
175 Buffer: array of Byte;
176 I: Integer;
177begin
178 with Core.ActiveRegistry do begin
179 ValueType := GetValueType(ValueName);
180 case ValueType of
181 vtBoolean: if ReadBool(ValueName) then Result := 'True' else Result := 'False';
182 vtInteger: Result := IntToStr(ReadInteger(ValueName));
183 vtString: Result := ReadString(ValueName);
184 vtFloat: Result := FloatToStr(ReadFloat(ValueName));
185 vtUnknown: Result := '?';
186 vtText: Result := ReadString(ValueName);
187 vtBinary: begin
188 SetLength(Buffer, GetValueSize(ValueName));
189 ReadBinaryData(ValueName, PByte(Buffer)^, Length(Buffer));
190 Result := '';
191 for I := 0 to Length(Buffer) - 1 do begin
192 Result := Result + IntToHex(Buffer[I], 2);
193 if I < Length(Buffer) - 1 then Result := Result + ' ';
194 end;
195 end;
196 end;
197 end;
198end;
199
200procedure TFormMain.LoadNode(Node: TTreeNode; Key: TRegKey);
201begin
202
203end;
204
205procedure TFormMain.ReloadValues;
206var
207 KeyInfo: TRegKeyInfo;
208begin
209 if Core.ActiveRegistry.GetKeyInfo(KeyInfo) then
210 ListView1.Items.Count := KeyInfo.NumberValues
211 else ListView1.Items.Count := 0;
212 ListView1.Refresh;
213end;
214
215procedure TFormMain.ReloadTreeNode(Node: TTreeNode; Reg: TGeneralRegistry);
216var
217 Keys: TStrings;
218 I: Integer;
219 NewNode: TTreeNode;
220begin
221 Keys := TStringList.Create;
222 Reg.GetKeyNames(Keys);
223 for I := 0 to Keys.Count - 1 do begin
224 NewNode := Node.TreeNodes.AddChild(Node, Keys[I]);
225 Reg.OpenKey(Keys[I], False);
226 ReloadTreeNode(NewNode, Reg);
227 end;
228 Keys.Free;
229end;
230
231procedure TFormMain.ReloadKeys;
232var
233 NewNode: TTreeNode;
234begin
235 TreeView1.Items.Clear;
236 NewNode := TreeView1.Items.AddChild(nil, 'Local computer');
237 ReloadTreeNode(NewNode, Core.ActiveRegistry);
238 NewNode.Expand(True);
239end;
240
241end.
242
Note: See TracBrowser for help on using the repository browser.