by ささら » 2005年6月05日(日) 18:29
Tomorrowさんのご指摘も含め、全体的に修正しました。
Tomorrowさん、ご指摘有難う御座いますn(_ _)n
コード: 全て選択
'APIの宣言
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (lpAppName As BytePtr, lpKeyName As BytePtr, nDefault As Long, lpFileName As BytePtr) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpDefault As BytePtr, pReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (lpAppName As BytePtr, lpReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (lpszReturnBuffer As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
Declare Function GetPrivateProfileStruct Lib "kernel32" Alias "GetPrivateProfileStructA" (lpszSection As BytePtr,lpszKey As BytePtr,lpStruct As VoidPtr,uSizeStruct As DWord,szFile As BytePtr) As Long
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (lpAppName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileStruct Lib "kernel32" Alias "WritePrivateProfileStructA" (lpszSection As BytePtr, lpszKey As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord, szFile As BytePtr) As Long
'クラス
Class PRIVATEPROFILE
Private
ppPath[512-1] As Byte 'プロファイルのパス
ppSection[64-1] As Byte 'セクション名
ppbuf[512-1] As Byte
Public
'プロファイルの設定
Function SetPrivateProfile(lpFileName As BytePtr) As Long
dim i As Long, l As Long
i=GetModuleFileName(GetModuleHandle(0),ppPath,512)
Select Case lpFileName
Case 0
ppPath[i-3]=105
ppPath[i-2]=110
ppPath[i-1]=105
' Case Else
' GetFileTitle(Path,buf,128)
' l=lstrlen(buf)
' Path[i-l-1]=0
' lstrcat(Path,lpFileName)
End Select
End Function
'セクションの設定
Function SetSection(lpAppName As BytePtr) As Long
lstrcpy(ppSection,lpAppName)
End Function
'数値の読み込み
Function ReadInt(lpKeyName As BytePtr,nDefault As Long) As Long
ReadInt=GetPrivateProfileInt(ppSection,lpKeyName,nDefault,ppPath)
End Function
'文字列の読み込み
Function ReadString(lpKeyName As BytePtr,nDefault As BytePtr) As BytePtr
GetPrivateProfileString(ppSection,lpKeyName,nDefault,ppbuf,512,ppPath)
ReadString=ppbuf
End Function
'構造体の読み込み
Function ReadStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
GetPrivateProfileStruct(ppSection,lpKeyName,lpStruct,uSizeStruct,ppPath)
End Function
'セクションの書き込み
Function WriteSection(lpAppName As BytePtr) As Long
WritePrivateProfileSection(lpAppName,"",ppPath)
lstrcpy(ppSection,lpAppName)
End Function
'数値の書き込み
Function WriteInt(lpKeyName As BytePtr,lpValue As Long) As Long
lstrcpy(ppbuf,Str$(lpValue))
WritePrivateProfileString(ppSection,lpKeyName,ppbuf,ppPath)
End Function
'文字列の書き込み
Function WriteString(lpKeyName As BytePtr,lpString As BytePtr) As Long
WritePrivateProfileString(ppSection,lpKeyName,lpString,ppPath)
End Function
'構造体の書き込み
Function WriteStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
WritePrivateProfileStruct(ppSection,lpKeyName,lpStruct,uSizeStruct,ppPath)
End Function
End Class
こんな感じで宜しいんでしょうか?オブジェクト指向はムズかしい…(=_=)
あと、サンプルも間違っていました。スミマセン(- -;)
読み込み
コード: 全て選択
Function IniLoad()
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte
pPP.SetPrivateProfile(0)
pPP.SetSection("Main")
wp.left=pPP.ReadInt("PosX",0)
wp.top=pPP.ReadInt("PosY",0)
lstrcpy(buf,pPP.ReadString("FilePath",""))
SetDlgItemText(hMainWnd,eb_exe_path,buf)
End Function
書き込み
コード: 全て選択
Function IniSave()
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte
GetWindowRect(hMainWnd,wp)
pPP.SetPrivateProfile(0)
pPP.SetSection("Main")
pPP.WriteInt("PosX",wp.left)
pPP.WriteInt("PosY",wp.top)
GetDlgItemText(hMainWnd,eb_exe_path,buf,512)
pPP.WriteString("FilePath",buf)
End Function
外部から指定できるようにしたらどうでしょうか?
INIファイルのパス名を作るのは別クラスにでも担当させて...
あるいは元のクラスは汎用的に作っておいて、それを継承してパス限定の仕様にするとか。
とのご助言ですが、自分的には今のままでも問題ないので、
とりあえず後回しってことで…(- -;)ゞ
Tomorrowさんのご指摘も含め、全体的に修正しました。
Tomorrowさん、ご指摘有難う御座いますn(_ _)n
[code]'APIの宣言
Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPrivateProfileIntA" (lpAppName As BytePtr, lpKeyName As BytePtr, nDefault As Long, lpFileName As BytePtr) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpDefault As BytePtr, pReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (lpAppName As BytePtr, lpReturnedString As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
'Declare Function GetPrivateProfileSectionNames Lib "kernel32" Alias "GetPrivateProfileSectionNamesA" (lpszReturnBuffer As BytePtr, nSize As DWord, lpFileName As BytePtr) As DWord
Declare Function GetPrivateProfileStruct Lib "kernel32" Alias "GetPrivateProfileStructA" (lpszSection As BytePtr,lpszKey As BytePtr,lpStruct As VoidPtr,uSizeStruct As DWord,szFile As BytePtr) As Long
Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (lpAppName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (lpAppName As BytePtr, lpKeyName As BytePtr, lpString As BytePtr, lpFileName As BytePtr) As Long
Declare Function WritePrivateProfileStruct Lib "kernel32" Alias "WritePrivateProfileStructA" (lpszSection As BytePtr, lpszKey As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord, szFile As BytePtr) As Long
'クラス
Class PRIVATEPROFILE
Private
ppPath[512-1] As Byte 'プロファイルのパス
ppSection[64-1] As Byte 'セクション名
[b]ppbuf[512-1] As Byte[/b]
Public
'プロファイルの設定
Function SetPrivateProfile(lpFileName As BytePtr) As Long
dim i As Long, l As Long
i=GetModuleFileName(GetModuleHandle(0),ppPath,512)
Select Case lpFileName
Case 0
ppPath[i-3]=105
ppPath[i-2]=110
ppPath[i-1]=105
' Case Else
' GetFileTitle(Path,buf,128)
' l=lstrlen(buf)
' Path[i-l-1]=0
' lstrcat(Path,lpFileName)
End Select
End Function
'セクションの設定
Function SetSection(lpAppName As BytePtr) As Long
lstrcpy(ppSection,lpAppName)
End Function
'数値の読み込み
Function ReadInt(lpKeyName As BytePtr,nDefault As Long) As Long
ReadInt=GetPrivateProfileInt(ppSection,lpKeyName,nDefault,ppPath)
End Function
'文字列の読み込み
Function ReadString(lpKeyName As BytePtr,nDefault As BytePtr) As BytePtr
GetPrivateProfileString(ppSection,lpKeyName,nDefault,ppbuf,512,ppPath)
ReadString=ppbuf
End Function
'構造体の読み込み
Function ReadStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
GetPrivateProfileStruct(ppSection,lpKeyName,lpStruct,uSizeStruct,ppPath)
End Function
'セクションの書き込み
Function WriteSection(lpAppName As BytePtr) As Long
WritePrivateProfileSection(lpAppName,"",ppPath)
lstrcpy(ppSection,lpAppName)
End Function
'数値の書き込み
Function WriteInt(lpKeyName As BytePtr,lpValue As Long) As Long
lstrcpy(ppbuf,Str$(lpValue))
WritePrivateProfileString(ppSection,lpKeyName,ppbuf,ppPath)
End Function
'文字列の書き込み
Function WriteString(lpKeyName As BytePtr,lpString As BytePtr) As Long
WritePrivateProfileString(ppSection,lpKeyName,lpString,ppPath)
End Function
'構造体の書き込み
Function WriteStruct(lpKeyName As BytePtr, lpStruct As VoidPtr, uSizeStruct As DWord) As Long
WritePrivateProfileStruct(ppSection,lpKeyName,lpStruct,uSizeStruct,ppPath)
End Function
End Class[/code]
こんな感じで宜しいんでしょうか?オブジェクト指向はムズかしい…(=_=)
あと、サンプルも間違っていました。スミマセン(- -;)
読み込み
[code]Function IniLoad()
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte
[b]pPP.SetPrivateProfile(0)[/b]
pPP.SetSection("Main")
wp.left=pPP.ReadInt("PosX",0)
wp.top=pPP.ReadInt("PosY",0)
lstrcpy(buf,pPP.ReadString("FilePath",""))
SetDlgItemText(hMainWnd,eb_exe_path,buf)
End Function[/code]
書き込み
[code]Function IniSave()
dim pPP As PRIVATEPROFILE, wp as RECT, i As Long, buf[512-1] As Byte
GetWindowRect(hMainWnd,wp)
[b]pPP.SetPrivateProfile(0)[/b]
pPP.SetSection("Main")
pPP.WriteInt("PosX",wp.left)
pPP.WriteInt("PosY",wp.top)
GetDlgItemText(hMainWnd,eb_exe_path,buf,512)
pPP.WriteString("FilePath",buf)
End Function[/code]
[quote]外部から指定できるようにしたらどうでしょうか?
INIファイルのパス名を作るのは別クラスにでも担当させて...
あるいは元のクラスは汎用的に作っておいて、それを継承してパス限定の仕様にするとか。[/quote]
とのご助言ですが、自分的には今のままでも問題ないので、
とりあえず後回しってことで…(- -;)ゞ