format PE GUI 4.0
entry start

include 'win32ax.inc'

section '.data' data readable writeable
 _class db 'FASMWIN32',0
 _title  db 'Right Click Test',0
 _error db 'Startup failed.',0

 wc WNDCLASS 0,WindowProc,0,0,NULL,NULL,NULL,COLOR_BTNFACE+1,NULL,_class

 msg MSG

 pnt POINT

 rct RECT

 hInstance   dd ?

 hBT1         dd ?
 hBT2         dd ?
 hBT3         dd ?
 hST1         dd ?

 tBT1   db 'tBT1',0
 tBT2    db 'tBT2',0
 tBT3    db 'tBT3',0
 tST1    db 'tST1',0

 ctlClsNameTips  db '_class32',0
 
; AG62:
fmt db '-- WM_CONTEXTMENU: WPARAM = %08X, LPARAM = %08X',0
; ==============================================================

section '.code' code readable executable
   start:
        invoke  GetModuleHandle,0
        mov [wc.hInstance],eax
  invoke LoadIcon,0,IDI_APPLICATION
  mov [wc.hIcon],eax
  invoke LoadCursor,0,IDC_ARROW
  mov [wc.hCursor],eax
  invoke RegisterClass,wc
  test eax,eax
  jz .error
  invoke CreateWindowEx,0,_class,_title,\
      WS_VISIBLE+WS_DLGFRAME+WS_SYSMENU,\
      10,10,310,160,\
      NULL,NULL,[wc.hInstance],NULL
  test eax,eax
  jz .error

  .msg_loop:
   invoke GetMessage,msg,NULL,0,0
   cmp eax,1
   jb .end_loop
   jne .msg_loop
   invoke TranslateMessage,msg
   invoke DispatchMessage,msg
   jmp .msg_loop

  .error:
 invoke MessageBox,NULL,_error,NULL,MB_ICONERROR+MB_OK

  .end_loop:
 invoke ExitProcess,[msg.wParam]

   proc WindowProc uses ebx esi edi,hDlg,uMsg,wParam,lParam

  cmp [uMsg],WM_RBUTTONUP
  je    .wmRBUTTONUP
  cmp  [uMsg],WM_CREATE
  je   .wmCREATE
  cmp [uMsg],WM_COMMAND
  je .wmcommand
  cmp [uMsg],WM_DESTROY
  je .wmdestroy

    ; AG62:
    cmp       [uMsg],WM_CONTEXTMENU
    je        .wmcontext
    ; =======================

  .defwndproc:
  invoke DefWindowProc,[hDlg],[uMsg],[wParam],[lParam]
  jmp .finish

    ; AG62:
.wmcontext:
    ; allocate local buffer
    sub       esp, 80
    mov       edi, esp
    ; print out the msg data
    cinvoke   wsprintfA, edi, fmt, [wParam],[lParam]
    invoke    OutputDebugStringA, edi
    ; free local buffer
    add       esp, 80
    ; return 0 from WNDPROC
    xor       eax, eax
    jmp       .finish
    ; =======================

  .wmRBUTTONUP:
  invoke GetCursorPos, pnt
  invoke GetWindowRect,[hST1],rct
  invoke PtInRect,rct,pnt.x,pnt.y
  cmp eax,0
  jne @F
  invoke MessageBox,0,'Right Click OK','!!!',0
  @@:

  xor eax,eax
  jmp .finish
  .wmCREATE:
   invoke CreateWindowEx,0,'BUTTON','BTN_1',\
     WS_CHILD + WS_VISIBLE,\
     10,10,60,25,\
     [hDlg],IDC_BT1,[wc.hInstance],NULL
   mov [hBT1],eax
   invoke CreateWindowEx,0,'BUTTON','BTN_2',\
     WS_CHILD + WS_VISIBLE,\
     235,10,60,25,\
     [hDlg],IDC_BT2,[wc.hInstance],NULL
   mov [hBT2],eax
   invoke CreateWindowEx,0,'STATIC','ST_1',\
     WS_CHILD + WS_VISIBLE + SS_NOTIFY,\
     125,10,60,25,\
     [hDlg],IDC_ST1,[wc.hInstance],NULL
   mov [hST1],eax
   invoke CreateWindowEx,0,'BUTTON','BTN_3',\
     WS_CHILD + WS_VISIBLE + SS_NOTIFY,\
     110,50,70,25,\
     [hDlg],IDC_BT3,[wc.hInstance],NULL
   mov [hBT3],eax


; -------------------- Tooltip ---------------------------------------------------------------

  ; invoke  InitCommonControls
  ; invoke  CreateWindowEx,NULL,ctlClsNameTips,NULL,\
    ; TTS_ALWAYSTIP,\
    ; CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,\
    ; NULL,NULL,[insH],NULL
    ; mov  [tipsH],eax

    ; mov  [id],0

    ; mov  [ti.cbSize],sizeof.TOOLINFO
    ; mov  [ti.uFlags],TTF_SUBCLASS

    ; push [hDlg]
    ; pop  [ti.hwnd]

  ; invoke  GetWindowRect,[hDlg],rect
  ; stdcall SetDlgToolArea,[hDlg],ti,tipsTxt1,[id],rect
    ; inc  [id]
  ; stdcall SetDlgToolArea,[hDlg],ti,tipsTxt2,[id],rect
    ; inc  [id]
  ; stdcall SetDlgToolArea,[hDlg],ti,tipsTxt3,[id],rect
    ; inc  [id]
  ; stdcall SetDlgToolArea,[hDlg],ti,tipsTxt4,[id],rect

  ; invoke  EnumChildWindows,[hDlg],EnumChild,ti

; -------------------------------------------------------------------------------------------

 xor eax,eax
 jmp .finish

 .wmcommand:

  ; other commands

  xor eax,eax
  jmp .finish

 .wmdestroy:
  invoke PostQuitMessage,0
  xor eax,eax
 .finish:
 ret
   endp

section '.idata' import data readable

library kernel32,'KERNEL32.DLL',\
 user32,'USER32.DLL',\
 comdlg32,'COMDLG32.DLL',\
 shell32,'SHELL32.DLL',\
 comctl32,'COMCTL32.dll'

  include '%fasminc%\api\kernel32.inc'
  include '%fasminc%\api\user32.inc'
  include '%fasminc%\api\comdlg32.inc'
  include '%fasminc%\api\shell32.inc'
  ;include '%fasminc%\api\ole32.inc'
  include '%fasminc%\api\comctl32.inc'

IDC_BT1 = 101
IDC_BT2 = 102
IDC_BT3 = 103
IDC_ST1 = 104
