
; This is a Win32 demo which uses a modal dialog to allow data entry to a program
; using radio buttons. 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

.data
ClassName db "SimpleWinClass",0
AppName   db "Radio Buttons",0
MenuName  db "RadioButton",0
DlgName   db "Buttons",0		; Name of Buttons dialog box in the resource file 
AboutDlgName  db "About",0		; Name of About dialog box	"	"	"

.data?
hInstance HINSTANCE ?
CommandLine LPSTR ?
hPen	dd	?
shape	dd	?

.const

ID_PICK_SHAPE equ 40001
ID_PICK_EXIT  equ 40002
ID_HELP_ABOUT equ 40003

ID_OK	equ 4
   
IDC_EDIT   equ 3000
IDC_BUTTON equ 3001
IDC_EXIT   equ 3002
IDC_RADIO1 equ 1001
IDC_RADIO2 equ 1002
IDC_RADIO3 equ 1003
IDC_RADIO4 equ 1004

;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
ShapeDlg 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 EndDialog,hDlg,0
		.ELSEIF eax==IDC_RADIO1
			mov shape,1			; Shape 1 identified
			invoke CheckRadioButton,hDlg,IDC_RADIO1,IDC_RADIO4,wParam
		.ELSEIF eax==IDC_RADIO2
			mov shape,2			; Shape2
			invoke CheckRadioButton,hDlg,IDC_RADIO1,IDC_RADIO4,wParam
	   	.ELSEIF eax==IDC_RADIO3
			mov shape,3			; Shape3
			invoke CheckRadioButton,hDlg,IDC_RADIO1,IDC_RADIO4,wParam
		.ELSEIF eax==IDC_RADIO4
			mov shape,4			; Shape4
			invoke CheckRadioButton,hDlg,IDC_RADIO1,IDC_RADIO4,wParam
                .ENDIF
        .ELSE
                mov eax,FALSE
                ret
        .ENDIF

        mov  eax,TRUE
        ret
ShapeDlg 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_PICK_SHAPE	; Display the Shape dialog box
		    invoke DialogBoxParam, hInstance, ADDR DlgName,hWnd,OFFSET ShapeDlg,NULL 	
		    invoke InvalidateRect, hWnd,NULL,TRUE
		    invoke UpdateWindow, hWnd
		.ELSEIF eax==ID_PICK_EXIT
		    invoke SendMessage,hWnd,WM_CLOSE,0,0
                .ELSEIF eax==ID_HELP_ABOUT
                    invoke DialogBoxParam,hInstance, ADDR AboutDlgName,hWnd,OFFSET AboutDlg,NULL
                .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 selected shape
;	-----------------------
		mov	eax,shape

		.IF	eax==1
			invoke MoveToEx,hdc,50,60,NULL
			invoke LineTo,hdc,100,300
		.ELSEIF eax==2
			invoke Rectangle,hdc,20,30,200,300
		.ELSEIF eax==3
			invoke Ellipse,hdc,10,30,200,300
		.ELSEIF eax==4
			invoke MoveToEx,hdc,200,100,NULL
			invoke LineTo,hdc,320,300
			invoke LineTo,hdc,200,300
			invoke LineTo,hdc,200,100
		.ENDIF
		
		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
