format PE GUI 4.0
entry start ; you forget to set the entry point for the code

include '%include%\win32a.inc'		; main window equates
include '%include%\macro\if.inc'       ; for HLL loops

;constants go here
ButtonID	=10
EditID		=11
IDM_MAIN	=100
IDM_HELLO	=200
IDM_CLEAR	=210
IDM_GETTEXT	=220
IDM_EXIT	=230

;---------------------------------------------------------------
section '.data' data readable writeable

	ClassName	    db 'SimpleWinClass',0
	AppName 	    db 'OurFirstWindow',0
	MenuName	    db 'FirstMenu',0
	ButtonClassName     db 'button',0
	ButtonText	    db 'My Fist Button',0
	EditClassName	    db 'edit',0
	TestString	    db 'Wow! I''m in an edit box now',0

;---------------------------------------------------------------
section '.udata' readable writeable
	hInstance	dd ?
	CommandLine	dd ?
	hwndButton	dd ?
	hwndEdit	dd ?
	buffer		rb 512

;---------------------------------------------------------------
section '.text' code readable executable

start:
	invoke	GetModuleHandle, NULL
	mov	ptr hInstance, eax
	invoke	GetCommandLine
	mov	ptr CommandLine, eax
	stdcall WinMain, ptr hInstance, NULL, ptr CommandLine, SW_SHOWDEFAULT
	invoke	ExitProcess, eax

proc WinMain, hInst, hPrevInst, CmdLine, CmdShow
     wc      WNDCLASSEX
     msg     MSG
     hwnd    dd ?

     enter

     mov	ptr wc.cbSize, sizeof.WNDCLASSEX
     mov	ptr wc.style, CS_HREDRAW or CS_VREDRAW
     mov	ptr wc.lpfnWndProc, WndProc
     mov	ptr wc.cbClsExtra,NULL
     mov	ptr wc.cbWndExtra,NULL
     push	ptr hInst
     pop	ptr wc.hInstance
     mov	ptr wc.hbrBackground,COLOR_BTNFACE+1
     mov	ptr wc.lpszMenuName, NULL
     mov	ptr wc.lpszClassName, ClassName

     invoke	LoadIcon,NULL,IDI_APPLICATION
     mov	ptr wc.hIcon,eax
     mov	ptr wc.hIconSm,eax

     invoke	LoadCursor,NULL,IDC_ARROW
     mov	ptr wc.hCursor,eax

     lea	eax,  ptr wc
     invoke	RegisterClassEx, eax

     invoke	LoadMenu,ptr hInstance, IDM_MAIN

     ; I've added eax to the hmenu parameter
     ; which is the handle to the menu you
     ; just loaded

     invoke	CreateWindowEx,WS_EX_CLIENTEDGE,ClassName, \
			AppName, WS_OVERLAPPEDWINDOW,\
			CW_USEDEFAULT, CW_USEDEFAULT,\
			300,200,NULL,eax, ptr hInst,NULL
     mov	ptr hwnd, eax

     invoke	ShowWindow, ptr hwnd, SW_SHOWNORMAL
     invoke	UpdateWindow, ptr hwnd

     push	ebx	;you need to preserve ebx
     lea	ebx, ptr msg

       @@:
	invoke	GetMessage, ebx,NULL,0,0
	or	eax,eax
	jz	@f
	invoke	TranslateMessage, ebx
	invoke	DispatchMessage, ebx
	jmp	@b
      @@:
	pop	ebx
	mov	eax,ptr msg.wParam
	return

proc WndProc, hWnd, uMsg, wParam, IParam
	enter

	.if ptr uMsg,e,WM_DESTROY
		invoke	PostQuitMessage, NULL
	.elseif ptr uMsg,e,WM_CREATE
		invoke CreateWindowEx,WS_EX_CLIENTEDGE, EditClassName,NULL,\
			WS_CHILD or WS_VISIBLE or WS_BORDER or ES_LEFT or\
			ES_AUTOHSCROLL,\
			50,35,200,25,ptr hWnd,8,ptr hInstance,NULL
		mov	ptr hwndEdit,eax
		invoke	SetFocus, ptr hwndEdit
		invoke	CreateWindowEx,NULL, ButtonClassName,ButtonText,\
			WS_CHILD or WS_VISIBLE or BS_DEFPUSHBUTTON,\
			75,70,140,25,ptr hWnd,ButtonID,ptr hInstance,NULL
		mov	ptr hwndButton,eax
	.elseif ptr uMsg,e,WM_COMMAND
		mov	eax, ptr wParam
		.if ptr IParam,e,0
			.if ax,e,IDM_HELLO
				invoke	SetWindowText, ptr hwndEdit, TestString
			.elseif ax,e,IDM_CLEAR
				invoke	SetWindowText, ptr hwndEdit, NULL
			.elseif ax,e,IDM_GETTEXT
				invoke	GetWindowText, ptr hwndEdit, buffer, 512
				invoke	MessageBox, NULL, buffer, AppName, MB_OK
			.else
				invoke	DestroyWindow, ptr hWnd
			.endif
		.else
			.if	ax,e,ButtonID
				shr	eax,16
				.if	ax,e,BN_CLICKED
					invoke SendMessage, ptr hWnd, WM_COMMAND, IDM_GETTEXT,0
				.endif
			.endif
		.endif
	;.endif << typo
	.else
		invoke	DefWindowProc, ptr hWnd, ptr uMsg, ptr wParam, ptr IParam
		return
	.endif
	xor	eax, eax
	return

;---------------------------------------------------------------
section '.idata' import data readable writeable
; import all the API functions that we used

library kernel, 'KERNEL32.DLL', user, 'USER32.DLL'

import	kernel,\
	GetModuleHandle,'GetModuleHandleA',\
	GetCommandLine, 'GetCommandLineA',\
	ExitProcess, 'ExitProcess'

import	user,\
	RegisterClassEx, 'RegisterClassExA',\
	CreateWindowEx, 'CreateWindowExA',\
	DefWindowProc, 'DefWindowProcA',\
	ShowWindow, 'ShowWindow',\
	GetMessage, 'GetMessageA',\
	TranslateMessage, 'TranslateMessage',\
	DispatchMessage, 'DispatchMessageA',\
	LoadCursor, 'LoadCursorA',\
	UpdateWindow,'UpdateWindow',\
	LoadIcon, 'LoadIconA',\
	PostQuitMessage,'PostQuitMessage',\
	SendMessage,'SendMessageA',\
	DestroyWindow,'DestroyWindow',\
	MessageBox,'MessageBoxA',\
	LoadMenu,'LoadMenuA',\
	SetFocus,'SetFocus',\
	GetWindowText,'GetWindowTextA',\
	SetWindowText,'SetWindowTextA'

;-----------------------------------------------------
section '.rsrc' resource data readable

  directory RT_MENU, menus

  resource menus,\
  IDM_MAIN,LANG_ENGLISH+SUBLANG_DEFAULT,main_menu

menu main_menu
     menuitem '&PopUp',0,MFR_POPUP + MFR_END
	  menuitem '&Say Hello',IDM_HELLO,0
	  menuitem '&Clear',IDM_CLEAR,0
	  menuitem '&Get Text',IDM_GETTEXT,0
;=====================================================