Форум по Delphi программированию

Delphi Sources



Вернуться   Форум по Delphi программированию > Все о Delphi > [ "Начинающим" ]
Ник
Пароль
Регистрация <<         Правила форума         >> FAQ Пользователи Календарь Поиск Сообщения за сегодня Все разделы прочитаны

Ответ
 
Опции темы Поиск в этой теме Опции просмотра
  #1  
Старый 13.01.2010, 22:08
exy exy вне форума
Прохожий
 
Регистрация: 13.01.2010
Сообщения: 27
Репутация: 10
По умолчанию Сохранение настроек программы в реестр

Люди помогите пожалуйста! у меня есть прога в которой я хочу реализовать сохранение настроек проги в реестр. только вот не понимаю я этот реестр... Люди помогите может кто то напишет модуль для этого дела(хотя бы примерный), а то я не могу сам.. бьюсь бьюсь все как об стену.
Ответить с цитированием
  #2  
Старый 13.01.2010, 22:14
lmikle lmikle вне форума
Модератор
 
Регистрация: 17.04.2008
Сообщения: 8,096
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
По умолчанию

Да там все просто.
Вот пример из одной моей программы (но он сложный, т.к. я под разные разделы настроек писал служебные классы):

Код:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
unit Preferences;
 
interface
 
uses
  Windows, SysUtils, Classes, Forms, Registry, ShellAPI;
 
const
  RegBase = 'Software\Company name\Software name\';
 
type
  TOptionsBase = class
  protected
    procedure ReadStrValue(R : TRegistry; AName : String; var AValue : String);
    procedure ReadIntValue(R : TRegistry; AName : String; var AValue : Integer);
    procedure ReadBoolValue(R : TRegistry; AName : String; var AValue : Boolean);
  end;
 
  TWndLayout = class(TOptionsBase)
  private
    FWndTop : Integer;
    FWndLeft : Integer;
    FWndWidth : Integer;
    FWndHeight : Integer;
    FIsMaximized : Boolean;
 
    FDataBaseWidth : Integer;
    FDiscHeight : Integer;
    FDiscInfoWidth : Integer;
    FDiscFoldersWidth : Integer;
 
    FMediaList: TStringList;
    FFilesList : TStringList;
  public
    constructor Create;
    destructor Destroy; override;
 
    procedure Save(R : TRegistry);
    procedure Load(R : TRegistry);
 
    property WndTop : Integer read FWndTop write FWndTop;
    property WndLeft : Integer read FWndLeft write FWndLeft;
    property WndWidth : Integer read FWndWidth write FWndWidth;
    property WndHeight : Integer read FWndHeight write FWndHeight;
    property IsMaximized : Boolean read FIsMaximized write FIsMaximized;
 
    property DatabaseWidth : Integer read FDatabaseWidth write FDatabaseWidth;
    property DiscHeight : Integer read FDiscHeight write FDiscHeight;
    property DiscInfoWidth : Integer read FDiscInfoWidth write FDiscInfoWidth;
    property DiscFoldersWidth : Integer read FDiscFoldersWidth write FDiscFoldersWidth;
 
    property MediaList : TStringList read FMediaList;
    property FilesList : TStringList read FFilesList;
  end;
 
  TGeneral = class(TOptionsBase)
  private
    FLoadLast : Boolean;
    FSelectAll : Boolean;
    FLoadLastFile : String;
  public
    constructor Create;
 
    procedure Save(R : TRegistry);
    procedure Load(R : TRegistry);
 
    property LoadLast : Boolean read FLoadLast write FLoadLast;
    property SelectAll : Boolean read FSelectAll write FSelectAll;
    property LoadLastFile : String read FLoadLastFile write FLoadLastFile;
  end;
 
  TPreferences = class
  private
    FLayout : TWndLayout;
    FGeneral : TGeneral;
  public
    constructor Create;
    destructor Destroy; override;
 
    procedure Load;
    procedure Save;
 
    property Layout : TWndLayout read FLayout;
    property General : TGeneral read FGeneral;
  end;
 
procedure CallLink(ALink: String);
 
implementation
 
procedure CallLink(ALink: String);
begin
  ShellExecute(Application.Handle,PChar('open'),PChar(ALink),Nil,Nil,SW_SHOW);
end;
 
{ TOptionsBase }
 
procedure TOptionsBase.ReadBoolValue(R: TRegistry; AName: String;
  var AValue: Boolean);
begin
  If R.ValueExists(AName) Then AValue := R.ReadBool(AName);
end;
 
procedure TOptionsBase.ReadIntValue(R: TRegistry; AName: String;
  var AValue: Integer);
begin
  If R.ValueExists(AName) Then AValue := R.ReadInteger(AName);
end;
 
procedure TOptionsBase.ReadStrValue(R: TRegistry; AName: String;
  var AValue: String);
begin
  AValue := '';
  If R.ValueExists(AName) Then AValue := R.ReadString(AName);
end;
 
{ TWndLayout }
 
constructor TWndLayout.Create;
begin
  FMediaList := TStringList.Create;
  FFilesList := TStringList.Create;
 
  FWndTop := 50;
  FWndLeft := 50;
  FWndWidth := 850;
  FWndHeight := 640;
  FIsMaximized := False;
 
  FDataBaseWidth := 190;
  FDiscHeight := 250;
  FDiscInfoWidth := 240;
  FDiscFoldersWidth := 240;
end;
 
destructor TWndLayout.Destroy;
begin
  FMediaList.Free;
  FFilesList.Free;
  inherited;
end;
 
procedure TWndLayout.Load(R : TRegistry);
var
  I : Integer;
  Buf : String;
begin
  If R.OpenKeyReadOnly(RegBase + 'Layout\') Then
    Begin
      ReadIntValue(R,'WndTop',FWndTop);
      ReadIntValue(R,'WndLeft',FWndLeft);
      ReadIntValue(R,'WndWidth',FWndWidth);
      ReadIntValue(R,'WndHeight',FWndHeight);
      ReadBoolValue(R,'IsMaximized',FIsMaximized);
 
      ReadIntValue(R,'DataBaseWidth',FDataBaseWidth);
      ReadIntValue(R,'DiscHeight',FDiscHeight);
      ReadIntValue(R,'DiscInfoWidth',FDiscInfoWidth);
      ReadIntValue(R,'DiscFoldersWidth',FDiscFoldersWidth);
 
      R.CloseKey;
    End;
 
  If R.OpenKeyReadOnly(RegBase + 'Layout\Media\') Then
    Begin
      For I := 0 To FMediaList.Count-1 Do
        Begin
          ReadStrValue(R,FMediaList.Names[i],Buf);
          FMediaList.Values[FMediaList.Names[i]] := Buf;
        End;
      R.CloseKey;
    End;
 
  If R.OpenKeyReadOnly(RegBase + 'Layout\Files\') Then
    Begin
      For I := 0 To FFilesList.Count-1 Do
        Begin
          ReadStrValue(R,FFilesList.Names[i],Buf);
          If Buf <> '' Then FFilesList.Values[FFilesList.Names[i]] := Buf;
        End;
      R.CloseKey;
    End;
 
end;
 
procedure TWndLayout.Save(R : TRegistry);
var
  I : Integer;
begin
  If R.OpenKey(RegBase + 'Layout\',True) Then
    Begin
      R.WriteInteger('WndTop',FWndTop);
      R.WriteInteger('WndLeft',FWndLeft);
      R.WriteInteger('WndWidth',FWndWidth);
      R.WriteInteger('WndHeight',FWndHeight);
      R.WriteBool('IsMaximized',FIsMaximized);
 
      R.WriteInteger('DataBaseWidth',FDataBaseWidth);
      R.WriteInteger('DiscHeight',FDiscHeight);
      R.WriteInteger('DiscInfoWidth',FDiscInfoWidth);
      R.WriteInteger('DiscFoldersWidth',FDiscFoldersWidth);
 
      R.CloseKey;
    End;
 
  If R.OpenKey(RegBase + 'Layout\Media\',True) Then
    Begin
      For I := 0 To FMediaList.Count-1 Do
        R.WriteString(FMediaList.Names[i],FMediaList.ValueFromIndex[i]);
      R.CloseKey;
    End;
 
  If R.OpenKey(RegBase + 'Layout\Files\',True) Then
    Begin
      For I := 0 To FFilesList.Count-1 Do
        R.WriteString(FFilesList.Names[i],FFilesList.ValueFromIndex[i]);
      R.CloseKey;
    End;
end;
 
{ TPreferences }
 
constructor TPreferences.Create;
begin
  FLayout := TWndLayout.Create;
  FGeneral := TGeneral.Create;
end;
 
destructor TPreferences.Destroy;
begin
  FLayout.Free;
  FGeneral.Free;
  inherited;
end;
 
procedure TPreferences.Load;
var
  R : TRegistry;
begin
  R := TRegistry.Create;
  R.RootKey := HKEY_CURRENT_USER;
  Try
    FLayout.Load(R);
    FGeneral.Load(R);
  Finally
    R.Free;
  End;
end;
 
procedure TPreferences.Save;
var
  R : TRegistry;
begin
  R := TRegistry.Create;
  R.RootKey := HKEY_CURRENT_USER;
  Try
    FLayout.Save(R);
    FGeneral.Save(R);
  Finally
    R.Free;
  End;
end;
 
{ TGeneral }
 
constructor TGeneral.Create;
begin
  FLoadLast := True;
  FSelectAll := False;
  FLoadLastFile := ExtractFilePath(Application.ExeName) + 'Default.cdi';
end;
 
procedure TGeneral.Load(R: TRegistry);
begin
  If R.OpenKeyReadOnly(RegBase + 'General\') Then
    Begin
      {$I include\reg_crypt_begin.inc}
      ReadBoolValue(R,'LoadLast',FLoadLast);
      ReadStrValue(R,'LoadLastFile',FLoadLastFile);
      {$I include\reg_crypt_end.inc}
      ReadBoolValue(R,'SelectAll',FSelectAll);
 
      R.CloseKey;
    End;
end;
 
procedure TGeneral.Save(R: TRegistry);
begin
  If R.OpenKey(RegBase + 'General\',True) Then
    Begin
      R.WriteBool('LoadLast',FLoadLast);
      R.WriteBool('SelectAll',FSelectAll);
      R.WriteString('LoadLastFile',FLoadLastFile);
 
      R.CloseKey;
    End;
end;
 
end.
Ответить с цитированием
  #3  
Старый 13.01.2010, 22:37
exy exy вне форума
Прохожий
 
Регистрация: 13.01.2010
Сообщения: 27
Репутация: 10
Восклицание ответ

мне предлагали вот так сделать

Код:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
procedure TfmLogin.bbConnectClick(Sender: TObject);
var Reg: TRegistry;
begin
Screen.Cursor : = crDefault;
Reg := TRegistry.Create;
if not Reg.KeyExists('Software\Staff') then
begin
WriteStrParam('Организация','Наша организация');
WriteStrParam('Код организации','');
WriteStrParam('Начальник организации (должность)','')
WriteStrParam('Начальник организации (Ф.И.О.) ','') ;
end;
Reg.Free;
WriteStrParam('База данных',laedDatabase.Text);
WriteStrParam('Пользователь',laedUser.Text) ;

lmikle: пользуемся тегами!

но прога жалуется на WriteStrParam. и я сам в общем то не знаю что это за ф-ия
Ответить с цитированием
  #4  
Старый 13.01.2010, 23:53
lmikle lmikle вне форума
Модератор
 
Регистрация: 17.04.2008
Сообщения: 8,096
Версия Delphi: 7, XE3, 10.2
Репутация: 49089
По умолчанию

Ну, у тебя какого-то модуля нехватает.
Разбери мой пример - там все просто. Можешь не обращать внимание на классы. Просто смотри методы Save и Load у этих классов - это собственно сохраниение и чтение настроек.
Ответить с цитированием
Ответ


Delphi Sources

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск
Опции просмотра

Ваши права в разделе
Вы не можете создавать темы
Вы не можете отвечать на сообщения
Вы не можете прикреплять файлы
Вы не можете редактировать сообщения

BB-коды Вкл.
Смайлы Вкл.
[IMG] код Вкл.
HTML код Выкл.
Быстрый переход


Часовой пояс GMT +3, время: 21:38.


 

Сайт

Форум

FAQ

Соглашения

Прочее

 

Copyright © Форум "Delphi Sources" by BrokenByte Software, 2004-2025