format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'
include '%fasminc%\Riched32.inc'
section '.code' code readable executable

  start:
        invoke  LoadLibrary, RichEditDllName
	invoke	GetModuleHandle,0
	mov	[wc.hInstance],eax
        invoke  RegisterClass, wc
        invoke  CreateWindowEx,0,myClass,myTitle,WS_VISIBLE+WS_OVERLAPPEDWINDOW,CW_USEDEFAULT, CW_USEDEFAULT, 700, 400, NULL, NULL, [wc.hInstance],NULL
        mov     [hWndMain], eax
        invoke  InitCommonControls
        invoke  GetCurrentProcess
        mov     [hProcess], eax
  msg_loop:
	invoke	GetMessage,msg,NULL,0,0
	or	eax,eax
	jz	end_loop
        invoke  TranslateAccelerator, [hWndMain], [hAccel], msg
        or      eax, eax
        jnz     msg_loop
        invoke  IsDialogMessage, [hWndMain], msg
        or      eax, eax
        jnz     msg_loop
	invoke	TranslateMessage,msg
	invoke	DispatchMessage,msg
	jmp	msg_loop
  end_loop:
	invoke	ExitProcess,[msg.wParam]

proc wndProc hwnd,wmsg,wparam,lparam
	push	ebx esi edi
	cmp	[wmsg],WM_DESTROY
        je      .wmDESTROY
        cmp     [wmsg],WM_CREATE
        je      .wmCREATE
        cmp     [wmsg], WM_NOTIFY
        je      .wmNOTIFY
    .wmDEFAULT:
	invoke	DefWindowProc,[hwnd],[wmsg],[wparam],[lparam]
        jmp     .wmBYE
    .wmNOTIFY:
        mov     eax, [lparam]
        cmp     [eax+MSGFILTER.msg], WM_LBUTTONDOWN
        je      .wmNwmLBD
        jmp     .wmDEFAULT

;**************  PROBLEM AREA **********************

    .wmNwmLBD:
;        int3            ; the four alternatives I've tried.
        mov     ebx, [eax+MSGFILTER.lparam]
        mov     eax, ebx
        and     eax, 0x0000FFFF
        shr     ebx, 16
        mov     [pt.x], eax
        mov     [pt.y], ebx
        invoke  SendMessage, [editHnd], EM_CHARFROMPOS, 0, pt

;***************************************************
        push    eax
        shr     eax, 16         ;0 based line No. should now be in eax
        stdcall Word2Asc, eax, tmpBuff1
        pop     eax
        and     eax, 0x0000FFFF ;0 based char No. should now be in eax
        stdcall Word2Asc, eax, tmpBuff2
        invoke  SetWindowText, [hWndMain], winStr1
        jmp     .wmDEFAULT
    .wmCREATE:
        invoke  GetClientRect, [hwnd], rect
        invoke  CreateWindowEx, WS_EX_CLIENTEDGE, RichEditClass, NULL, WS_CHILD+WS_VISIBLE+WS_VSCROLL+WS_HSCROLL+ES_AUTOVSCROLL+ES_AUTOHSCROLL+ES_MULTILINE+ES_WANTRETURN, [rect.left], [rect.top], [rect.right], [rect.bottom], [hwnd], NULL, [wc.hInstance], NULL
        mov     [editHnd], eax
        invoke  SendMessage,[editHnd],EM_SETEVENTMASK,0,ENM_MOUSEEVENTS ;capture mouse events in edit control
        invoke  SetWindowText, [editHnd], tmpStr
        jmp     .wmBYE
    .wmDESTROY:
	invoke	PostQuitMessage,0
	xor	eax,eax
    .wmBYE:
	pop	edi esi ebx
	ret
endp

proc Word2Asc HexVal, buff
        push    eax ebx ecx edx
        mov     eax, [HexVal]
        mov     ebx, 0x0A
        mov     ecx, 5
    @@:
        xor     edx, edx
        div     ebx
        add     edx, 0x30
        push    edx
        loop    @b
        mov     edi, [buff]
        mov     ecx, 5
    @@:
        pop     eax
        stosb
        loop    @b
        pop     edx ecx ebx eax
        ret
endp


section '.data' data readable writeable

pt                      POINT
reEM_CHARFROMPOS        = WM_USER+39
winStr1                 db 'Line '
tmpBuff1                db 5 dup ?
winStr2                 db '     Char '
tmpBuff2                db 5 dup ?, 0
editHnd                 dd ?
hWndMain                dd ?
hProcess                dd ?
hAccel                  dd ?
msg                     MSG
rect                    RECT
wc                      WNDCLASS 0, wndProc, 0, 0, NULL, NULL, NULL, COLOR_BTNFACE+1, NULL, myClass
RichEditDllName         db 'riched20.DLL', 0
RichEditClass           db 'RICHEDIT20A', 0
myTitle                 db 'Test', 0
myClass                 db 'Test', 0
ENM_MOUSEEVENTS         = 0x00020000
tmpStr                  db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A
                        db 'A line of text in the window', 0x0D, 0x0A, 0
struct	MSGFILTER
	nmhdr		NMHDR
	msg		dd ?
        wparam          dd ?
        lparam          dd ?
ends


section '.idata' import data readable writeable

library kernel32,'KERNEL32.DLL',\
        user32,  'USER32.DLL',\
	gdi32,	 'GDI32.DLL',\
        comdlg32,'COMDLG32.DLL',\
        advapi32,'ADVAPI32.DLL',\
        comctl32,'COMCTL32.DLL'

include '%fasminc%\apia\Kernel32.inc'
include '%fasminc%\apia\User32.inc'
include '%fasminc%\apia\Gdi32.inc'
include '%fasminc%\apia\Comdlg32.inc'
include '%fasminc%\apia\Advapi32.inc'
include '%fasminc%\apia\Comctl32.inc'
