home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / w3_prog / glbedit.arj / GENERIC.C next >
C/C++ Source or Header  |  1991-10-09  |  7KB  |  273 lines

  1. /************************************************************************
  2.  *
  3.  *     Project:  Edit control with a private DS
  4.  *
  5.  *     Module:  generic.c
  6.  *
  7.  *     Copyright (c) 1991 Microsoft Corporation. All rights reserved.
  8.  *
  9.  ************************************************************************/
  10.  
  11. #include <windows.h>
  12. #include "generic.h"
  13.  
  14. #define SEGMENT  4096  // size of initial buffer.  Will grow if necessary.
  15.  
  16. HANDLE        hInst;
  17. FARPROC       lpOldListProc, lpOldEditProc;
  18. GLOBALHANDLE  ghEditDS;
  19. HWND          hwndEdit, hwndMain;
  20.  
  21. /************************************************************************
  22.  *  int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  23.  *                     LPSTR lpCmdLine, int nCmdShow)
  24.  ************************************************************************/
  25.  
  26. int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance,
  27.                    LPSTR lpCmdLine, int nCmdShow)
  28. {
  29.    MSG    msg;
  30.    HANDLE hAccel;
  31.  
  32.   if (!hPrevInstance)
  33.       if (!InitApplication(hInstance))
  34.          return FALSE;
  35.  
  36.    if (!InitInstance(hInstance, nCmdShow))
  37.       return FALSE;
  38.  
  39.   hAccel = LoadAccelerators(hInstance, "Accel");
  40.  
  41.   while (GetMessage(&msg, NULL, 0, 0))
  42.      {
  43.      if (TranslateAccelerator(hwndMain, hAccel, &msg) == 0)
  44.         {
  45.         TranslateMessage(&msg);
  46.         DispatchMessage(&msg);
  47.         }
  48.      }
  49.   return msg.wParam;
  50.  
  51. } /* end of WinMain() */
  52.  
  53. /***********************************************************************/
  54.  
  55. BOOL InitApplication(hInstance)
  56. HANDLE hInstance;
  57. {
  58.    WNDCLASS  wc;
  59.  
  60.    wc.style = NULL;
  61.    wc.lpfnWndProc = MainWndProc;
  62.    wc.cbClsExtra = 0;
  63.    wc.cbWndExtra = 0;
  64.    wc.hInstance = hInstance;
  65.    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  66.    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  67.    wc.hbrBackground = COLOR_WINDOW + 1;
  68.    wc.lpszMenuName = "GenericMenu";
  69.    wc.lpszClassName = "GenericWClass";
  70.  
  71.    RegisterClass(&wc);
  72.  
  73.    return TRUE;
  74.  
  75. }  /* end of InitApplication */
  76.  
  77. /************************************************************************/
  78.  
  79. BOOL InitInstance(hInstance, nCmdShow)
  80. HANDLE  hInstance;
  81. int     nCmdShow;
  82. {
  83.    HWND  hWnd;
  84.  
  85.    hInst = hInstance;
  86.  
  87.    hWnd = CreateWindow("GenericWClass", "Generic Sample Application",
  88.                        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
  89.                        CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
  90.                        hInstance, NULL);
  91.    if (!hWnd)
  92.       return FALSE;
  93.  
  94.    ShowWindow(hWnd, nCmdShow);
  95.    UpdateWindow(hWnd);
  96.  
  97.    hwndMain = hWnd;
  98.  
  99.    return TRUE;
  100.  
  101. } /* end of InitInstance */
  102.  
  103. /**************************************************************************/
  104.  
  105. long FAR PASCAL MainWndProc( hWnd, message, wParam, lParam )
  106. HWND      hWnd;
  107. unsigned  message;
  108. WORD      wParam;
  109. LONG      lParam;
  110. {
  111.    FARPROC       lpProcAbout;
  112.    LPVOID        lpPtr;
  113.  
  114.    switch (message)
  115.       {
  116.    case WM_CREATE:
  117.       /*
  118.        *  Allocate space for the new DS.
  119.        */
  120.       ghEditDS = GlobalAlloc(GMEM_DDESHARE | GMEM_MOVEABLE | GMEM_ZEROINIT,
  121.                                  SEGMENT);
  122.       if (ghEditDS == NULL)
  123.          {
  124.          MessageBox(hWnd, "Buffer failed!", "Edit buffer", MB_OK);
  125.          ghEditDS = hInst;
  126.          }
  127.       else
  128.          {
  129.          /*
  130.           *  Initialize the new segment.
  131.           *  Although we unlock the segment, it is still locked by
  132.           *  our global lock.
  133.           */
  134.          lpPtr = GlobalLock(ghEditDS);
  135.          LocalInit(HIWORD((LONG)lpPtr), 0, (WORD)(GlobalSize(ghEditDS) - 16));
  136.          UnlockSegment(HIWORD((LONG)lpPtr));
  137.          }
  138.  
  139.       /*
  140.        *  Create the edit control. Note that the allocated segment
  141.        *  handle is passed in the CreateWindow call as the hInstance
  142.        *  parameter.
  143.        */
  144.       hwndEdit = CreateWindow("edit", NULL,
  145.                  WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL
  146.                  | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL | ES_LEFT,
  147.                  200, 20, 250, 200, hWnd, 2, HIWORD((LONG)lpPtr), NULL);
  148.  
  149.       /*
  150.        *  The EM_LIMITTEXT function will allow the edit field to
  151.        *  hold more than 32K.  The default for an edit field is
  152.        *  approximately 32K, but by sending a 0, 0L through as
  153.        *  the parameters, the default then becomes all available
  154.        *  memory or 64K which comes first.
  155.        */
  156.       SendMessage(hwndEdit, EM_LIMITTEXT, 0, 0L);
  157.  
  158.       return 0;
  159.  
  160.    case WM_SETFOCUS:
  161.       SetFocus(hwndEdit);
  162.       return 0;
  163.  
  164.    case WM_INITMENU:
  165.       /*
  166.        *  Check to see if Undo buffer is available.
  167.        *  If so, activate menu item; otherwise, gray
  168.        *  and disable it.
  169.        */
  170.       if (SendMessage(hwndEdit, EM_CANUNDO, 0, 0L))
  171.          EnableMenuItem(GetMenu(hWnd), IDM_UNDO, MF_ENABLED);
  172.       else
  173.          EnableMenuItem(GetMenu(hWnd), IDM_UNDO, MF_DISABLED | MF_GRAYED);
  174.       break;
  175.  
  176.    case WM_COMMAND:
  177.       switch (wParam)
  178.          {
  179.       case IDM_ABOUT:
  180.          lpProcAbout = MakeProcInstance(AboutDlgProc, hInst);
  181.          DialogBox(hInst, "AboutBox", hWnd, lpProcAbout);
  182.          FreeProcInstance(lpProcAbout);
  183.          break;
  184.  
  185.       case IDM_COPY:
  186.          /*
  187.           *  Copy highlighted contents to clipboard.
  188.           */
  189.          SendMessage(hwndEdit, WM_COPY, 0, 0L);
  190.          break;
  191.  
  192.       case IDM_CUT:
  193.          /*
  194.           *  Cut highlighted contents to clipboard.
  195.           */
  196.          SendMessage(hwndEdit, EM_EMPTYUNDOBUFFER, 0, 0L);
  197.          SendMessage(hwndEdit, WM_CUT, 0, 0L);
  198.          break;
  199.  
  200.       case IDM_PASTE:
  201.          /*
  202.           *  Paste from the clipboard to the edit field.
  203.           */
  204.          SendMessage(hwndEdit, WM_PASTE, 0, 0L);
  205.          break;
  206.  
  207.       case IDM_UNDO:
  208.          /*
  209.           *  Undo previous editing command.  Note that the
  210.           *  undo buffer is allocated by USER, and does not
  211.           *  come out of the buffer this application created.
  212.           */
  213.          SendMessage(hwndEdit, EM_UNDO, 0, 0L);
  214.          break;
  215.  
  216.       case IDM_CLEAR:
  217.          /*
  218.           *  Delete highlighted items.
  219.           */
  220.          SendMessage(hwndEdit, EM_EMPTYUNDOBUFFER, 0, 0L);
  221.          SendMessage(hwndEdit, WM_CLEAR, 0, 0L);
  222.          break;
  223.  
  224.       case IDM_SELECTALL:
  225.          /*
  226.           *  Select every character in the edit control.
  227.           */
  228.          SendMessage(hwndEdit, EM_SETSEL, 0, 65534);
  229.          break;
  230.  
  231.       default:
  232.          return DefWindowProc(hWnd, message, wParam, lParam);
  233.          }
  234.       break;
  235.  
  236.    case WM_DESTROY:
  237.       PostQuitMessage(0);
  238.       break;
  239.  
  240.    default:
  241.       return DefWindowProc(hWnd, message, wParam, lParam);
  242.       }
  243.    return NULL;
  244.  
  245. }  /* end of MainWndProc */
  246.  
  247. /*************************************************************************/
  248.  
  249. BOOL FAR PASCAL AboutDlgProc(hDlg, message, wParam, lParam)
  250. HWND      hDlg;
  251. unsigned  message;
  252. WORD      wParam;
  253. LONG      lParam;
  254. {
  255.    switch (message)
  256.       {
  257.    case WM_INITDIALOG:
  258.       return TRUE;
  259.  
  260.    case WM_COMMAND:
  261.       if (wParam == IDOK || wParam == IDCANCEL)
  262.          {
  263.          EndDialog(hDlg, TRUE);
  264.          return TRUE;
  265.          }
  266.       break;
  267.       }
  268.  
  269.    return FALSE;
  270. } /* end of AboutDlgProc */
  271.  
  272. /* End of File: generic.c */
  273.