home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / mleenter.arj / MLEENTER.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  9KB  |  355 lines

  1. /*
  2.  *
  3.  *  Window EditSubclasser App
  4.  *
  5.  *  Microsoft WinSDK Support
  6.  *  Copyright (c) 1991 Microsoft Corporation. All rights reserved.
  7.  *
  8.  */
  9.  
  10. #define NOMINMAX
  11. #include <windows.h>
  12. #include <stdlib.h>
  13. #include "mleenter.h"
  14.  
  15.  
  16. HWND        hgWnd;        //Global Window Handle.  Very useful.
  17. HANDLE      hgInst;       //Global Instance Handle.  Ditto.
  18.  
  19.  
  20. //Variables for the dialog box.
  21. WORD        wEnterMethod;
  22. FARPROC     lpMLEEnterSubProc;
  23. FARPROC     lpEditProcOrg;
  24.  
  25.  
  26.  
  27. /*
  28.  * Array of procedure addresses for all the dialog boxes.  DoDialog
  29.  * makes a ProcInstance when needed from the entry in this array.
  30.  */
  31. FARPROC     rglpDialogs[CDIALOGS]={
  32.                                   MLEEnterDlgProc
  33.                                   };
  34.  
  35.  
  36.  
  37.  
  38. /*
  39.  * WinMain
  40.  *
  41.  * Purpose:
  42.  *  Main entry point of application.   Should register the app class
  43.  *  if a previous instance has not done so and do any other one-time
  44.  *  initializations.
  45.  *
  46.  * Parameters:
  47.  *  See Windows SDK Guide to Programming, page 2-3
  48.  *
  49.  * Return Value:
  50.  *  Value to return to Windows--termination code.
  51.  *
  52.  */
  53.  
  54. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  55.                     LPSTR lpszCmdLine, int nCmdShow)
  56.     {
  57.     WNDCLASS    wndClass;
  58.     HANDLE      hStringMem;
  59.     HBRUSH      hBrushBk;
  60.     HWND        hWnd;
  61.     HDC         hDC;
  62.     MSG         msg;
  63.     short       i;
  64.  
  65.  
  66.     hgInst=hInstance;
  67.  
  68.     if (!hPrevInstance)
  69.         {
  70.         wndClass.style          = CS_HREDRAW | CS_VREDRAW;
  71.         wndClass.lpfnWndProc    = MLEEnterWndProc;
  72.         wndClass.cbClsExtra     = 0;
  73.         wndClass.cbWndExtra     = 0;
  74.         wndClass.hInstance      = hInstance;
  75.         wndClass.hIcon          = LoadIcon(hInstance, "MLEEnterIcon");
  76.         wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
  77.         wndClass.hbrBackground  = COLOR_WINDOW + 1;
  78.         wndClass.lpszMenuName   = "MLEEnterMenu";
  79.         wndClass.lpszClassName  = "MLEEnter";
  80.  
  81.  
  82.         if (!RegisterClass(&wndClass))
  83.             return FALSE;
  84.         }
  85.  
  86.  
  87.     hWnd=CreateWindow("MLEEnter",
  88.                       "Edit Control Enter Key",
  89.                       WS_MINIMIZEBOX | WS_OVERLAPPEDWINDOW,
  90.                       35, 35, 400, 150,
  91.                       NULL, NULL, hInstance, NULL);
  92.  
  93.     hgWnd=hWnd;
  94.  
  95.     ShowWindow(hWnd, nCmdShow);
  96.     UpdateWindow(hWnd);
  97.  
  98.  
  99.     while (GetMessage(&msg, NULL, 0,0 ))
  100.         {
  101.         TranslateMessage(&msg);
  102.         DispatchMessage(&msg);
  103.         }
  104.  
  105.     return msg.wParam;
  106.     }
  107.  
  108.  
  109.  
  110. /*
  111.  * MLEEnterWndProc
  112.  *
  113.  * Purpose:
  114.  *  Window class procedure.  Standard callback.
  115.  *
  116.  * Parameters:
  117.  *  The standard.  See Section 2.4 Windows SDK Guide to Programming,
  118.  *  page 2-4.
  119.  *
  120.  * Return Value:
  121.  *  See Parameters, above.
  122.  *
  123.  */
  124.  
  125.  
  126. long FAR PASCAL MLEEnterWndProc(HWND hWnd, unsigned iMessage,
  127.                             WORD wParam, LONG lParam)
  128.     {
  129.  
  130.     switch (iMessage)
  131.         {
  132.         case WM_DESTROY:
  133.             PostQuitMessage(0);
  134.             break;
  135.  
  136.         case WM_COMMAND:
  137.             switch (wParam)
  138.                 {
  139.                 case IDM_DIALOGMLEENTER:
  140.                     WDoDialog(IDD_MLEENTER);
  141.                     break;
  142.  
  143.                 case IDM_DIALOGEXIT:
  144.                     PostMessage(hWnd, WM_CLOSE, 0, 0L);
  145.                     break;
  146.                 }
  147.  
  148.         default:
  149.             return (DefWindowProc(hWnd, iMessage, wParam, lParam));
  150.         }
  151.  
  152.     return 0L;
  153.     }
  154.  
  155.  
  156.  
  157. /*
  158.  * WDoDialog
  159.  *
  160.  * Purpose:
  161.  *  Handles necessary housekeeping to display a dialog, i.e. making
  162.  *  the ProcInstance and calling DialogBox.  Also processes the return
  163.  *  value in case of errors.
  164.  *
  165.  * Parameters:
  166.  *  wID     WORD indicating the dialog to display.  This should
  167.  *          ONLY be a value defined by IDD_*
  168.  *
  169.  * Return Value:
  170.  *  -1      If the function fails to display the dialog.
  171.  *          Otherwise the value that the dialog passes from EndDialog.
  172.  *
  173.  */
  174.  
  175. WORD PASCAL WDoDialog(WORD wID)
  176.     {
  177.     FARPROC    lpDialogProc;
  178.     WORD       wRet;
  179.  
  180.  
  181.     /*
  182.      * Associate the procedure with this instance's data.  Exit
  183.      * if we can't do it.
  184.      */
  185.     lpDialogProc=MakeProcInstance(rglpDialogs[wID-1], hgInst);
  186.  
  187.     if (lpDialogProc==NULL)
  188.         wRet=-1;
  189.     else
  190.         wRet=DialogBox(hgInst, MAKEINTRESOURCE(wID), hgWnd, lpDialogProc);
  191.  
  192.  
  193.     FreeProcInstance(lpDialogProc);
  194.  
  195.     return wRet;
  196.     }
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206. /*
  207.  * MLEEnterDlgProc
  208.  *
  209.  * Purpose:
  210.  *  Dialog procedure that subclasses the multiline edit control to
  211.  *  detect the caret position within the control.
  212.  *
  213.  * Parameters:
  214.  *  Standard for a window procedure.
  215.  *
  216.  * Return Value:
  217.  *  Standard for dialog procedure.
  218.  *
  219.  */
  220.  
  221. BOOL FAR PASCAL MLEEnterDlgProc(HWND hDlg, WORD iMessage,
  222.                                 WORD wParam, LONG lParam)
  223.     {
  224.     static BOOL fEditFocus;
  225.     static BOOL fEditChange;
  226.     PAINTSTRUCT ps;
  227.     TEXTMETRIC  tm;
  228.     BYTE        rgb[256];
  229.     LONG        lOld;
  230.     HDC         hDC;
  231.     HWND        hEdit;
  232.  
  233.     switch (iMessage)
  234.         {
  235.         case WM_INITDIALOG:
  236.             wEnterMethod=ID_METHODDEF;
  237.             CheckRadioButton(hDlg, ID_METHODDEF, ID_METHODSUB, ID_METHODDEF);
  238.  
  239.             lpMLEEnterSubProc=MakeProcInstance((FARPROC)MLEEnterSubProc, hgInst);
  240.  
  241.             lOld=GetWindowLong(GetDlgItem(hDlg, ID_EDIT), GWL_WNDPROC);
  242.             lpEditProcOrg=(FARPROC)lOld;
  243.             return(TRUE);
  244.  
  245.  
  246.         case DM_GETDEFID:
  247.             /*
  248.              * Do this method only if ID_METHODDEF is selected, the edit control
  249.              * has the focus, and the TAB key is not down.  We check the TAB since
  250.              * we will get DM_GETDEFID when we are tabbed to, in which case
  251.              * we don't want to send a LF.
  252.              */
  253.             if (ID_METHODDEF==wEnterMethod && fEditFocus &&
  254.                 !(GetKeyState(VK_TAB) & 0x8000))
  255.                 {
  256.                 SendDlgItemMessage(hDlg, ID_EDIT, WM_CHAR, 0x0A, 0L);
  257.                 return TRUE;
  258.                 }
  259.             break;
  260.  
  261.  
  262.         case WM_COMMAND:
  263.             switch(wParam)
  264.                 {
  265.                 case ID_METHODSUB:
  266.                     wEnterMethod=wParam;
  267.  
  268.                     //Hook in the subclass procedure.
  269.                     SetWindowLong(GetDlgItem(hDlg, ID_EDIT),
  270.                                   GWL_WNDPROC, (LONG)lpMLEEnterSubProc);
  271.  
  272.                     break;
  273.  
  274.                 case ID_METHODDEF:
  275.                     wEnterMethod=wParam;
  276.  
  277.                     //Unhook the subclass procedure.
  278.                     SetWindowLong(GetDlgItem(hDlg, ID_EDIT),
  279.                                   GWL_WNDPROC, (LONG)lpEditProcOrg);
  280.  
  281.                     break;
  282.  
  283.                 case ID_EDIT:
  284.                     if (EN_KILLFOCUS==HIWORD(lParam))
  285.                         fEditFocus=FALSE;
  286.  
  287.                     if (EN_SETFOCUS==HIWORD(lParam))
  288.                         fEditFocus=TRUE;
  289.  
  290.                     break;
  291.  
  292.  
  293.                 case IDOK:
  294.                     EndDialog(hDlg, TRUE);
  295.                     SetWindowLong(GetDlgItem(hDlg, ID_EDIT),
  296.                                   GWL_WNDPROC, (LONG)lpEditProcOrg);
  297.                     FreeProcInstance(lpMLEEnterSubProc);
  298.                     return TRUE;
  299.  
  300.                 default:
  301.                     break;
  302.                 }
  303.             break;
  304.  
  305.         default:
  306.             break;
  307.         }
  308.  
  309.     return FALSE;
  310.     }
  311.  
  312.  
  313.  
  314.  
  315. /*
  316.  * MLEEnterSubProc
  317.  *
  318.  * Purpose:
  319.  *  Dialog procedure that subclasses the multiline edit control to
  320.  *  detect the caret position within the control.
  321.  *
  322.  * Parameters:
  323.  *  Standard for a window procedure.
  324.  *
  325.  * Return Value:
  326.  *  Standard for dialog procedure.
  327.  *
  328.  */
  329.  
  330. LONG FAR PASCAL MLEEnterSubProc(HWND hWnd, WORD iMessage,
  331.                                 WORD wParam, LONG lParam )
  332.     {
  333.     WORD     iLine;
  334.     WORD     iCol;
  335.     WORD     iSel;
  336.     WORD     iTopLine;
  337.     POINT    pt;
  338.  
  339.     switch (iMessage)
  340.         {
  341.         case WM_KEYDOWN:
  342.             if (ID_METHODSUB==wEnterMethod && VK_RETURN==wParam)
  343.                 {
  344.                 PostMessage(hWnd, WM_CHAR, 0x0A, 0L);
  345.                 return 0L;
  346.                 }
  347.             break;
  348.  
  349.         default:
  350.             break;
  351.         }
  352.  
  353.     return CallWindowProc(lpEditProcOrg, hWnd, iMessage, wParam, lParam);
  354.     }
  355.