format PE64 GUI 4.0
include 'win64wx.inc'
;Global const
 MY_STYLE = WS_OVERLAPPEDWINDOW + WS_VISIBLE
 LANG	  = LANG_VIETNAMESE+SUBLANG_DEFAULT
;end const
section '.code' data executable
  entry $
  start:
    sub     rsp,8
    invoke  GetModuleHandle,0
    mov     [wC.hInstance],rax
    invoke  LoadIcon,rax,100
    mov     [wC.hIcon],rax
    invoke  LoadCursor,0,IDC_ARROW
    mov     [wC.hCursor],rax
    invoke  RegisterClass,wC
    test    rax,rax
    jz	    @error.register

    invoke  CreateWindowEx,0,szClass,szTitle,MY_STYLE,128,128,400,300,NULL,NULL,[wC.hInstance],NULL
    test    rax,rax
    jz	    @error.main
    mov     [hWnd_],rax

  @loop:
    invoke  GetMessage,msg,NULL,0,0
    cmp     eax,1
    jb	    @exit
    jne     @loop
    invoke  TranslateMessage,msg
    invoke  DispatchMessage,msg
    jmp     @loop

  @error:
    .register:
      invoke  MessageBox,0,szError.register,szTitle,MB_ICONERROR
      jmp     @exit
    .main:
      invoke  MessageBox,0,szError.main,szTitle,MB_ICONINFORMATION
      jmp     @exit
  @exit:
    invoke  ExitProcess,[msg.wParam]
  ;---------/ This is main function \----------
  proc	  WndProc,hWnd,wMsg,wParam,lParam
    mov   [hWnd],rcx
    mov   [wMsg],rdx
    mov   [wParam],r8
    mov   [lParam],r9
   @@:
    cmp   edx,WM_CREATE
    jz	  .create
    cmp   edx,WM_SIZE
    jz	  .size
    cmp   edx,WM_COMMAND
    jz	  .Command
    cmp   edx,WM_DESTROY
    jz	  .destroy
   .def:
    invoke  DefWindowProc,rcx,rdx,r8,r9
    jmp     .exit
   .create:
    ;
    invoke  CreateWindowEx,WS_EX_CLIENTEDGE,'Edit',0,WS_VISIBLE+WS_CHILD+WS_HSCROLL+WS_VSCROLL+ES_AUTOHSCROLL+ES_AUTOVSCROLL+ES_MULTILINE,0,0,0,0,[hWnd],0,[wC.hInstance],NULL
    mov     [hEdit],rax
    invoke  CreateWindowEx,0,'button','MessageBox',WS_VISIBLE+WS_CHILD,10,10,80,25,[hWnd],0,[wC.hInstance],NULL
    mov     [hOK],rax
    invoke  CreateWindowEx,0,'button','Exit',WS_VISIBLE+WS_CHILD,100,10,80,25,[hWnd],0,[wC.hInstance],NULL
    mov     [hExit],rax
    invoke  CreateFont,16,0,0,0,0,FALSE,FALSE,FALSE,ANSI_CHARSET,OUT_RASTER_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,FIXED_PITCH+FF_DONTCARE,'Tahoma'
    mov     [hFont],rax
    invoke  SendMessage,[hEdit],WM_SETFONT,rax,FALSE
    invoke  SendMessage,[hOK],WM_SETFONT,[hFont],FALSE
    invoke  SendMessage,[hExit],WM_SETFONT,[hFont],FALSE
    jmp     .exit
   .size:
    invoke  GetClientRect,[hWnd],rc
    invoke  MoveWindow,[hEdit],[rc.left],45,[rc.right],[rc.bottom],TRUE
    xor     rax,rax
    jmp     .exit
   .destroy:
    invoke  PostQuitMessage,0
    xor     eax,eax
  .Command:

    cmp  r9,[hOK]
    jz	 .ok
    cmp  r9,[hExit]
    jz	 .quit
    jmp  .exit
    .ok:
	invoke	SendMessage,[hEdit],WM_GETTEXTLENGTH,0,0
	add	eax,1
	mov	[nSize],rax
	invoke	VirtualAlloc,0,rax,MEM_COMMIT,PAGE_READWRITE
	mov	[hBuff],rax
	invoke	SendMessage,[hEdit],WM_GETTEXT,[nSize],hBuff
	;
	;mov     rcx,0    ;<- like this it run ok but not use
	mov	rcx,[hWnd_]  ;<- like this if not type in textbox it can run
			     ;if type any charector it can't run
			     ; Why
	invoke	MessageBox,rcx,hBuff,szTitle,MB_ICONINFORMATION
	xor	rax,rax
	jmp	.exit
    .quit:
	invoke	PostQuitMessage,0
	xor	eax,eax
   .exit:
    ret
  endp


 ;================[ const string ]==============
 szClass    TCHAR  'MainWindow',0
 szTitle    TCHAR  'Win64 Example',0
 szError:
  .register TCHAR  'Error on register Class',0
  .create   TCHAR  'Error on Object',0
  .main     TCHAR  'Error on Create main window',0

;-----------------------------------------------------------------------------
section '.data' data readable writeable
  wC	   WNDCLASS 0,WndProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE,NULL,szClass

section '.bss' data readable writeable
  hOK	   dq	?
  hExit    dq	?
  hEdit    dq	?
  hFont    dq	?
  hBuff    dq	?
  nSize    dq	?
  hWnd_    dq	?
  msg	   MSG
  rc	   RECT

section 'idata' import data readable
   library  kernel32,'kernel32.dll',\
	    user32,'user32.dll',\
	    gdi32,'gdi32.dll'
   include  'api\user32.inc'
   include  'api\kernel32.inc'
   include  'api\gdi32.inc'

;section 'resource' resource data readable
;   directory RT_VERSION,versions
;   resource  versions,\
;             1,LANG,version
;   versioninfo version,VOS__WINDOWS32,VFT_APP,VFT2_UNKNOWN,LANG,0,\
;             'FileDescription','...............',\
;             'CompanyName','................',\
;             'LegalCopyright','......................',\
;             'FileVersion','0.0.0.1',\
;             'ProductVersion','0.0.0.1',\
;             '1.Teen goosc','Win64.exe'

;section '.reloc' fixups data discardable