;################################################
;              -- HelloWorld.asm --
;     Jay Sullivan             Public Domain
;################################################

;================================================
;              Meta-Directives
;================================================

.386
.MODEL FLAT,STDCALL
OPTION CASEMAP:NONE
             
;================================================
;              Header
;================================================

    ;-------------------------------------------------
    ;              Includes
    ;-------------------------------------------------
    
    include \masm32\include\windows.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
        
    ;-------------------------------------------------
    ;              Linked Libraries
    ;-------------------------------------------------
    
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
            
    ;-------------------------------------------------
    ;              Prototypes
    ;-------------------------------------------------
    
    WinMain PROTO
    WndProc PROTO hWnd:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
        
;================================================
;            .DATA/.CONST/.DATA?
;================================================

.DATA

    MainWindowClass WNDCLASSEX < \
    	Sizeof WNDCLASSEX, 0, Offset WndProc, 0, 0,0,0,0, \
    	COLOR_HIGHLIGHT+1, 0, Offset MainWindowClassName, 0>

.CONST

    MainWindowClassName db "BlankWindowClass",0
    ButtonClassName     db "BUTTON",0
    MainWindowTitle     db "Blank Window",0
    ClickMeButtonText   db "Click me",0
    PopupMessage        db "Hello, world!",0
    PopupTitle          db "Hello, world!",0

.DATA?

    MainWindowHandle    HWND ?
    ClickMeButtonHandle HWND ?
    MessageStruct       MSG  <?>
    
;================================================
;            .CODE
;================================================

.CODE

start:
    invoke GetModuleHandle, 0
    mov MainWindowClass.hInstance, eax
    invoke WinMain
    invoke ExitProcess,eax

;================================================
;            WinMain
;================================================

WinMain PROC

    ;------------------------------------------------
    ;    Register Window
    ;------------------------------------------------

        ;------------------------------------------------
        ;    Load Icon for Main Window
        ;------------------------------------------------
            
            invoke LoadIcon, 0, IDI_APPLICATION
            mov MainWindowClass.hIcon,eax
            
        ;------------------------------------------------
        ;    Load Mouse Cursor for Main Window
        ;------------------------------------------------
            
            invoke LoadCursor, 0, IDC_ARROW
            mov MainWindowClass.hCursor,eax
            
        ;------------------------------------------------
        ;    Register the Main Window's Class.
        ;------------------------------------------------
            
            invoke RegisterClassEx, Offset MainWindowClass
            
    ;------------------------------------------------
    ;    Initialize Window
    ;------------------------------------------------
    
        ;------------------------------------------------
        ;    Create Main Window 
        ;------------------------------------------------

            invoke CreateWindowEx, WS_EX_CLIENTEDGE,\
                Offset MainWindowClassName,\
                Offset MainWindowTitle,\
                WS_OVERLAPPEDWINDOW,\
                200,200,300,300,\
                0,0,\
                MainWindowClass.hInstance,\
                0
            mov MainWindowHandle,eax

        ;------------------------------------------------
        ;    Add a button that says "Click me"
        ;------------------------------------------------
            
            invoke CreateWindowEx, WS_EX_STATICEDGE, Offset ButtonClassName, Offset ClickMeButtonText, \
                    WS_VISIBLE or WS_CHILD or WS_BORDER or ES_LEFT, 10, 10, 75, 35, MainWindowHandle, 0, \
                    MainWindowClass.hInstance, 0
            mov ClickMeButtonHandle,eax
            
        ;-----------------------------
        ;    Show Window
        ;------------------------------
            
            invoke ShowWindow, MainWindowHandle, SW_SHOWNORMAL
            invoke UpdateWindow, MainWindowHandle
            
        ;-----------------------------
        ;    Begin Message Loop
        ;-----------------------------
            
            jmp GetMsg
            
            MessageLoop:
              invoke TranslateMessage, Offset MessageStruct
              invoke DispatchMessage, Offset MessageStruct
            GetMsg:
              invoke GetMessage, Offset MessageStruct, 0, 0, 0
              test eax, eax
              jnz MessageLoop
            
    mov eax, MessageStruct.wParam
    ret
WinMain ENDP

;================================================
;            WinMain
;================================================

WndProc PROC hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

    ;-----------------------------
     .if uMsg==WM_DESTROY
    ;-----------------------------
        
        invoke PostQuitMessage, 0

    ;-----------------------------
     .elseif uMsg==WM_COMMAND
    ;-----------------------------
        
        mov eax,wParam
        and eax, 0FFFFFFFFh
           
        ;-----------------------------
         .if eax == BN_CLICKED
        ;-----------------------------
        
            mov eax, lParam

            ;-----------------------------
            .if eax==ClickMeButtonHandle
            ;-----------------------------

            invoke MessageBox, NULL, Offset PopupMessage, Offset PopupTitle, MB_OK        

            ;-----------------------------            
            .endif
            ;-----------------------------

        ;-----------------------------
         .else
        ;-----------------------------

            invoke DestroyWindow, hWnd

        ;-----------------------------
         .endif
        ;-----------------------------

    ;-----------------------------
     .else
    ;-----------------------------
        
        invoke DefWindowProc, hWnd, uMsg, wParam, lParam
        ret
        
    ;-----------------------------
     .endif
    ;-----------------------------
    
    xor eax,eax
    ret

WndProc ENDP


end start