; http://www.chat.ru/~babekcom/
; Пример обработки нажатия клавиши мыши и вывод текста в окно
;
.386
.model flat,stdcall

include	win32.inc

.data
szTitleName	db "Mouse", 0
szClassName	db "SimpleWinClass",0
AppName  	db "Mouse Click",0
MouseClick 	db 0         		; 0=нажатия все еще нет

.data?

hInst	dd ?
msg		MSGSTRUCT	<?>
wc		WNDCLASS	<?>
Rect		RECT		<?>
PaintStruct	PAINTSTRUCT	<?>
point		POINT		<?>
newhwnd		dd ?
hDC		dd ?
CommandLine	dd ?

.code

start:
	call	GetModuleHandleA, 0
	mov	[hInst],eax
	call	GetCommandLineA
	call	WinMain, [hInst], 0, [CommandLine], SW_SHOWNORMAL
	call	ExitProcess,eax

WinMain	proc	uses ebx edi esi, hwnd:DWORD, wmsg:DWORD,\
		wparam:DWORD, lparam:DWORD

        mov     [wc.clsStyle], CS_HREDRAW + CS_VREDRAW
        mov     [wc.clsLpfnWndProc], offset WndProc
        mov     [wc.clsCbClsExtra], 0
        mov     [wc.clsCbWndExtra], 0

        mov     eax, [hInst]
        mov     [wc.clsHInstance], eax

        call    LoadIconA, 0, IDI_APPLICATION
        mov     [wc.clsHIcon], eax

        call    LoadCursorA, 0 ,IDC_ARROW
        mov     [wc.clsHCursor], eax

        call    GetStockObject, WHITE_BRUSH
        mov     [wc.clsHbrBackground], eax

        mov     [wc.clsLpszMenuName], 0
        mov     [wc.clsLpszClassName], offset szClassName

        call    RegisterClassA, offset wc

        call    CreateWindowExA, 0, offset szClassName, offset szTitleName, \
                WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, \
                CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,0,0, \
                [hInst], 0

        mov     [newhwnd], eax

        call    ShowWindow, [newhwnd], SW_SHOWNORMAL
        call    UpdateWindow, [newhwnd]
;----------------------------------------------------------------------
	.WHILE TRUE
	call	GetMessageA, offset msg, 0,0,0
                .BREAK .IF (!eax)
	call	DispatchMessageA, offset msg
	.ENDW
	mov     eax, [wparam]
	ret
;----------------------------------------------------------------------
WinMain endp

WndProc	proc uses ebx edi esi, hwnd:DWORD, wmsg:DWORD,\
		wparam:DWORD, lparam:DWORD

	.IF wmsg==WM_DESTROY
	call	PostQuitMessage, 0
	.ELSEIF wmsg==WM_LBUTTONDOWN	; если нажата левая кнопка мыши
	mov	eax, [lparam]
	and	eax,0ffffh
	mov	[point.ptX],eax
	mov	eax,[lparam]
	shr	eax,16
	mov	[point.ptY],eax
	mov	[MouseClick],TRUE
	call	InvalidateRect, [hwnd], 0, TRUE
	.ELSEIF wmsg==WM_PAINT
	call	BeginPaint,[hwnd], offset PaintStruct	; получим контекст устройства
	mov    [hDC],eax				; на которое будет вывод
	.IF MouseClick
	call	TextOutA, [hDC], [point.ptX], [point.ptY], offset AppName,11
	.ENDIF
	call	EndPaint, [hwnd], offset PaintStruct	; удалим контекст устройства
	.ELSE
	call	DefWindowProcA, [hwnd], [wmsg], [wparam], [lparam]
		ret
	.ENDIF
	xor    eax,eax
	ret
WndProc endp
end start
