win32汇编环境,窗口程序中对按钮控件常用操作的示例
;运行效果
;win32汇编环境,窗口程序中对按钮控件常用操作的示例
;常用的操作,例如创建按钮控件,使其无效,改变文本,得到文本等。
;将代码复制进radasm软件里,直接就可以编译运行。重点部分加备注。
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat,stdcall
option casemap:none
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include gdi32.inc
includelib gdi32.lib
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
WinMain proto :DWORD,:DWORD,:DWORD,:DWORD
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.DATA
ClassName db "SimpleWinClass",0
AppName db "窗口程序的框架",0
szButtonClassName db "button",0 ;按钮类名
sz_FontName db "宋体 ",0
szTitle db "提示",0
szButtonTitle db "我是被测试的按钮",0
szButtonTitle10 db "我是改变后的文字",0
szButtonTitle11 db "第1个按钮是有效的",0
szButtonTitle12 db "第1个按钮是无效的",0
szButtonTitle01 db "改变第1个按钮的文字",0
szButtonTitle02 db "得到第1个按钮的文字",0
szButtonTitle03 db "使第1个按钮无效",0
szButtonTitle04 db "使第1个按钮有效",0
szButtonTitle05 db "判断第1个按钮是否有效",0
.DATA?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hButton HWND ? ;按钮控件句柄变量
hButton01 HWND ?
hButton02 HWND ?
hButton03 HWND ?
hButton04 HWND ?
hButton05 HWND ?
.const;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; Equ 等值定义
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ButtonID equ 10 ;按钮控件标识符
ButtonID01 equ 11
ButtonID02 equ 12
ButtonID03 equ 13
ButtonID04 equ 14
ButtonID05 equ 15
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.CODE
start:
invoke GetModuleHandle, NULL
mov hInstance,eax
invoke GetCommandLine
mov CommandLine,eax
invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
invoke ExitProcess, eax
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:DWORD
LOCAL wc:WNDCLASSEX
LOCAL msg:MSG
LOCAL hwnd:HWND
mov wc.cbSize,SIZEOF WNDCLASSEX
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.hbrBackground,COLOR_WINDOW+1
mov wc.lpszMenuName,NULL
mov wc.lpszClassName,OFFSET ClassName
invoke LoadIcon,NULL,IDI_APPLICATION
mov wc.hIcon,eax
mov wc.hIconSm,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
invoke RegisterClassEx, addr wc
invoke CreateWindowEx,NULL, ADDR ClassName,ADDR AppName,WS_CAPTION or WS_SYSMENU or WS_MINIMIZEBOX,100,100,250,340, NULL,NULL,hInst, NULL ;创建主窗口
mov hwnd,eax
invoke ShowWindow, hwnd,CmdShow
invoke UpdateWindow, hwnd
.while TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.break .if (!eax)
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
.endw
mov eax,msg.wParam
ret
WinMain endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM
LOCAL @DEFAULT_GUI_FONT,@OLD_DEFAULT_GUI_FONT
LOCAL @szBuffer[256]:byte
.if uMsg == WM_CREATE
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,30,200,25,hWnd,ButtonID,NULL,NULL ;创建按钮控件
mov hButton,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle01, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,100,200,25,hWnd,ButtonID01,NULL,NULL
mov hButton01,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle02, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,140,200,25,hWnd,ButtonID02,NULL,NULL
mov hButton02,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle03, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,180,200,25,hWnd,ButtonID03,NULL,NULL
mov hButton03,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle04, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,220,200,25,hWnd,ButtonID04,NULL,NULL
mov hButton04,eax
invoke CreateWindowEx,NULL,ADDR szButtonClassName,ADDR szButtonTitle05, WS_TABSTOP OR WS_VISIBLE OR WS_CHILD OR BS_DEFPUSHBUTTON ,20,260,200,25,hWnd,ButtonID05,NULL,NULL
mov hButton05,eax
invoke GetStockObject,DEFAULT_GUI_FONT
mov @DEFAULT_GUI_FONT,eax
invoke SendMessage,hButton,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE ;设置按钮控件的字体
invoke SendMessage,hButton01,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton02,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton03,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton04,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke SendMessage,hButton05,WM_SETFONT,@DEFAULT_GUI_FONT,TRUE
invoke DeleteObject,@DEFAULT_GUI_FONT ;删除本程序新建的字体
.elseif uMsg == WM_DESTROY
invoke PostQuitMessage,NULL
.elseif uMsg == WM_COMMAND
mov eax,wParam
.if ax == ButtonID01
invoke SendMessage,hButton,WM_SETTEXT,0,addr szButtonTitle10 ;改变第1个按钮的文字
.elseif ax == ButtonID02
invoke SendMessage,hButton,WM_GETTEXT,256,addr @szBuffer ;得到第1个按钮的文字
invoke MessageBox,NULL,addr @szBuffer,addr szTitle,MB_OK
.elseif ax == ButtonID03
invoke EnableWindow,hButton,FALSE ;使第1个按钮按钮无效
.elseif ax == ButtonID04
invoke EnableWindow,hButton,TRUE ;使第1个按钮按钮有效
.elseif ax == ButtonID05
invoke IsWindowEnabled ,hButton ;IsWindowEnabled函数为判断窗口是否有效
.if eax != 0 ;返回值不为0,则有效,为0则无效
invoke MessageBox,NULL,addr szButtonTitle11,addr szTitle,MB_OK
.else
invoke MessageBox,NULL,addr szButtonTitle12,addr szTitle,MB_OK
.endif
.endif
.else
invoke DefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xor eax,eax
ret
WndProc endp
;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start