Author Topic: Intercepting Child Window messages  (Read 1279 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Author
  • *****
  • Posts: 3334
    • Oxygen Basic
Intercepting Child Window messages
« on: July 06, 2012, 01:17:04 AM »
I found this technique very useful for sending notifications to the main window. Function keys and control /escape/return keys are intercepted in the message loop before they reach child windows.

Code: OxygenBasic
  1.  
  2.   'MESSAGE LOOP
  3.  '============
  4.  '
  5.  sys bRet
  6.   '
  7.  do while bRet := GetMessage (&wm, 0, 0, 0)
  8.     if bRet == -1 then
  9.       'show an error message?
  10.    else
  11.       if wm.message=WM_KEYDOWN
  12.         m=wm.wparam
  13.         select case m
  14.         case VK_RETURN
  15.           for i=1 to nchw
  16.             if wm.hwnd=hchw(i)
  17.               SendMessage hwnd,WM_COMMAND,2000+VK_RETURN,0
  18.               if i>1 then continue while 'first window is editor
  19.            end if
  20.           next
  21.         case VK_ESCAPE
  22.           'SendMessage hwnd,WM_CLOSE,0,0
  23.        case VK_F1 to VK_F10
  24.           SendMessage hwnd,WM_COMMAND,1001+m-VK_F1, 0
  25.         case "A" to "Z"
  26.           if CtrlKey then
  27.             SendMessage hwnd,WM_COMMAND,2000+m, 0
  28.             if m=65 or m=70 or m=71 then continue while
  29.           end if
  30.         end select
  31.       end if
  32.       TranslateMessage @wm
  33.       DispatchMessage @wm
  34.     end if
  35.   wend
  36.   '
  37.  function=wm.wparam
  38.  
  39.   end function ; end of WinMain
  40.  

On the receiving end, WM_COMMAND messages are captured. The originating child window can be identified by GetFocus. (it may be the built-in Find Box or Goto Box here)

Code: OxygenBasic
  1.   case WM_COMMAND
  2.     sys id,co,hw,a,b
  3.     id=wparam and 0xffff
  4.     co=wparam >>16
  5.     hw=hchw[0]
  6.     select case id
  7.     case 1001 'F1
  8.      SetWindowText hWnd, "Help"
  9.       s=GetText hw
  10.       HelpMessageBox
  11.     case 1003 'F3
  12.      FindKeyword hchw, 0
  13.       SetWindowText hWnd, "Find Next"
  14.     case 1004 'F4
  15.      FindKeyword hchw, 1
  16.       SetWindowText hWnd, "Find Next Word"
  17.     case 1005 'F5
  18.      SetWindowText hWnd, "Compile"
  19.       s=GetText hw
  20.       Exec s
  21.     case 1008 'F8
  22.      string f
  23.       SetWindowText hWnd, "Load File"
  24.       f=GetFileName fi,0
  25.       if f then
  26.         fi=f
  27.         s=GetFile fi
  28.         le=len s
  29.         SendMessage hchw[0],WM_SETTEXT,le,strptr s
  30.       end if
  31.       SetWindowText hwnd,strptr fi
  32.     case 1009 'F9
  33.      string f
  34.       if fo="" then fo="_.o2bas"
  35.       f=fo
  36.       if GetAsyncKeyState VK_SHIFT
  37.         f=GetFileName fo,1
  38.       end if
  39.       if f then
  40.         fo=f
  41.         s=GetText hchw[0]
  42.         putfile fo,s
  43.         SetWindowText hWnd, "Saved "+fo
  44.       end if
  45.     case 2065 'Ctrl A
  46.      SelectAll hw
  47.     case 2070 'Ctrl F
  48.      SelectAll hchw(1)
  49.     case 2071 'Ctrl G
  50.      SendMessage hw,EM_GETSEL,@a,@b
  51.       a=SendMessage hw,EM_LINEFROMCHAR,a,0
  52.       string sl=str(a+1)
  53.       SendMessage hchw(2),WM_SETTEXT,len(sl),strptr sl
  54.       SelectAll hchw(2)
  55.     case 2013 'VK_ENTER
  56.      sys v,w,h=GetFocus()
  57.       if h=hchw(1)
  58.         FindKeyWord hchw,0
  59.       elseif h=hchw(2)
  60.         string g=nuls 24
  61.         SendMessage h,WM_GETTEXT,24,strptr g
  62.         v=val g
  63.         if v then
  64.           v=SendMessage hw,EM_LINEINDEX,v-1,0
  65.           SetFocus hw
  66.           SendMessage hw,EM_SETSEL,v,v
  67.           SendMessage hw,EM_SCROLLCARET,0,0
  68.         end if
  69.       end if
  70.     end select
  71.  

Charles
« Last Edit: July 06, 2012, 01:27:37 AM by Charles Pegge »