WIN32汇编为按钮添加处理过程

     由于笔者也是初学者,学到哪,也就写到哪,望各位大神见谅哈,本博文呢,主要是在主窗体中添加了一个button,但是该Button是动态添加的,也就是说使用了

CreateWindowEx API函数,同时为该按钮的点击事件添加处理过程:

    先说下原理,其实呢,很简单,就是先检查uMsg是否是WM_COMMAND,然后呢,就看看,触发事件的控件的ID是否使我们要处理的控件的ID,就这两样,但是呢由于我们使用

的是CreateWindowEx API,动态创建的按钮,其ID我们并不清楚,但是一点,就是CreateWindowEx中的HMENU可以作为控件的ID传递进来,而后我们根据该ID,我们进行处理!

.386
.Model Flat,stdcall
option  casemap:none
;头文件包含
include windows.inc
include kernel32.inc
includelib kernel32.lib
include user32.inc
includelib user32.lib
include gdi32.inc
includelib gdi32.lib
;数据段定义
.data
hInstance dd ?
hWinMain  dd ?
hButton   dd ?
;常量定义
.const
szClassName db "MyClass",0
szCaption   db "My FirstWindow",0
szText      db "这是我的第二个程序",0
szButton    db "Button",0;
szButtonText db "ButtonText",0
szBuffer    db "点击了按钮",0

;代码段定义
.code
;窗口的消息处理函数
WinProc proc uses ebx edi esi,hWnd,uMsg,wParam,lParam
;局部变量的定义
local @stPs:PAINTSTRUCT
local @stRect:RECT;
local @hDc;
mov eax,uMsg;

.if    eax == WM_PAINT
	 invoke BeginPaint,hWnd,addr @stPs;
	 mov @hDc,eax;
	 invoke GetClientRect,hWnd,addr @stRect;
	 invoke DrawText,@hDc,addr szText,-1,addr @stRect,\
	 DT_SINGLELINE or DT_CENTER or DT_VCENTER;
	 invoke EndPaint,hWnd,addr @stPs;
.elseif    eax == WM_CLOSE
	 invoke DestroyWindow,hWinMain;
	 invoke PostQuitMessage,NULL;
.elseif eax==WM_CREATE
     invoke CreateWindowEx,NULL,\
	                        offset szButton,offset szButtonText,WS_CHILD or WS_VISIBLE,10,10,65,22,hWnd,1001,hInstance,NULL;
	   mov hButton,eax
.elseif eax==WM_COMMAND
     mov eax,wParam
	  .if ax==1001
	invoke MessageBox,NULL,addr szBuffer,NULL,NULL;
	 .endif
.else
    invoke DefWindowProc,hWnd,uMsg,wParam,lParam;
	 ret;该ret非常重要,如果没有ret,就相当于多数消息没有正确的返回值,则,窗口不能正常工作
.endif
    xor eax,eax
	 ret
WinProc endp;
;主函数
_WinMain proc 
     local @stWndClass:WNDCLASSEX;
	  local @stMsg:MSG;
	  invoke GetModuleHandle,NULL;
	  mov hInstance,eax
	  invoke RtlZeroMemory,addr @stWndClass,sizeof @stWndClass;
;注册窗口类
     invoke LoadCursor,0,IDC_ARROW;
	  ;指定鼠标指针
	  mov   @stWndClass.hCursor,eax;
	  push  hInstance 
	  pop   @stWndClass.hInstance;
	  mov   @stWndClass.cbSize,sizeof WNDCLASSEX;
	  mov   @stWndClass.style,CS_HREDRAW or CS_VREDRAW;
	  mov   @stWndClass.lpfnWndProc,offset WinProc;
	  mov   @stWndClass.hbrBackground,COLOR_WINDOW+1;
	  mov   @stWndClass.lpszClassName,offset szClassName;
	  invoke RegisterClassEx,addr @stWndClass;
;建立便显示窗口
     invoke CreateWindowEx,WS_EX_CLIENTEDGE,\
	  offset szClassName,offset szCaption,\
	  WS_OVERLAPPED,100,100,600,400,\
	  NULL,NULL,hInstance,NULL;
	  mov hWinMain,eax;
	  invoke ShowWindow,hWinMain,SW_SHOWNORMAL;
	  invoke UpdateWindow,hWinMain;
;消息循环
.while TRUE
     invoke GetMessage,addr @stMsg,NULL,0,0;
	   .break  .if eax==0		
	  invoke TranslateMessage,addr @stMsg;
	  invoke DispatchMessage,addr @stMsg;
.endw;
ret
_WinMain endp;
start: 
   call _WinMain;
	invoke ExitProcess,0;
	.while TRUE
	.endw
end start
嗯,就是这样了,不过我在学习过程中遇到了一点小小的问题,就是.if的嵌套使用,

在.elseif eax==WM_COMMAND

      mov eax,wParam;

     .if ax,1001;过程中遇到了问题,就是不能正确的编译成功,如果要嵌套使用if语句,要注意还要有.if的闭合语句.endif

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

世纪殇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值