Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > VB6 > сохранение


Автор: Vadok 28.12.2003, 16:21
exclamation.gif как зделать так что бы программы сохраняла в себе все изменения confused.gif??

Автор: shilnik 29.12.2003, 05:38
Какая программа, какие изменения? Поподробней бы.

Автор: cardinal 29.12.2003, 18:22
Вот тебе вырезка из MSDN (если у тебя ее еще нет, то купи - полезная вещь):

Managing Application Settings
In Microsoft Windows 3.1 and earlier versions of Windows, program settings like window positions, files used, and other items were commonly stored in .ini files. In Windows NT, Windows 95, and later versions of Windows these program settings are stored in the system registry.

Visual Basic provides a standard registry location for storing program information for applications created in Visual Basic:

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\appname\section\key

Visual Basic also provides four statements and functions to manipulate the settings stored in your application's registry location.

Function or Statement Description
GetSetting function Retrieves registry settings.
SaveSetting statement Saves or creates registry settings.
GetAllSettings function Returns an array containing multiple registry settings.
DeleteSetting statement Deletes registry settings.


Note To view registry entries, use the Regedit application, included with Windows 95 and Windows NT.

SaveSetting and DeleteSetting Statement Example
The following example first uses the SaveSetting statement to make entries in the Windows registry (or .ini file on 16-bit Windows platforms) for the MyApp application, and then uses the DeleteSetting statement to remove them.
Код
' Place some settings in the registry.
SaveSetting appname := "MyApp", section := "Startup", _
           key := "Top", setting := 75
SaveSetting "MyApp","Startup", "Left", 50
' Remove section and all its settings from registry.
DeleteSetting "MyApp", "Startup"

GetSetting Function Example
This example first uses the SaveSetting statement to make entries in the Windows registry (or .ini file on 16-bit Windows platforms) for the application specified as appname, and then uses the GetSetting function to display one of the settings. Because the default argument is specified, some value is guaranteed to be returned. Note that section names can't be retrieved with GetSetting. Finally, the DeleteSetting statement removes all the application's entries.
Код
' Variant to hold 2-dimensional array returned by GetSetting.
Dim MySettings As Variant
' Place some settings in the registry.
SaveSetting "MyApp","Startup", "Top", 75
SaveSetting "MyApp","Startup", "Left", 50

Debug.Print GetSetting(appname := "MyApp", section := "Startup", _
                      key := "Left", default := "25")

DeleteSetting "MyApp", "Startup"

GetAllSettings Function Example
This example first uses the SaveSetting statement to make entries in the Windows registry for the application specified as appname, then uses the GetAllSettings function to display the settings. Note that application names and section names can't be retrieved with GetAllSettings. Finally, the DeleteSetting statement removes the application's entries.
Код
' Variant to hold 2-dimensional array returned by GetAllSettings
' Integer to hold counter.
Dim MySettings As Variant, intSettings As Integer
' Place some settings in the registry.
SaveSetting appname := "MyApp", section := "Startup", _
key := "Top", setting := 75
SaveSetting "MyApp","Startup", "Left", 50
' Retrieve the settings.
MySettings = GetAllSettings(appname := "MyApp", section := "Startup")
  For intSettings = LBound(MySettings, 1) To UBound(MySettings, 1)
     Debug.Print MySettings(intSettings, 0), MySettings(intSettings, 1)
  Next intSettings
DeleteSetting "MyApp", "Startup"

Автор: Vadok 29.12.2003, 20:24
cardinalизвени .....но ничего не понял......немного с английским проблема .....у меня в прогу изменяеться Caption в Label ....и мне нужно .. если я даже изменил Caption то при следушей загрузке программы там были те значения которые ...были самыми последними......" это у меня программа типа голосование" и мне надо что бы сохранялись количество голосов

Автор: shilnik 30.12.2003, 05:59
Сохранение значения в реестре:
SaveSetting "MyApp","Startup", "Top", 75
Значение 75 будет присвоено параметру Top в ветке
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\MyApp\Startup

Возвращение значения из реестра:
GetSetting("MyApp","Startup", "Top", 50)
Функция вернет 50 если ключа Top не существует.


Автор: Vadok 30.12.2003, 08:35
а куда писать это

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\MyApp\Startup

Автор: ~FoX~ 30.12.2003, 09:10
Vadok
А можно плюнуть на реестр и повозиться с *.ini - шками wink.gif

Автор: shilnik 30.12.2003, 10:25
Цитата(Vadok @ 30.12.2003, 08:35)
а куда писать это

HKEY_CURRENT_USER\Software\VB and VBA Program Settings\MyApp\Startup

Никуда не писать smile.gif
Просто VB сохраняет все именно в этой ветке:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\
дальше MyApp, Startup, Top - это то,что ты указываешь в SaveSetting

Если напишешь
SaveSetting "A1","A2", "K1", 40,
то все сохранится:
HKEY_CURRENT_USER\Software\VB and VBA Program Settings\А1\А2\К1 = 40

А вообще ~FoX~ прав можно и с ini-шками, а то реестр даже на чистой машине загажен.

Автор: Vadok 30.12.2003, 19:11
а как зделать через ini confused.gif?

Автор: shilnik 31.12.2003, 08:11
Используй API:

Private Const MAX_STRING = 1024
Описание АПи функций
Private Declare Function api_GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" ( _
ByVal lpApplicationName As String, ByVal lpKeyName As String, _
ByVal lpDefault As String, ByVal lpReturnedString As String, _
ByVal nSize As Long, ByVal lpFileName As String _
) As Long
Private Declare Function api_WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" ( _
ByVal lpAppName As String, ByVal lpKeyName As String, _
ByVal lpString As String, ByVal lpFileName As String _
) As Long
Private Declare Function api_GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" ( _
ByVal lpAppName As String, ByVal lpKeyName As String, _
ByVal nDefault As Long, ByVal lpFileName As String _
) As Long


Public Function GetPrivateProfileString(cAppName As String, cKeyName As String, cDefault As String, cFileName As String) As String
Dim sBuf As String
Dim nRet As Long
sBuf = String(MAX_STRING, vbNullChar)
nRet = api_GetPrivateProfileString(cAppName, cKeyName, cDefault, sBuf, MAX_STRING, cFileName)
sBuf = StrZ(sBuf)
GetPrivateProfileString = sBuf
End Function

Public Function WritePrivateProfileString(cAppName As String, cKeyName As String, cValue As String, cFileName As String) As Boolean
Dim nRet As Long
nRet = api_WritePrivateProfileString(cAppName, cKeyName, cValue, cFileName)
WritePrivateProfileString = True
End Function

Public Function GetPrivateProfileInt(cAppName As String, cKeyName As String, nDefault As Long, cFileName As String) As Long
Dim nRet As Long
nRet = api_GetPrivateProfileInt(cAppName, cKeyName, nDefault, cFileName)
GetPrivateProfileInt = nRet
End Function

Автор: Vadok 3.1.2004, 23:34
это через ini ??

Автор: Vadok 5.1.2004, 03:11
Люди так хто нибудь мне скажит как зделать что бы Caption Labela сохронялся и загружался с ini файла...

Автор: shilnik 5.1.2004, 06:09
Да, это через ИНИ.
Все, что было в моем предыдущем посте запихиваешь в модель, например WinAPI.mod, а в глевной форме:

Private Sub Form_Load()

Lfbel1.Caption=GetPrivateProfileString("Label", "Caption", "0", App.Path & "\Config.ini")

End Sub

Private Sub Form_Unload(Cancel As Integer)

If not WritePrivateProfileString("Label", "Caption", Lfbel1.Caption, App.Path & "\Config.ini") then
MsgBox "Ошибка записи конфигурации"
End if

End Sub

Вроде бы так, пробуй.

ЗЫ. С наступившим!!!

Автор: Vadok 5.1.2004, 21:22
shilnikнеа неработае....гдето ошибка.... мне сказали что можно без АРИ функций

Автор: cardinal 5.1.2004, 23:06
Ты сразу руки то не опускай. Поправь вот эту строку
Код
sBuf = StrZ(sBuf)

на эту
Код
sBuf = Left(sBuf, nRet)

в процедуре GetPrivateProfileString и все будет работать!

Автор: shilnik 6.1.2004, 05:41
А, извини, StrZ - это моя функция. Вот она

Public Function StrZ(lpzBuffer As String) As String
Dim nPos As Long
nPos = InStr(lpzBuffer, vbNullChar)
If nPos > 0 Then StrZ = Left(lpzBuffer, nPos - 1) Else StrZ = lpzBuffer
End Function

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)