使用win32汇编实现一个电话本Demo
PhoneBook.inc
include windows.inc
include kernel32.inc
include user32.inc
include Comctl32.inc
include shell32.inc
includelib kernel32.lib
includelib user32.lib
includelib Comctl32.lib
includelib shell32.lib
DlgProc PROTO :HWND,:UINT,:WPARAM,:LPARAM
;图标
ICO_MAIN equ 101
;输入姓名
IDC_EDT_NAME equ 1001
;姓名标签
IDC_STC1 equ 1002
;添加号码
IDC_BTN_ADDNUM equ 1003
;删除号码
IDC_BTN_DELNUM equ 1004
;修改号码
IDC_BTN_UPNUM equ 1005
;查询号码
IDC_BTN_SELNUM equ 1006
;输入号码
IDC_EDT_NUMBER equ 1008
;显示所有联系人
IDC_EDT_DISPLAY equ 1009
;联系人组别
IDC_EDT_SEC equ 1012
IDD_DIALOG1 equ 101
;#######################################################################
.const
;ini文件名字
szFileName db '\telNum.ini',0
szSecPos db 'Windows Position',0
szFmtSelect db '%s的电话:%s',0
szKeyX db 'X',0
szKeyY db 'Y',0
szFmt1 db '%d',0
szFmtSection db '[%s]'
;换行
szCrLf db 0dh,0ah,0
lpText db '添加成功!',0
lpCaption db '提示',0
lpText1 db '没有该联系人!',0
lpText2 db '请输入电话号码!',0
lpText3 db '修改成功!',0
lpText4 db '删除成功!',0
lpText5 db '未找到联系人!',0
lpText6 db '组别,姓名,电话不能为空!',0
lpText7 db '组别,姓名不能为空!',0
;#########################################################################
.data?
hInstance dd ?
hWinMain dd ?
hIcon dd ?
szProfileName dd MAX_PATH dup(?)
szBuffer1 dd 32760 dup(?)
szBuffer2 dd 32760 dup(?)
;#########################################################################
.386
.model flat, stdcall ;32 bit memory model
option casemap :none ;case sensitive
include PhoneBook.inc
.code
; 循环遍历全部的组别和对应组别中的姓名,将其保存在缓冲区中
_EnumINI proc
local @szBuffer[256]:byte
;读取 组别 列表并循环处理
invoke SetDlgItemText,hWinMain,IDC_EDT_DISPLAY,NULL
;返回组别的名称存入szBuffer1中
invoke GetPrivateProfileSectionNames,addr szBuffer1,\
sizeof szBuffer1,addr szProfileName
mov esi,offset szBuffer1
.while byte ptr [esi]
invoke wsprintf,addr @szBuffer,addr szFmtSection,esi
invoke SendDlgItemMessage,hWinMain,IDC_EDT_DISPLAY,EM_REPLACESEL,FALSE,addr @szBuffer
; 读取 联系人 列表并循环显示
;根据组别名称,返回联系人
invoke GetPrivateProfileSection,esi,addr szBuffer2,\
sizeof szBuffer2,addr szProfileName
mov edi,offset szBuffer2
.while byte ptr [edi]
invoke SendDlgItemMessage,hWinMain,IDC_EDT_DISPLAY,EM_REPLACESEL,FALSE,edi
invoke SendDlgItemMessage,hWinMain,IDC_EDT_DISPLAY,EM_REPLACESEL,FALSE,addr szCrLf
invoke lstrlen,edi
add edi,eax
inc edi
.endw
invoke lstrlen,esi
add esi,eax
inc esi
.endw
ret
_EnumINI endp
;将当前路径和 ini 文件名组合起来
_GetPosition proc
local @szBuffer[512]:byte
invoke GetCurrentDirectory,MAX_PATH,addr szProfileName
mov esi,offset szProfileName
invoke lstrlen,esi
mov ecx,offset szFileName
.if byte ptr [esi+eax-1] == '\'
inc ecx
.endif
invoke lstrcat,esi,ecx
ret
_GetPosition endp
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke InitCommonControls
invoke DialogBoxParam,hInstance,IDD_DIALOG1,NULL,addr DlgProc,NULL
invoke ExitProcess,0
;########################################################################
DlgProc proc hWin:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
local @szSection[256]:byte ;组别
local @szKey[256]:byte ;姓名
local @szValue[256]:byte ;电话
local @szBuffer[256]:byte ;字符缓冲区
mov eax,uMsg
.if eax==WM_INITDIALOG
invoke _GetPosition
invoke _EnumINI
.elseif eax==WM_COMMAND
;获取对话框中的值
invoke GetDlgItemText,hWin,IDC_EDT_SEC,addr @szSection,sizeof @szSection
invoke GetDlgItemText,hWin,IDC_EDT_NAME,addr @szKey,sizeof @szKey
invoke GetDlgItemText,hWin,IDC_EDT_NUMBER,addr @szValue,sizeof @szValue
mov eax,wParam
;##########修改号码##########
.if ax == IDC_BTN_UPNUM
;首先查询号码是否存在
invoke GetPrivateProfileString,addr @szSection,\
addr @szKey,NULL,addr @szBuffer,\
sizeof @szBuffer,addr szProfileName
.if eax == 0
;返回0表示没有该联系人
invoke MessageBox,hWin,addr lpText1,addr lpCaption, MB_OK
.else
;非0则存在联系人,判断添加号码是否为空
.if @szValue != NULL
invoke WritePrivateProfileString,addr @szSection,\
addr @szKey,addr @szValue,addr szProfileName
invoke MessageBox,hWin,addr lpText3,addr lpCaption, MB_OK
.else
invoke MessageBox,hWin,addr lpText2,addr lpCaption, MB_OK
.endif
.endif
;##########删除号码##########
.elseif ax == IDC_BTN_DELNUM
.if @szSection == NULL || @szKey == NULL
invoke MessageBox,hWin,addr lpText7,addr lpCaption, MB_OK
.else
invoke WritePrivateProfileString,addr @szSection,\
addr @szKey,NULL,addr szProfileName
invoke MessageBox,hWin,addr lpText4,addr lpCaption, MB_OK
.endif
;##########添加号码##########
.elseif ax == IDC_BTN_ADDNUM
.if @szSection == NULL || @szKey == NULL || @szValue == NULL
invoke MessageBox,hWin,addr lpText6,addr lpCaption, MB_OK
.else
invoke WritePrivateProfileString,addr @szSection,\
addr @szKey,addr @szValue,addr szProfileName
invoke MessageBox,hWin,addr lpText,addr lpCaption, MB_OK
.endif
;##########查询号码##########
.elseif ax == IDC_BTN_SELNUM
invoke GetPrivateProfileString,addr @szSection,\
addr @szKey,NULL,addr @szValue,\
sizeof @szValue,addr szProfileName
.if eax != 0
invoke wsprintf,addr @szBuffer,addr szFmtSelect,addr @szKey,addr @szValue
invoke SetDlgItemText,hWin,IDC_EDT_DISPLAY,addr @szBuffer
.else
invoke MessageBox,hWin,addr lpText5,addr lpCaption, MB_OK
.endif
.endif
invoke _EnumINI
.elseif eax==WM_CLOSE
invoke EndDialog,hWin,0
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
DlgProc endp
end start
运行效果如图所示: