Windows Event Handler in MASM

View Poll Results: Did you find this snippet useful
Very Much 0 0%
Somewhat Interesting 1 100.00%
Hard to understand 0 0%
Wouldn't design app in assembly 0 0%
Voters: 1. You may not vote on this poll

Tight_Coder_Ex Tight_Coder_Ex is offline Offline Nov 18th, 2009, 3:28 pm |
0
This procedure dispatches application defined event handlers and alternately executes default process for both standard or sub/super classed windows.

Map is defined as follows. In most cases I define this table in .data, but in can be in .const also. I choose writeable memory as application can redefine characteristics.
  1. MWMap dd 0 ; Address of sub or super class default proc
  2. dw (MWEnd - MWMap - 6) / 6 ; Determine number of messages in map
  3. dw WM_DESTROY
  4. dd QuitApp
  5. dw WM_CREATE
  6. dd CMainWnd
  7. MWEnd equ $

Entry point of windows procedure requires only two instructions
  1. MWndProc proc
  2.  
  3. mov esi, offset MWMap ; Point to message map for this window
  4. jmp ScanMap
  5.  
  6. MWndProc endp

By whatever means you choose to intialize WNDCLASSEX then entry point need only be specified as the 3 parameter
  1. Wc WNDCLASSEX < 30h, CS_HREDRAW or CS_VREDRAW, MWndProc, 0, 0,\
  2. 0, 0, 0, COLOR_APPWORKSPACE + 1, NULL, AppName, 0>

Here is an example of the actual handler
  1. CMainWnd proc uses ebx edi
  2.  
  3. LOCAL Rc:RECT
  4.  
  5. lea ebx, Rc
  6. mov edi, [esi]
  7.  
  8. invoke GetClientRect, dword ptr [esi], ebx ; Get client windows metrics
  9. invoke InflateRect, ebx, -32, -32 ; Adjust for border
  10. mov eax, [ebx + 8]
  11. sub eax, [ebx]
  12.  
  13. invoke CreateWindowEx,
  14. WS_EX_STATICEDGE or WS_EX_CLIENTEDGE, ADDR EditClass, NULL, \
  15. WS_VISIBLE or WS_BORDER or WS_CHILD or ES_AUTOHSCROLL, \
  16. 32, 32, eax, 22, edi, 64, Wc.hInstance, NULL
  17.  
  18. mov EditWnd, eax
  19. mov edi, eax ; Save copy of window
  20. and eax, eax
  21. jnz @f
  22.  
  23. int 3
  24.  
  25. @@: invoke SetWindowLong, eax, GWL_WNDPROC, ADDR EditProc
  26. mov EditMap, eax
  27.  
  28. invoke GetStockObject, ANSI_VAR_FONT
  29. invoke SendMessage, edi, WM_SETFONT, eax, 0
  30. invoke SendMessage, edi, EM_SETMARGINS, EC_LEFTMARGIN or EC_RIGHTMARGIN, 0180018H
  31. invoke SetFocus, edi
  32.  
  33. stc ; Inhibit default processing by setting carry
  34. ret
Quick reply to this message  
Assembly Syntax
  1. ScanMap proc
  2.  
  3. ; EBX = Pointer to default procedure if window has been sub/super classed.
  4. ; ECX = Number of handlers in map (can be NULL, although not usual).
  5.  
  6. lodsd ; Get address of default window procedure.
  7. mov ebx, eax ; Save in case window has been sub/super classed
  8. lodsw ; Number of messages in map
  9. cwde ; Sign extend to 32 bits, can be NULL but not usually the case
  10.  
  11. .if eax ; Does map have defninitions
  12. mov ecx, eax ; Move into counter register
  13.  
  14. ; Cycle through each of the application defined handlers until a match is found,
  15. ; otherwise just automatically drop into default message processing.
  16.  
  17. @@: lodsw ; Get message number from map
  18. mov edx, eax ; Copy it so next instruction doesn't destroy
  19. lodsd ; Pointer to application defined handler
  20.  
  21. .if dx == [esp + 8] ; When match if found, execute handler
  22. lea esi, [esp + 4] ; ESI points to API parameters
  23. call eax ; Call handler
  24. jnc @F ; Do default processing if no carry
  25.  
  26. xor eax, eax ; Set default return condition
  27. ret 16 ; Clean-up stack and return.
  28. .endif
  29.  
  30. loopnz @B ; Keep comparing until ECX = Zero.
  31. .endif
  32.  
  33. @@: ; Either there wasn't a need to handle this event or the handler requires default processing
  34.  
  35. .if !ebx
  36. jmp DefWindowProc ; Do default processing for this window
  37. .endif
  38.  
  39. ; Processing of a subclassed window requires address of default code so it will
  40. ; be placed on stack at this point.
  41.  
  42. xchg [esp], ebx ; Exchange default processing address
  43. push ebx ; with return and push on stack again
  44. jmp CallWindowProc ; Do default processing for this window

Message:


Thread Tools Search this Thread



Tag cloud for Assembly
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC