home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / OS2BAS.ZIP / WINHOOK.BAS < prev    next >
BASIC Source File  |  1989-09-14  |  4KB  |  121 lines

  1. '***********************************************************
  2. '* 
  3. '* Program Name: WinHook.BAS
  4. '*
  5. '* Include File: WinHook.BI
  6. '*
  7. '* Functions   :
  8. '*               WinSetHook
  9. '*               WinCallMsgFilter
  10. '*               WinReleaseHook
  11. '*
  12. '* Description : This program demonstrates the hook functions.
  13. '*
  14. '*               A message filter hook is created with
  15. '*               WinSetHook and is processed in the FUNCTION
  16. '*               MsgFilterHook.  The function pointer for
  17. '*               WinSetHook is returned from RegMsgFilterHook.
  18. '*
  19. '*               WinCallMsgFilter is used in the message loop
  20. '*               to filter messages through MsgFilterHook.
  21. '*
  22. '*               MsgFilterHook simply writes "Message filtered"
  23. '*               to the file, WinHook.OUT.
  24. '*
  25. '*               Finally, the hook is released with
  26. '*               WinReleaseHook.
  27. '*
  28. '*               This program only demonstrates the calls to the
  29. '*               various hook functions.  For more information
  30. '*               on using hooks, see Chapter 28 of "OS/2
  31. '*               Programmer's Reference, Volume 1".
  32. '***********************************************************
  33.  
  34. '*********         Initialization section        ***********
  35.  
  36. REM $INCLUDE: 'PMBase.BI'
  37. REM $INCLUDE: 'WinHook.BI'
  38. DECLARE FUNCTION RegMsgFilterHook&
  39.  
  40. OPEN "WinHook.OUT" FOR OUTPUT AS #1
  41. DIM aqmsg AS QMSG
  42.  
  43. flFrameFlags& =  FCFTITLEBAR      OR FCFSYSMENU OR _
  44.                  FCFSIZEBORDER    OR FCFMINMAX  OR _
  45.                  FCFSHELLPOSITION OR FCFTASKLIST
  46.  
  47. szClientClass$ = "ClassName" + CHR$(0)
  48.  
  49. hab&  = WinInitialize    (0)
  50. hmq&  = WinCreateMsgQueue(hab&, 0)
  51.  
  52. '**** Set message filter hook
  53. bool% = WinSetHook(hab&, hmq&, HKMSGFILTER, RegMsgFilterHook, 0)
  54. PRINT #1, "WinSetHook:", bool%
  55.  
  56. bool% = WinRegisterClass(_
  57.         hab&,_
  58.         MakeLong(VARSEG(szClientClass$), SADD(szClientClass$)),_
  59.         RegBas,_
  60.         0,_
  61.         0)
  62.  
  63. hwndFrame& = WinCreateStdWindow (_
  64.              HWNDDESKTOP,_
  65.              WSVISIBLE,_
  66.              MakeLong (VARSEG(flFrameFlags&),  VARPTR(flFrameFlags&)),_
  67.              MakeLong (VARSEG(szClientClass$), SADD(szClientClass$)),_
  68.              0,_
  69.              0,_
  70.              0,_
  71.              0,_
  72.              MakeLong (VARSEG(hwndClient&), VARPTR(hwndClient&)))
  73.  
  74. '**************         Message loop         ***************
  75.  
  76. WHILE WinGetMsg(hab&, MakeLong(VARSEG(aqmsg), VARPTR(aqmsg)), 0, 0, 0)
  77.   '**** If message not filtered, dispatch to window procedure
  78.   IF NOT(WinCallMsgFilter(hab&, MakeLong(VARSEG(aqmsg), VARPTR(aqmsg)), 0)) THEN
  79.     bool% = WinDispatchMsg(hab&, MakeLong(VARSEG(aqmsg), VARPTR(aqmsg)))
  80.   END IF
  81. WEND
  82.  
  83. '***********         Finalize section        ***************
  84.  
  85. bool% = WinDestroyWindow   (hwndFrame&)
  86.  
  87. '**** Release message filter hook
  88. bool% = WinReleaseHook(hab&, hmq&, HKMSGFILTER, RegMsgFilterHook, 0)
  89. PRINT #1, "WinReleaseHook:", bool%
  90.  
  91. bool% = WinDestroyMsgQueue (hmq&)
  92. bool% = WinTerminate (hab&)
  93.  
  94. CLOSE #1
  95.  
  96. END
  97.  
  98. '***********         Window procedure        ***************
  99.  
  100. FUNCTION ClientWndProc& (hwnd&, msg%, mp1&, mp2&) STATIC
  101.      DIM ClientRect AS RECTL
  102.      ClientWndProc& = 0
  103.      SELECT CASE msg%
  104.      CASE WMPAINT     'Paint the window with background color
  105.         hps&  = WinBeginPaint(hwnd&, 0,_
  106.                 MakeLong(VARSEG(ClientRect), VARPTR(ClientRect)))
  107.         bool% = WinFillRect  (hps&,_
  108.                 MakeLong(VARSEG(ClientRect), VARPTR(ClientRect)),0)
  109.         bool% = WinEndPaint  (hps&)
  110.      CASE ELSE        'Pass control to system for other messages
  111.         ClientWndProc& = WinDefWindowProc(hwnd&, msg%, mp1&, mp2&)
  112.      END SELECT
  113. END FUNCTION
  114.  
  115.  
  116. '***************** Message Filter Hook Function *******************
  117.  
  118. FUNCTION MsgFilterHook%(hab&, msgf%, pqmsg&) STATIC
  119.     PRINT #1, "Message filtered"
  120. END FUNCTION
  121.