📄 ini.asm
字号:
;操作Ini文件
;鬼龙之舞
;=============================================================================
;include _GetAppPath.asm
_IniInit proto ;初始化,一般在窗体的wm_create或wm_initdialog中调用
_IniEnd proto ;释放,一般在窗体的wm_destroy或wm_close中调用
;--------------------------------------------------------------------------
_IniSet2Default equ <_IniInit>
;数值返回在eax里
;所有的字符串都返回在lpIniBuf,默认是255,
;如果缓冲不够大,使用_IniResizeBuffer来重新设定
_IniGetInt proto:DWORD ,:DWORD ;Section,Key
_IniGetStr proto:DWORD ,:DWORD ;Section,Key
_IniSetInt proto:DWORD ,:DWORD,:DWORD ;Section,Key,a DWORD Value
_IniSetStr proto:DWORD ,:DWORD,:DWORD ;Section,Key,address of string
;---------------------------------------------------------------------------
;根据控件的ID来读取或保存,主要用在Edit,Combo控件里
_IniGetInt2Edit proto:DWORD ,:DWORD ,:DWORD ,:DWORD ;hWnd,id,Section,Key
_IniGetStr2Edit proto:DWORD ,:DWORD ,:DWORD ,:DWORD
_IniSetIntFromEdit proto:DWORD ,:DWORD ,:DWORD ,:DWORD
_IniSetStrFromEdit proto:DWORD ,:DWORD ,:DWORD ,:DWORD
;---------------------------------------------------------------------------
_IniResizeBuffer proto:DWORD ;重新设置缓冲区的大小,默认值为MAX_PATH
_IniSetFileName proto:DWORD,:DWORD ;设定文件名,默认值为程序目录下的option.ini
_IniSetDefaultSec proto:DWORD ;设定默认的Section,可以在时传递0以简化代码,默认为[App]
;--------------------------------------------------------------------
;返回值在lpIniBuf
_IniGetSectionKeys proto :DWORD ;返回一个Section下的所有Key,以\0分开,\0\0结束
_IniGetSections proto ;返回ini文件的所有Section,以\0分开,\0\0结束
;如果缓冲不够大,使用_IniResizeBuffer来重新设定
;----------------------------------------------------------------
;删除
_IniDelSection proto :DWORD ;Section
_IniDelKey proto:DWORD,:DWORD ;Section,Key
__inisection macro
mov eax,lpszSection
.if !eax
lea eax, lpDefaultSection
.endif
exitm<eax>
endm
.data
szIniFile db 'option.ini',0
.data?
szIniFileName db MAX_PATH dup(?)
dwlpIniBufSize dd ?
lpDefaultSection db 20 dup(?)
lpIniBuf dd ? ;读取字符串的反回值
.CODE
;-------------------------------------------------------
;初始化INI文件名
;默认下将会在程序下建立一个叫option.ini的文件名
;如果要更改,可以用_IniSetFileName
_IniInit proc
invoke _GetAppPath,addr szIniFileName,TRUE
invoke lstrcat,eax,addr szIniFile
mov dwlpIniBufSize,MAX_PATH
.if lpIniBuf==0
invoke GlobalAlloc,GPTR ,MAX_PATH
mov lpIniBuf,eax
.endif
mov DWORD ptr lpDefaultSection,'ppA'
ret
_IniInit endp
;-----------------------------------------------------------
;结束
_IniEnd proc
invoke GlobalFree,lpIniBuf
ret
_IniEnd endp
;---------------------------------------------------------
;设定默认的Section
_IniSetDefaultSec proc @lpszSection
.if @lpszSection==0
mov DWORD ptr lpDefaultSection,'ppA'
.else
invoke lstrcpy,lpDefaultSection,@lpszSection
.endif
ret
_IniSetDefaultSec endp
;-----------------------------------------------------------
;设定文件名
;lpszFileName[in],文件名的文件
;bNotInAppPath[in],指出lpszFileName是否取程序的所在目录,
;如果为True,lpszFileName必须是带路径的全名
_IniSetFileName proc uses edi lpszFileName,bNotInAppPath
.if bNotInAppPath==FALSE
invoke _GetAppPath,addr szIniFileName,TRUE
.else
lea eax,szIniFileName
mov BYTE ptr [eax],0
.endif
invoke lstrcat,eax,lpszFileName
ret
_IniSetFileName endp
;---------------------------------------------------------------
;读取数值
_IniGetInt proc lpszSection,lpszKey
invoke GetPrivateProfileInt,__inisection(),lpszKey,0,addr szIniFileName
ret
_IniGetInt endp
;-------------------------------------------------------------------
;读取数值到对话框上的Edit
_IniGetInt2Edit proc hDlg,id,lpszSection,lpszKey
invoke _IniGetInt, lpszSection,lpszKey
invoke SetDlgItemInt,hDlg,id,eax,FALSE
ret
_IniGetInt2Edit endp
;---------------------------------------------------------------
;保存字符串
_IniSetStr proc lpszSection,lpszKey,lpszValue
invoke WritePrivateProfileString,__inisection(),lpszKey,lpszValue,addr szIniFileName
ret
_IniSetStr endp
;-----------------------------------------------------------------
;根据控件对话框上的Edit保存字符串
_IniSetStrFromEdit proc hDlg,id,lpszSection,lpszKey
local szValue[255]:BYTE
invoke GetDlgItemText,hDlg,id,addr szValue,255
invoke _IniSetStr,lpszSection,lpszKey,addr szValue
ret
_IniSetStrFromEdit endp
;----------------------------------------------------------------------------
_IniSetInt proc uses edi lpszSection,lpszKey,dwValue
local @szBuf[15]:BYTE
;保存数值
lea edi,@szBuf
invoke wsprintf,edi,ctext("%u"),dwValue
invoke WritePrivateProfileString,__inisection(),lpszKey,edi,addr szIniFileName
ret
_IniSetInt endp
;------------------------------------------------------------------
;根据控件对话框上的Edit保存数值
_IniSetIntFromEdit proc hDlg,id,lpszSection,lpszKey
invoke GetDlgItemInt,hDlg,id,FALSE,FALSE
invoke _IniSetInt,lpszSection,lpszKey,eax
ret
_IniSetIntFromEdit endp
;-----------------------------------------------------------------------------
;读取字符串
_IniGetStr proc lpszSection,lpszKey
invoke GetPrivateProfileString,__inisection(),lpszKey,NULL,lpIniBuf,dwlpIniBufSize,addr szIniFileName
ret
_IniGetStr endp
;---------------------------------------------------------
;读取字符串到对话框上的Edit
_IniGetStr2Edit proc hDlg,id,lpszSection,lpszKey
invoke _IniGetStr,lpszSection,lpszKey
invoke SetDlgItemText,hDlg,id,lpIniBuf
ret
_IniGetStr2Edit endp
;------------------------------------------------------
;删除Section
_IniDelSection proc lpszSection
invoke WritePrivateProfileString,lpszSection,NULL , NULL ,addr szIniFileName
ret
_IniDelSection endp
;--------------------------------------------------------
_IniDelKey proc lpszSection,lpszKey
;删除Key
invoke WritePrivateProfileString,lpszSection,lpszKey , NULL ,addr szIniFileName
ret
_IniDelKey endp
;-----------------------------------------------------------
;重新设定缓冲区大小
_IniResizeBuffer proc dwSize
push dwSize
pop dwlpIniBufSize
.if lpIniBuf
invoke GlobalFree,lpIniBuf
.endif
invoke GlobalAlloc,GPTR ,dwSize
mov lpIniBuf,eax
ret
_IniResizeBuffer endp
;-------------------------------------------------------
;返回一个Section下的所有Key,以\0隔开
_IniGetSectionKeys proc lpszSection
invoke GetPrivateProfileSection,lpszSection,lpIniBuf,dwlpIniBufSize,addr szIniFileName
ret
_IniGetSectionKeys endp
;--------------------------------
;反回所有的Section,以\0隔开
_IniGetSections proc
invoke GetPrivateProfileSectionNames,lpIniBuf,dwlpIniBufSize,addr szIniFileName
ret
_IniGetSections endp
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -