我用masm32写的小程序

本文档展示了如何使用masm32编写一个简单的Windows应用程序,该程序包括按钮和滚动条交互,以及动态更新显示。程序设计包含窗口初始化、消息处理、定时器设置等功能,并使用GDI进行图形绘制。

       这是我用masm32写的小程序,本来是想作一个画摆线的,但课程时间紧,只好作罢。

 .386

.model flat,stdcalloption casemap:noneMyMain proto :DWORD,:DWORD,:DWORD,:DWORD include windows.incinclude user32.incincludelib user32.libinclude kernel32.incincludelib kernel32.libinclude gdi32.incincludelib gdi32.libinclude masm32.incincludelib masm32.lib .dataButtonClassName db "button",0EditClassName db "edit",0MyClassName db "MyClass",0ButtonText1 db "开始",0ButtonText2 db "停止",0ButtonText3 db "加快",0ButtonText4 db "减慢",0CursorFileName db "F://wDoc.cur",0MyMenuName db "FirstMenu",0MyAppName db "MyFirstWindow",0MaxMessage db "Sorry",0SorryMessage db "已经到达最大速度了!",0AboutText db "摆线半成品 by Honglin Yu masm32 08.4.4.",0About  db "关于",0SpeedShow1 db "现在的速度是",0SpeedShow2 db "像素每秒",0ScrollBarClassName db "scrollbar",0StartValue dword 1EndValue dword 250CurrentPos dword 5dwLine  dword 1dwPage  dword 10Step  dword 5Temp  dword 3 dup(' ')  db 0TempLength dword 0.data?hInstance HINSTANCE ?ButtonhWnd HWND  ?CommandLine LPSTR  ?edithwnd HWND  ?buffer  db  512 dup(?)X  dword  ?Xdowning dword  ?Y  dword  ?Ydowning dword  ?BeginSign dword  ?TimerEnable dword  ?Speed  dword  ?PenWidth dword  ?top  dword  ?topdowning dword  ?bottom  dword  ?bottomdowning dword  ?ClickX  dword  ?ClickY  dword  ?VectorX  dword  ?VectorY  dword  ?MouseX  dword  ?MouseY  dword  ?DownCircle dword  ?DownButton1 dword  ?DownButton2 dword  ?DownButton3 dword  ?DownButton4 dword  ?Button1Left dword  ?Button1Right dword  ?Button1top dword  ?Button1bottom dword  ?Button2Left dword  ?Button2Right dword  ?Button2top dword  ?Button2bottom dword  ?Button3Left dword  ?Button3Right dword  ?Button3op dword  ?Button3bottom dword  ?Button4Left dword  ?Button4Right dword  ?Button4top dword  ?Button4bottom dword  ?LY  dword  ?hwndScrollBar dword  ? .constButtonID1 equ 1ButtonID2 equ 2ButtonID3 equ 3ButtonID4 equ 4 TimerID  equ 1000 IDM_ABOUT equ 11 IDM_EXIT equ 14 ID_SCROLLBAR       equ 21 .code start: invoke GetModuleHandle,NULL mov hInstance,eax invoke GetCommandLine mov CommandLine,eax invoke MyMain,hInstance,NULL,CommandLine,SW_SHOWDEFAULT invoke ExitProcess,eax  MyMain proc hInst:HINSTANCE,hp: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 MyProc  mov wc.cbClsExtra,NULL  mov wc.cbWndExtra,NULL  push hInst  pop wc.hInstance  invoke LoadIcon,NULL,IDI_APPLICATION  mov wc.hIcon,eax  mov wc.hIconSm,eax  invoke LoadCursorFromFile,offset CursorFileName  ;invoke LoadCursor,NULL,IDC_HAND  mov wc.hCursor,eax  mov wc.hbrBackground,COLOR_WINDOW+1  mov wc.lpszMenuName,OFFSET MyMenuName  mov wc.lpszClassName,OFFSET MyClassName  invoke RegisterClassEx,addr wc      invoke CreateWindowEx,NULL,ADDR MyClassName,ADDR MyAppName,/            WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,/            CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,/            hInst,NULL   mov hWnd,eax  invoke ShowWindow,hWnd,SW_SHOWNORMAL  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  MyMain endp  MyProc proc hWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM      LOCAL hdc:HDC     LOCAL ps:PAINTSTRUCT     LOCAL rect:RECT    LOCAL hpen:HPEN    .IF uMsg==WM_CREATE           ;Variable Initial      mov top,200      mov bottom,300      push top      pop X      push bottom      pop Y      mov Speed,100      mov BeginSign,1      mov PenWidth,5      mov top,200      mov bottom,300      mov DownCircle,FALSE      invoke GetClientRect,hWnd,ADDR rect      mov eax,rect.right      mov ebx,9      xor edx,edx      div bx      mov LY,eax      mov Button1Left,eax      add eax,LY      mov Button1Right,eax      add eax,LY      mov Button2Left,eax      add eax,LY      mov Button2Right,eax      add eax,LY      mov Button3Left,eax      add eax,LY      mov Button3Right,eax      add eax,LY      mov Button4Left,eax      add eax,LY      mov Button4Right,eax                  mov DownButton1,FALSE      mov DownButton2,FALSE      mov DownButton3,FALSE      mov DownButton4,FALSE     ;invoke CreateWindowEx,WS_EX_CLIENTEDGE, ADDR BottonClassName,NULL,/          ;               WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT or/          ;               ES_AUTOHSCROLL,/          ;               100,100,200,200,hWnd,8,hInstance,NULL         invoke CreateWindowEx,NULL, ADDR ButtonClassName,ADDR ButtonText1,/                         WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,/                         Button1Left,70,LY,25,hWnd,ButtonID1,hInstance,NULL         invoke CreateWindowEx,NULL, ADDR ButtonClassName,ADDR ButtonText2,/                         WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,/                         Button2Left,70,LY,25,hWnd,ButtonID2,hInstance,NULL         invoke CreateWindowEx,NULL, ADDR ButtonClassName,ADDR ButtonText3,/                         WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,/                         Button3Left,70,LY,25,hWnd,ButtonID3,hInstance,NULL                 invoke CreateWindowEx,NULL, ADDR ButtonClassName,ADDR ButtonText4,/                         WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,/                         Button4Left,70,LY,25,hWnd,ButtonID4,hInstance,NULL         invoke SetFocus,eax                invoke CreateWindowEx,WS_EX_CLIENTEDGE, ADDR ScrollBarClassName,addr MyAppName,/          WS_VISIBLE  or WS_CHILD or WS_EX_WINDOWEDGE or  SBS_HORZ  ,/          100,40,200,25,hWnd,ID_SCROLLBAR,hInstance,NULL         mov hwndScrollBar,eax   invoke SetScrollRange,hwndScrollBar,SB_CTL,StartValue,EndValue,TRUE                           ;Timer Set      invoke SetTimer,hWnd,TimerID,Speed,NULL          .ELSEIF uMsg==WM_DESTROY         invoke KillTimer,hWnd,TimerID        invoke PostQuitMessage,NULL           .ELSEIF uMsg==WM_PAINT         invoke BeginPaint,hWnd, ADDR ps         mov    hdc,eax                         mov eax,DownCircle        .if eax==TRUE         invoke Rectangle,hdc,X,bottom,Y,top        .endif         invoke GetClientRect,hWnd, ADDR rect        xor eax,eax mov eax,241 shl eax,8 add eax,150 shl eax,8 add eax,241        invoke CreatePen,PS_SOLID,PenWidth,eax       mov hpen, eax       invoke SelectObject,hdc,hpen        invoke Ellipse,hdc,X,bottom,Y,top        invoke DeleteObject,hpen                        mov eax,Step        mov ebx,1000        mul ebx              mov ebx,Speed          xor edx,edx        div bx               invoke dwtoa,eax,ADDR Temp invoke lstrlen,ADDR Temp  mov TempLength,eax  invoke lstrlen,ADDR SpeedShow1 mov ebx,10 mul bx             invoke TextOut,hdc,eax,0,ADDR Temp,TempLength        invoke lstrlen,ADDR SpeedShow1        invoke TextOut,hdc,0,0,ADDR SpeedShow1,eax        invoke lstrlen,ADDR SpeedShow2        invoke TextOut,hdc,150,0,ADDR SpeedShow2,eax                ;invoke DrawText, hdc,ADDR MyAppName,-1, ADDR rect, /          ;       DT_SINGLELINE or DT_CENTER or DT_VCENTER         invoke EndPaint,hWnd, ADDR ps                  invoke SetScrollPos,hwndScrollBar,SB_CTL,CurrentPos,TRUE                            .ELSEIF uMsg==WM_COMMAND     mov eax,wParam     .IF ax==IDM_ABOUT      invoke MessageBox,hWnd,addr AboutText,addr About,MB_OK     .ENDIF     .IF ax==IDM_EXIT             invoke KillTimer,hWnd,TimerID         invoke PostQuitMessage,NULL            .ENDIF     .IF ax==ButtonID1              mov BeginSign,0           .ENDIF     .IF ax==ButtonID2       mov BeginSign,1     .ENDIF     .IF ax==ButtonID3      mov eax,Speed      .if eax>20       sub Speed,20       invoke KillTimer,hWnd,TimerID       invoke SetTimer,hWnd,TimerID,Speed,NULL       mov rect.left, 0       mov rect.top, 0       mov rect.right,200       mov rect.bottom,200       invoke InvalidateRect,hWnd,ADDR rect,TRUE      .else       invoke MessageBox,hWnd,ADDR SorryMessage,ADDR MaxMessage,MB_OK      .endif     .ENDIF     .IF ax==ButtonID4       add Speed,20       invoke KillTimer,hWnd,TimerID       invoke SetTimer,hWnd,TimerID,Speed,NULL       mov rect.left,0       mov rect.top,0       mov rect.right,200       mov rect.bottom,200       invoke InvalidateRect,hWnd,ADDR rect,TRUE     .ENDIF         .ELSEIF uMsg==WM_TIMER     mov  eax,BeginSign     .if(!eax)       push X      push Y            mov eax,X      sub eax,PenWidth      mov X,eax     push X      pop rect.left            mov eax,top      sub eax,PenWidth      push eax      pop rect.top            mov eax,Y      add eax,PenWidth      push eax      pop rect.right            mov eax,bottom      add eax,PenWidth      push eax      pop rect.bottom             pop Y      pop X            mov eax,Step      add X,eax      add Y,eax               invoke InvalidateRect,hWnd,ADDR rect,TRUE                       invoke GetClientRect,hWnd,ADDR rect      push rect.right      pop eax      .if eax<Y            mov X,100        mov Y,200       .endif                      .endif    .ELSEIF uMsg==WM_MOUSEMOVE       mov eax,lParam      shr eax,16      and eax,0FFFFH      mov MouseY,eax      mov eax,lParam      and eax,0FFFFH      mov MouseX,eax      mov eax,DownCircle      .if eax==TRUE            mov eax,MouseX        sub eax,ClickX        mov VectorX,eax        mov eax,Xdowning        add eax,VectorX        mov X,eax        mov eax,Ydowning        add eax,VectorX        mov Y,eax                mov eax,MouseY        sub eax,ClickY        mov VectorY,eax        mov eax,topdowning        add eax,VectorY        mov top,eax        mov eax,bottomdowning        add eax,VectorY        mov bottom,eax       invoke GetClientRect,hWnd,ADDR rect       invoke InvalidateRect,hWnd,ADDR rect,TRUE         .endif    .ELSEIF uMsg==WM_LBUTTONDOWN        mov eax,lParam      shr eax,16      and eax,0FFFFH      mov ClickY,eax      mov eax,lParam      and eax,0FFFFH      mov ClickX,eax                  sub eax,X      .if eax<100       mov eax,ClickY       sub eax,top       .if eax<100        mov BeginSign,1        push X    pop Xdowning    push top    pop topdowning    push Y        pop Ydowning        push bottom        pop bottomdowning        mov DownCircle,TRUE                .endif      .endif          .ELSEIF uMsg==WM_LBUTTONUP      mov DownCircle,FALSE      invoke GetClientRect,hWnd,ADDR rect      invoke InvalidateRect,hWnd,ADDR rect,TRUE    .ELSEIF uMsg==WM_HSCROLL      mov eax,hwndScrollBar       .if lParam==eax         mov eax,wParam         .if ax==SB_LINERIGHT            mov eax,dwLine            add CurrentPos,eax         .elseif ax==SB_LINELEFT            mov eax,dwLine            sub CurrentPos,eax            push CurrentPos            pop Step         .elseif ax==SB_PAGELEFT            mov eax,dwPage            sub CurrentPos,eax            push CurrentPos            pop Step            .if SIGN?               push StartValue               pop CurrentPos            push CurrentPos            pop Step            .endif         .elseif ax==SB_PAGERIGHT            mov eax,dwPage            add CurrentPos,eax            push CurrentPos            pop Step         .elseif ax==SB_THUMBTRACK            mov eax,wParam            shr eax,16            movzx eax,ax            mov CurrentPos, eax            push CurrentPos            pop Step         .endif          mov eax,CurrentPos         .if eax>EndValue            push EndValue            pop CurrentPos         .endif         .if eax<StartValue            push StartValue            pop CurrentPos         .endif              .endif      mov rect.left,80      mov rect.top,10      mov rect.right,300      mov rect.bottom,70          invoke InvalidateRect,hWnd,ADDR rect,TRUE              .ELSEIF          invoke DefWindowProc,hWnd,uMsg,wParam,lParam         ret     .ENDIF     xor   eax, eax     ret   ret  MyProc endp end start          

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值