win32汇编简单实现窗口程序

.386
.model flat,stdcall
option casemap:none
;==========================
;include部分
include windows.inc
include gdi32.inc
include user32.inc
includelib user32.lib
includelib gdi32.lib
include kernel32.inc
includelib kernel32.lib
;===========================
;未初始化数据段
.data?
hInstance   dd  ?   ;主程序句柄
hManinHwnd  dd  ?   ;主窗口句柄
;常量段
.const
szContent   db  "this is first window",0
szWindowClass   db  "New_Window",0
szTitle db  "My_First_Window",0
szButton    db  "Button",0
szButtonTitle   db  "&Cancel",0
;已初始化的数据段
.data
szMsgText db    1024    dup(?)
szFrmatText db  "ebx:%0.8X",0
;代码段
.code
_WindowCallbackProc     proc       uses ebx edi esi hWnd,uMsg,wParam,lParam
        local   @stPs:PAINTSTRUCT
        local   @stRect:RECT
        local   @hDc:dword
        mov eax,uMsg
        .if eax ==  WM_PAINT
            invoke  BeginPaint,hWnd,addr @stPs
            mov @hDc,eax

            invoke  GetClientRect,hWnd,addr @stRect
            invoke  DrawText,@hDc,addr szContent,-1,\
            addr @stRect,\
            DT_SINGLELINE or DT_CENTER or DT_VCENTER
        
        .elseif eax == WM_CREATE
            invoke  CreateWindowEx,NULL,offset szButton,offset  szButtonTitle,WS_CHILD or WS_VISIBLE,\
            10,50,80,40,hWnd,1001,hInstance,NULL
            
        .elseif eax == WM_COMMAND
            mov ebx,wParam
            and ebx,0FFFFh
            .if ebx == 03E9h
                invoke  wsprintf,addr szMsgText,addr szFrmatText,ebx
                invoke MessageBox,NULL,addr szMsgText,NULL,NULL
            .endif
        .elseif eax ==  WM_CLOSE
            invoke  DestroyWindow,hManinHwnd
            invoke  PostQuitMessage,NULL
        .else
            invoke  DefWindowProc,hWnd,uMsg,wParam,lParam
            ret
        .endif
        xor eax,eax
        ret

_WindowCallbackProc     endp
;定义wMain函数
_wMain      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  _WindowCallbackProc
    mov     @stWndClass.hbrBackground,COLOR_WINDOW + 1
    mov     @stWndClass.lpszClassName,offset  szWindowClass
    invoke  RegisterClassEx,addr    @stWndClass
;********************************************************************
; 建立并显示窗口
;********************************************************************
        invoke  CreateWindowEx,WS_EX_CLIENTEDGE,offset szWindowClass,offset szTitle,\
            WS_OVERLAPPEDWINDOW,\
            100,100,600,400,\
            NULL,NULL,hInstance,NULL
        mov hManinHwnd,eax
        invoke  ShowWindow,hManinHwnd,SW_SHOWNORMAL
        invoke  UpdateWindow,hManinHwnd
;********************************************************************
; 消息循环
;********************************************************************
        .while  TRUE
            invoke  GetMessage,addr @stMsg,NULL,0,0
            .break  .if eax == 0
            invoke  TranslateMessage,addr @stMsg
            invoke  DispatchMessage,addr @stMsg
        .endw
        ret

_wMain      endp
start:
        call    _wMain
        invoke  ExitProcess,NULL
end start

猜你喜欢

转载自www.cnblogs.com/guolongzheng/p/11670866.html