home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_122 / 6.ddi / TASMEXMP.ZIP / WINPROG.ASM < prev   
Encoding:
Assembly Source File  |  1992-06-10  |  8.0 KB  |  242 lines

  1. ; Turbo Assembler    Copyright (c) 1988, 1991 By Borland International, Inc.
  2.  
  3. ; WINPROG.ASM - Template for writing windows programs
  4.  
  5. ; From the Turbo Assembler Users Guide
  6.  
  7. MULTIPLE_INST = 0       ;set to 1 to allow multiple instances of
  8.                         ; the program, 0 otherwise.
  9.  
  10. P286                    ;select pertinent processor
  11. WARN PRO                ;enable Turbo Assembler's checking for
  12.                         ; protection violations that can be caught
  13.                         ; at assembly time
  14.  
  15. MODEL      SMALL        ;other models are allowed
  16. INCLUDELIB SLIBCEW      ;library must be appropriate for model
  17.  
  18. ;Include the standard library for Windows
  19. INCLUDELIB LIBW
  20. EXTRN C _ACRTUSED:ABS   ;forces inclusion of the library
  21.  
  22. ;Enable local symbols
  23. LOCALS
  24.  
  25. ;Include files
  26. INCLUDE WINDOWS.INC   ;contains definitions for Windows data types,etc.
  27. INCLUDE WINPROG.INC   ;user-defined include file for user messages,etc.
  28.  
  29. ;External windows procedures
  30. ;All windows functions used must be declared as external
  31. EXTRN PASCAL LoadIcon:FAR          ;get a pointer to an icon
  32. EXTRN PASCAL LoadCursor:FAR        ;get a pointer to a cursor
  33. EXTRN PASCAL GetStockObject:FAR    ;get brush handle
  34. EXTRN PASCAL RegisterClass:FAR     ;register a window class
  35. EXTRN PASCAL CreateWindow:FAR      ;create a window
  36. EXTRN PASCAL ShowWindow:FAR        ;enable window
  37. EXTRN PASCAL UpdateWindow:FAR      ;force a draw
  38. EXTRN PASCAL GetMessage:FAR        ;get next message
  39. EXTRN PASCAL DispatchMessage:FAR   ;dispatch message
  40. EXTRN PASCAL DefWindowProc:FAR     ;process message in default manner
  41. EXTRN PASCAL PostQuitMessage:FAR   ;quit
  42. EXTRN PASCAL BeginPaint:FAR        ;begin paint request
  43. EXTRN PASCAL EndPaint:FAR          ;end paint request
  44. EXTRN PASCAL TextOut:FAR           ;display text
  45.  
  46. ;Our DLL function
  47. EXTRN PASCAL SetHello:FAR          ;DLL function
  48.  
  49. DATASEG
  50. lpszClass  db 'GenericWClass',0    ;class name of windows used by
  51.                                    ; application
  52. lpszTitle  db 'Generic Sample Assembly Application',0
  53.                                    ;title of the application
  54. lpszMenu   db 'GenericMenu',0      ;default menu title
  55.  
  56. ;<<Other initialized data goes here>>
  57.  
  58. UDATASEG
  59. hInstance  dw ?                    ;current instance handle
  60.  
  61. ;<<Other uninitialized data goes here>>
  62.  
  63. CODESEG
  64. ;[Main Application Windows procedure]
  65. ;Execution starts here!
  66. ;Returns AX=0 if initialization was unsuccessful
  67. WinMain PROC WINDOWS PASCAL
  68. PUBLIC PASCAL WinMain
  69. ARG @@hInstance:WORD,              \Handle to this instance
  70.     @@hPrevInstance:WORD,          \Handle to previous instance
  71.     @@lpszCmd:DWORD,               \Pointer to command line
  72.     @@nShow:WORD                   ;value to pass to ShowWindow
  73. USES ES,SI,DI
  74. UDATASEG
  75.     @@msg msgstruct ?              ;message storage
  76.     @@wc wndclass ?                ;window class structure
  77. CODESEG
  78. IF MULTIPLE_INST EQ 0
  79.    CMP @@hPrevInstance,0
  80.    JZ @@ok
  81.    XOR AX,AX
  82.    RET
  83. @@ok:
  84. ENDIF
  85.    MOV AX,@@hInstance
  86.    MOV hInstance,AX
  87.  
  88.    ;Initialize window class structure.
  89.    ;These parameters may change based on your preferences
  90.    MOV @@wc.clshInstance,AX
  91.    XOR AX,AX
  92.    MOV WORD PTR @@wc.clslpfnWndProc,OFFSET MainMsgProc
  93.    MOV WORD PTR @@wc.clslpfnWndProc+2,CS
  94.    MOV WORD PTR @@wc.clslpszClassName,OFFSET lpszClass
  95.    MOV WORD PTR @@wc.clslpszClassName+2,DS
  96.    MOV WORD PTR @@wc.clslpszMenuName,OFFSET lpszMenu
  97.    MOV WORD PTR @@wc.clslpszMenuName+2,DS
  98.    MOV @@wc.clsStyle,AX
  99.    MOV @@wc.clscbClsExtra,AX
  100.    MOV @@wc.clscbWndExtra,AX
  101.    CALL LoadIcon PASCAL,AX,AX IDI_APPLICATION
  102.    MOV @@wc.clshIcon,AX
  103.    XOR AX,AX
  104.    CALL LoadCursor PASCAL,AX,AX IDC_ARROW
  105.    MOV @@wc.clshCursor,AX
  106.    CALL GetStockObject PASCAL,WHITE_BRUSH
  107.    MOV @@wc.clshbrBackground,AX
  108.  
  109.    ;Register class
  110.    CALL RegisterClass PASCAL,DS OFFSET @@wc
  111.  
  112.    ;Now create window.
  113.    ;This uses default parameters; your preferences may vary.
  114.    XOR  AX,AX
  115.    MOV  BX,CW_USEDEFAULT
  116.    CALL CreateWindow PASCAL,DS OFFSET lpszClass,DS OFFSET lpszTitle,\
  117.         WS_OVERLAPPEDWINDOW AX,\
  118.         BX,BX,BX,BX,\
  119.         AX,AX,hInstance,AX AX
  120.  
  121.    ;AX is the window descriptor. Display window and update it.
  122.    PUSH AX
  123.    CALL ShowWindow PASCAL,AX,@@nShow
  124.    POP AX
  125.    CALL UpdateWindow PASCAL,AX
  126.  
  127.    ;Message handler loop.
  128. @@LUP: CALL GetMessage PASCAL,DS OFFSET @@msg,0,0,0
  129.    AND AX,AX
  130.    JZ @@RET
  131.    CALL DispatchMessage PASCAL,DS OFFSET @@msg
  132.    JMP @@LUP
  133. @@RET:                           ;no more messages
  134.    MOV AX,-1
  135. @@QUIT: RET
  136. WinMain ENDP
  137.  
  138. ;----------------------------------------------------------------------
  139. ;Main message-handling procedure
  140. ;An address to this procedure is passed to Windows during
  141. ;initialization portion of WinMain.
  142. ;This works by scanning the message dispatch table for a matching;message, and calling the appropriate handler. If message is not
  143. ;present in handler table, it is passed back to Windows to handle.
  144. ;Returns a value in DX:AX that is 0 if message was handled.
  145. ;----------------------------------------------------------------------
  146. MainMsgProc PROC WINDOWS PASCAL FAR
  147. PUBLICDLL PASCAL MainMsgProc
  148. ARG @@hwnd:WORD,                 \Window descriptor
  149.     @@message:WORD,              \ID of message
  150.     @@wParam:WORD,               \Additional word
  151.     @@lParam:DWORD               ;additional dword
  152. USES ES,SI,DI
  153.  
  154. DATASEG
  155.  
  156. ;Table of messages this procedure will handle.
  157.  
  158. ;This structure represents an entry in a message table.
  159. @@MsgRec  STRUC
  160. @@MsgType DW 0                   ;message ID
  161. @@MsgProc DW ?                   ;procedure address
  162. @@MsgRec  ENDS
  163.  
  164. ;Add to these as you wish.
  165. @@MsgTable LABEL @@MsgRec
  166.    @@MsgRec {@@MsgType=WM_DESTROY,  @@MsgProc=DestroyWindow}
  167.    @@MsgRec {@@MsgType=WM_PAINT,    @@MsgProc=PaintWindow}
  168.    @@MsgRec {@@MsgType=IDM_ABOUT,   @@MsgProc=DoAbout}
  169.    @@MsgRec {}    ;Terminate the list
  170.  
  171. CODESEG
  172.  
  173.    ;Dispatch based on message type.
  174.    MOV SI,OFFSET @@MsgTable
  175. @@LP: MOV AX,[SI.@@MsgType]
  176.    AND AX,AX
  177.    JZ @@NF
  178.    CMP AX,@@message
  179.    JZ @@FD
  180.    ADD SI,SIZE @@MsgRec
  181.    JMP SHORT @@LP
  182. @@FD:                     ;found; call routine to handle message
  183.    CALL [SI.@@MsgProc] PASCAL,@@hwnd,@@wParam,@@lParam
  184.    XOR AX,AX
  185.    XOR DX,DX
  186.    RET
  187. @@NF:                     ;not found; take default action
  188.    CALL DefWindowProc PASCAL,@@hwnd,@@message,@@wParam,@@lParam
  189.    ;Return whatever the default handler returns
  190.    RET
  191. MainMsgProc ENDP
  192.  
  193. ;----------------------------------------------------------------------
  194. ;These are the routines to handle messages.
  195. ;These message-handling routines are NEAR PASCAL procedures that take
  196. ;as arguments the window handle, and the word and dword param from
  197. ;the message.
  198. ;New message-handling routines should be added here.
  199. ;----------------------------------------------------------------------
  200.  
  201. ;Process WM_DESTROY message
  202. ;Closes the window.
  203. DestroyWindow PROC PASCAL NEAR
  204. ARG @@hwnd:WORD,                   \Window descriptor
  205.     @@wParam:WORD,                 \Word parameter
  206.     @@lParam:DWORD                 ;dword parameter
  207.     CALL PostQuitMessage PASCAL,0
  208.     RET
  209. DestroyWindow ENDP
  210.  
  211. ;Process WM_PAINT message
  212. ;Repaints the window.
  213. ;(This demonstrates use of the SetHello DLL routine)
  214. PaintWindow PROC PASCAL NEAR
  215. ARG @@hwnd:WORD,                   \Window descriptor.
  216.     @@wParam:WORD,                 \Word parameter
  217.     @@lParam:DWORD                 ;dword parameter
  218. LOCAL @@hdc:WORD                   ;handle to device context
  219. UDATASEG
  220.    @@ps   PAINTSTRUCT ?
  221.    @@text db 80 dup (?)
  222. CODESEG
  223.    CALL BeginPaint PASCAL,@@hwnd,DS OFFSET @@ps
  224.    MOV @@hdc,AX
  225.    CALL SetHello PASCAL,DS OFFSET @@text
  226.    SUB BX,BX
  227.    CALL TextOut PASCAL,@@hdc,BX,BX,DS OFFSET @@text,AX
  228.    CALL EndPaint PASCAL,@@hwnd,DS OFFSET @@ps
  229.    RET
  230. PaintWindow ENDP
  231.  
  232. ;Process IDM_ABOUT message.
  233. ;Display the "About..." box.
  234. DoAbout PROC PASCAL NEAR
  235. ARG @@hwnd:word,                   \Window descriptor
  236.     @@wParam:word,                 \Word parameter
  237.     @@lParam:dword                 ;dword parameter
  238. ;<<Your ABOUT stuff goes here>>
  239. RET
  240. DoAbout ENDP
  241. END
  242.