今天搞了一天,终于完成一个程序,请大家看看,也有点问题,请大家指教:
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
include \masm32\include\comdlg32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\comdlg32.lib
winmain proto :HINSTANCE
wndproc proto :HWND,:DWORD,:DWORD,:DWORD
.const
IDM_SAVE EQU 1001
IDM_OPEN EQU 1010
MAXSIZE EQU 260
MEMSIZE EQU 65535
EditID equ 1
.data
editclass db"EDIT",0
classname db "win32"
appname db "TEXT EDIT"
ofn OPENFILENAME <?>
filterstring db "All Files",0,"*.*",0
db "Text Files",0,"*.txt",0,0
buffer db MAXSIZE dup(0)
msg MSG <?>
wc WNDCLASSEX <?>
menuname db "ams",0
.data?
hInstance DWORD ?
cmdline DWORD ?
hWnd HWND ?
hEdit HWND ?
hMem HANDLE ?
pMem DWORD ?
hFile HANDLE ?
read DWORD ?
write DWORD ?
.code
;========================================================================================第一步骤
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke GetCommandLine
mov cmdline,eax
invoke winmain,hInstance
invoke ExitProcess,eax
;======================================================================================第二步骤
winmain proc hInst:HINSTANCE
mov wc.cbSize,sizeof wc ;50
mov wc.style,CS_HREDRAW or CS_VREDRAW
mov wc.lpfnWndProc,offset wndproc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInstance
pop wc.hInstance
mov wc.hIcon,NULL
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
mov wc.hbrBackground,COLOR_WINDOW+1
mov wc.lpszClassName,offset classname
mov wc.lpszMenuName,offset menuname
mov wc.hIconSm,NULL
invoke RegisterClassEx,addr wc ;63
invoke CreateWindowEx,WS_EX_CLIENTEDGE,addr classname,addr appname,\
WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,\
320,300,NULL,NULL,hInstance,NULL
mov hWnd,eax
;invoke LoadMenu,hWnd,600 原来的菜单ID是600
;invoke SetMenu,hWnd,eax
invoke ShowWindow,hWnd,SW_SHOWNORMAL
invoke UpdateWindow,hWnd
startloop:
invoke GetMessage,addr msg,NULL,0,0
.IF eax==0
jmp asd
.endif
invoke TranslateMessage,addr msg
invoke DispatchMessage,addr msg
jmp startloop
asd:
mov eax,msg.wParam
ret
winmain endp
;======================================================================================第三步骤
wndproc proc hWind:HWND,umsg:DWORD,wParam:DWORD,lParam:DWORD
.if umsg==WM_CREATE
invoke CreateWindowEx,NULL,addr editclass,NULL,WS_VISIBLE or WS_CHILD or ES_LEFT or ES_MULTILINE or\
ES_AUTOHSCROLL or ES_AUTOVSCROLL,0,0,0,0,hWind,EditID,hInstance,NULL
mov hEdit,eax
invoke SetFocus,hEdit
mov ofn.lStructSize,sizeof ofn
push hWnd
pop ofn.hWndOwner
push hInstance
pop ofn.hInstance
mov ofn.lpstrFilter,offset filterstring
mov ofn.lpstrFile,offset buffer
mov ofn.nMaxFile,MAXSIZE
.elseif umsg==WM_SIZE
mov eax,lParam
mov edx,eax
shr edx,16
and eax,0ffffh
sub edx,20
invoke MoveWindow,hEdit,0,0,EAX,EDX,TRUE
.elseif umsg==WM_DESTROY
invoke PostQuitMessage,NULL ;106
.elseif umsg==WM_COMMAND
mov eax,wParam
.if lParam==0 ;110
;============================================================
.if eax==IDM_OPEN
mov ofn.Flags,OFN_FILEMUSTEXIST or OFN_PATHMUSTEXIST or OFN_LONGNAMES or OFN_EXPLORER or\
OFN_HIDEREADONLY
invoke GetOpenFileName,addr ofn
.if eax==TRUE
invoke CreateFile,addr buffer,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
mov hFile,eax
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE ;117
mov hMem,eax
invoke GlobalLock,hMem
mov pMem,eax
invoke ReadFile,hFile,pMem,MEMSIZE-1,addr read,NULL
invoke SendMessage,hEdit,WM_SETTEXT,NULL,pMem
invoke CloseHandle,hFile
invoke GlobalUnlock,pMem
invoke GlobalFree,hMem
.endif
invoke SetFocus,hEdit
;=======================================================
.elseif eax==IDM_SAVE
mov ofn.Flags,OFN_LONGNAMES or OFN_EXPLORER or OFN_HIDEREADONLY
invoke GetSaveFileName,addr ofn
.if eax==TRUE
invoke CreateFile,addr buffer,GENERIC_READ or GENERIC_WRITE,FILE_SHARE_READ or FILE_SHARE_WRITE,\
NULL,CREATE_NEW,FILE_ATTRIBUTE_ARCHIVE,NULL
mov hFile,eax
invoke GlobalAlloc,GMEM_MOVEABLE or GMEM_ZEROINIT,MEMSIZE
mov hMem,eax
invoke GlobalLock,hMem
mov pMem,eax
invoke SendMessage,hEdit,WM_GETTEXT,MEMSIZE-1,pMem ;140
invoke WriteFile,hFile,pMem,eax,addr write,NULL
invoke CloseHandle,hFile
invoke GlobalUnlock,pMem ;145
invoke GlobalFree,hMem
.endif
invoke SetFocus,hEdit
;=====================
.else
invoke DestroyWindow,hWind
.endif
.endif
.else
invoke DefWindowProc,hWind,umsg,wParam,lParam
ret
.endif
xor eax,eax
ret
wndproc endp
end start ;162
RC 文件:
ams MENUEX MOVEABLE IMPURE LOADONCALL DISCARDABLE ; 原来菜单名为600
BEGIN
POPUP "&File", , , 0
BEGIN
MENUITEM "&Open", 1010
MENUITEM "&SAVE",1001
END
END
但用红字部分代码时却不能正成菜单,不知为嘛???
有点感想,也有点问题