home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_09 / 2n09009a < prev    next >
Text File  |  1991-07-26  |  5KB  |  133 lines

  1.  
  2. #include <windows.h>
  3.  
  4. long FAR PASCAL EditWndProc(
  5.     HWND hWnd,          /* window handle */
  6.     unsigned message,   /* message type */
  7.     WORD wParam,        /* additional information */
  8.     LONG lParam         /* additional information */
  9.     );
  10.  
  11. /* define names to use with Get/SetProp */
  12. #define REALEDITLO      MAKEINTRESOURCE(1)
  13. #define REALEDITHI      MAKEINTRESOURCE(2)
  14.  
  15. /* call to setup an edit control - returns TRUE if OK, FALSE if not */
  16. BOOL InitMLE(hDialogWnd, EditId)
  17. HWND hDialogWnd;        /* window handle of edit control */
  18. int EditId;                     /* Id of edit control within dialog box */
  19. {
  20.     extern HANDLE hInst;
  21.     HWND hEditWnd;      /* window handle of the MLE */
  22.     FARPROC lpfnRealEdit;       /* the 'real' edit control's WndProc */
  23.     FARPROC lpfnNewEdit;        /* the new edit control WndProc */
  24.  
  25.     /* find the edit control's window handle */
  26.     hEditWnd = GetDlgItem(hDialogWnd, EditId);
  27.     if (!hEditWnd)
  28.         return(FALSE);
  29.  
  30. /* subclass edit control to make return work properly */
  31. /* store the 'real' WndProc address using properties */
  32. /* use properties because a static variable would only */
  33. /* work for the case of a single edit control in a single */
  34. /* dialog box at one time */
  35.     lpfnRealEdit = (FARPROC) GetWindowLong(hEditWnd, GWL_WNDPROC);
  36.     if (!SetProp(hEditWnd, REALEDITLO, LOWORD(lpfnRealEdit)))
  37.         return(FALSE);
  38.     if (!SetProp(hEditWnd, REALEDITHI, HIWORD(lpfnRealEdit)))
  39.         return(FALSE);
  40.  
  41.     /* make the address of the new one */
  42.     lpfnNewEdit = MakeProcInstance((FARPROC)EditWndProc,hInst);
  43.     if (!lpfnNewEdit)
  44.         return(FALSE);
  45.  
  46.     /* and make it the address of the edit control's WndProc */
  47.     SetWindowLong(hEditWnd, GWL_WNDPROC, (long)lpfnNewEdit);
  48.     /* don't need to store the address of the new proc, cause */
  49.     /* its in the window long! */
  50.     return(TRUE);
  51. }
  52.  
  53. /* subclass the edit box in order to make return and tab act as one */
  54. /* would expect. The editbox in v3 requires that CTRL-RETURN is */
  55. /* pressed to get the "normal" action - BW - 29/5/91 */
  56.  
  57.  
  58. long FAR PASCAL EditWndProc(
  59.     HWND hWnd,          /* window handle */
  60.     unsigned message,   /* message type */
  61.     WORD wParam,        /* additional information */
  62.     LONG lParam         /* additional information */
  63.     )
  64. {
  65.     FARPROC lpfnRealEdit;
  66.     long lret;
  67.     char States[265];
  68. /* it's 265 cause the ref manual says 265 - */
  69. /* but I think its a wee typo and should be 256 */
  70. /* - but then again, there's no harm in being safe */
  71.  
  72. /* get the real WndProc's address from the property list */
  73.     lpfnRealEdit = (FARPROC)MAKELONG(GetProp(hWnd, REALEDITLO),
  74.                      GetProp(hWnd, REALEDITHI));
  75.     switch (message)    {
  76.         /* Catch KEYDOWN message and pretend that Ctrl is pressed */    
  77.         case WM_KEYDOWN:
  78.             if ((wParam == VK_RETURN) || (wParam == VK_TAB))
  79.                 {
  80.                 GetKeyboardState (States);
  81.                 States [VK_CONTROL] ^= 0x80;    /* xor state of <CTRL> key */
  82.                 SetKeyboardState (States);
  83.                 }
  84.             break;
  85.  
  86.         /* we need to trap WM_DESTROY to un-subclass the control */
  87.         case WM_DESTROY:
  88.             {
  89.             FARPROC lpfnNewEdit;
  90.             /* get the address of this WndProc */
  91.             lpfnNewEdit = (FARPROC)GetWindowLong(hWnd, GWL_WNDPROC);
  92.             /* replace it with the 'real' WndProc */
  93.             SetWindowLong(hWnd, GWL_WNDPROC, (long)lpfnRealEdit);
  94.             /* free the procinstance for this WndProc */
  95.             FreeProcInstance(lpfnNewEdit);
  96.             /* and clean up the property list */
  97.             RemoveProp(hWnd, REALEDITLO);
  98.             RemoveProp(hWnd, REALEDITHI);
  99.             break;
  100.             }
  101.         default:
  102.             break;
  103.         }
  104.     /* now call the window proc */
  105.     lret = CallWindowProc(lpfnRealEdit, hWnd, message, wParam, lParam);
  106.     /* now make sure the ctrl is removed */
  107.     switch (message)    {
  108.         /* Clear pretend Ctrl as soon as done
  109.            call for TAB */
  110.         case WM_KEYDOWN:
  111.             if (wParam == VK_TAB)
  112.                 {
  113.                 GetKeyboardState (States);
  114.                 States [VK_CONTROL] ^= 0x80;
  115.                 SetKeyboardState(States);
  116.                 }
  117.             break;
  118.         /* Clear pretend Ctrl when get WM_KEYUP (WM_KEYDOWN
  119.            and WM_KEYUP translate to WM_CHAR) */
  120.         case WM_CHAR:
  121.             if (wParam == VK_RETURN)
  122.                 {
  123.                 GetKeyboardState (States);
  124.                 States [VK_CONTROL] ^= 0x80;
  125.                 SetKeyboardState (States);
  126.                 }
  127.             break;
  128.         default:
  129.             break;
  130.         }
  131.     return(lret);
  132. }
  133.