
; This is a Win32 demo which uses a modal dialog to allow data entry to a program
; Ron Thomas 30/8/99 

.386
.model flat, stdcall

include windows.inc
include user32.inc
include kernel32.inc
include gdi32.inc
include \masm32\include\masm32.inc

includelib user32.lib
includelib kernel32.lib
includelib gdi32.lib
includelib masm32.lib

WinMain	  PROTO :DWORD, :DWORD, :DWORD, :SDWORD

lstrlenA	PROTO	WINAPI	:LPCSTR		; Not included in latest l2inc output
lstrlen		textequ	<lstrlenA>		; but lib can't be missing as function works 

.data
ClassName db "SimpleWinClass",0
AppName   db "Data Menu",0
MenuName  db "DataMenu",0
IntDlgName   db "IntDlg",0		; Name of Dialog Integer entry box in the resource file 
TextDlgName  db "TextDlg",0		; Name of Dialog Text entry box "	"	"
AboutDlgName db "About",0		; Name of About dialog box	"	"	"
screentext   db "A line of text.", 65 DUP(?),0	; 80 byte buffer
	
.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hPen	dd	?
iupperx dd	?
iuppery dd	?
ilowerx dd	?
ilowery dd	?

.const

ID_PICK_SHAPE equ 40001
ID_PICK_EXIT  equ 40002
ID_HELP_ABOUT equ 40003

ID_OK	equ 1
ID_CANCEL equ 2
   
IDC_EDIT   equ 3000
IDC_BUTTON equ 3001
IDC_EXIT   equ 3002
IDC_UPPERX equ 1000
IDC_UPPERY equ 1001
IDC_LOWERX equ 1002
IDC_LOWERY equ 1003	
IDC_TEXT   equ 1004

ID_DATA_TEXT  equ 40001
ID_DATA_INT   equ 40002
ID_HELP_ABOUT equ 40003
ID_HELP_EXIT  equ 40004

;Macros
;------
RGB	MACRO   red, green, blue	;; Get composite number from red green and blue bytes 

	mov	al,blue			;; ,,,blue	
	shl	eax,8			;; ,,blue,

	add	al,green		;; ,,blue,green
	shl	eax,8			;; ,blue,green,
	add	al,red			;; ,blue,green,red
	and	eax,0FFFFFFh		;; Mask out top byte to complete COLORREF dword 

	ENDM

LOWORD	MACRO 	bigword	;; Retrieves the low word from double word argument

	mov	eax,bigword
	and	eax,0FFFFh	;; Set to low word 
	ENDM
;---------------------------------------------------------------------------
.code
start:
	invoke GetModuleHandle, NULL
	mov    hInstance,eax
	invoke GetCommandLine
        invoke WinMain, hInstance,NULL,CommandLine, SW_SHOWDEFAULT
	invoke ExitProcess,eax
WinMain proc hInst:HINSTANCE,hPrevInst:HINSTANCE,CmdLine:LPSTR,CmdShow:SDWORD
	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 WndProc
	mov   wc.cbClsExtra,NULL
	mov   wc.cbWndExtra,NULL
        push  hInstance
        pop   wc.hInstance
        mov   wc.hbrBackground,COLOR_WINDOW+1
        mov   wc.lpszMenuName,OFFSET MenuName
	mov   wc.lpszClassName,OFFSET ClassName
        invoke LoadIcon,NULL,IDI_APPLICATION
	mov   wc.hIcon,eax
        mov   wc.hIconSm,0
        invoke LoadCursor,NULL,IDC_ARROW
	mov   wc.hCursor,eax
        invoke RegisterClassEx, addr wc
        INVOKE CreateWindowEx,WS_EX_CLIENTEDGE,ADDR ClassName,ADDR AppName,\
           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
WinMain endp


TextDlgProc PROC hDlg:HWND,iMsg:DWORD,wParam:WPARAM, lParam:LPARAM

        mov  eax,iMsg
        .IF     eax==WM_INITDIALOG
 
        .ELSEIF eax==WM_COMMAND

		LOWORD wParam

                .IF eax==ID_OK

                        invoke GetDlgItemText, hDlg,IDC_TEXT, ADDR screentext, 80
			invoke EndDialog, hDlg,TRUE
		.ELSEIF eax==IDCANCEL

			invoke EndDialog, hDlg,FALSE

                .ENDIF
        .ELSE
                mov eax,FALSE
                ret
        .ENDIF

        mov  eax,TRUE
        ret
TextDlgProc endp

IntDlgProc PROC hDlg:HWND,iMsg:DWORD,wParam:WPARAM, lParam:LPARAM

        mov  eax,iMsg
        .IF     eax==WM_INITDIALOG
 
        .ELSEIF eax==WM_COMMAND

	    LOWORD wParam

            .IF eax==ID_OK

		invoke  GetDlgItemInt, hDlg,IDC_UPPERX,NULL,0
                mov  iupperx,eax
		invoke  GetDlgItemInt, hDlg,IDC_UPPERX,NULL,0
                mov  iuppery,eax
		invoke  GetDlgItemInt, hDlg,IDC_LOWERX,NULL,0
                mov  ilowerx,eax
		invoke  GetDlgItemInt, hDlg,IDC_LOWERY,NULL,0
                mov  ilowery,eax
		invoke  EndDialog,hDlg,TRUE

	    .ELSEIF eax==IDCANCEL

		invoke  EndDialog,hDlg,FALSE
            .ENDIF

        .ELSE
                mov eax,FALSE
                ret
        .ENDIF

        mov  eax,TRUE
        ret
IntDlgProc endp

AboutDlg PROC hDlg:HWND, iMsg:DWORD, wParam:WPARAM, lParam:LPARAM

        mov  eax,iMsg

        .IF eax==WM_INITDIALOG

	  mov eax,TRUE
	  ret

        .ELSEIF eax==WM_COMMAND

            LOWORD  wParam

            .IF eax==ID_OK
                invoke EndDialog, hDlg,0	 
		mov eax,TRUE
		ret
	    .ENDIF
	    
        .ENDIF

        mov  eax,FALSE
        ret

AboutDlg endp

WndProc proc hWnd:HWND, uMsg:UINT, wParam:WPARAM, lParam:LPARAM

LOCAL ps:PAINTSTRUCT, hdc:HDC, hMenu:HMENU

	mov   eax,uMsg
	
        .IF eax==WM_COMMAND

	    LOWORD wParam
	    .IF     eax==ID_DATA_TEXT	; Display the Text dialog box

	        invoke DialogBoxParam,hInstance, ADDR TextDlgName,hWnd,ADDR TextDlgProc,NULL 	
	        invoke InvalidateRect, hWnd,NULL,TRUE
		invoke UpdateWindow, hWnd

	    .ELSEIF eax==ID_DATA_INT	; Display the Integer dialog box

		invoke DialogBoxParam,hInstance, ADDR IntDlgName,hWnd,ADDR IntDlgProc,NULL 	
		invoke InvalidateRect, hWnd,NULL,TRUE
		invoke UpdateWindow, hWnd
           
            .ELSEIF eax==ID_HELP_ABOUT
                invoke DialogBoxParam,hInstance, ADDR AboutDlgName,hWnd,ADDR  AboutDlg,NULL

 	    .ELSEIF eax==ID_HELP_EXIT
	        invoke SendMessage,hWnd,WM_CLOSE,0,0
            .ENDIF
	
	.ELSEIF eax==WM_PAINT

		invoke BeginPaint,hWnd,ADDR ps
		mov    hdc,eax	

		RGB    0,0,255			; Create blue pen 	
		invoke CreatePen,PS_SOLID,8,eax ; Solid and 8 pixels wide
		mov    hPen,eax
		invoke SelectObject,hdc,hPen

;	Draw the defined rectangle
;	--------------------------

		invoke Rectangle,hdc,iupperx,iuppery,ilowerx,ilowery

		invoke lstrlen, ADDR screentext			; Get string length in eax
		invoke TextOut,hdc,100,100,ADDR screentext,eax	; Print string within rectangle
		
		invoke  ValidateRect,hWnd,NULL
		invoke  EndPaint,hWnd,ADDR ps
			  	
	.ELSEIF eax==WM_DESTROY

		invoke DeleteObject,hPen
		invoke PostQuitMessage,NULL
        .ELSE
                invoke DefWindowProc,hWnd,uMsg,wParam,lParam
                ret
	.ENDIF

        xor    eax,eax
	ret
WndProc endp

        end start
