home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / caret / crcaret.c < prev    next >
C/C++ Source or Header  |  1988-08-10  |  5KB  |  162 lines

  1. /*
  2.  *  This program will demonstrate the use of the CreateCaret function.
  3.  *  It will define a new shape for the system caret and give ownership
  4.  *  of the system caret to the current window.
  5.  *
  6.  */
  7.  
  8. #include <windows.h>
  9. #include "crcaret.h"
  10.  
  11. HANDLE hInst;
  12.  
  13. int     PASCAL WinMain (hInstance, hPrevInstance, lpCmdLine, nCmdShow)
  14. HANDLE  hInstance;
  15. HANDLE  hPrevInstance;
  16. LPSTR   lpCmdLine;
  17. int     nCmdShow;
  18.   {
  19.   HWND hWnd;    /* window handle */
  20.   MSG msg;      /* message */
  21.   HMENU hMenu;    /* handle to the menu */
  22.  
  23.   if (!hPrevInstance)   /* app already initialized? */
  24.     if (!CrCaretInit (hInstance))        /* nope, init it */
  25.       return (NULL);             /* couldn't init */
  26.  
  27.   hInst = hInstance;        /* store the current instance */
  28.  
  29.   hWnd = CreateWindow ("CrCaret",  /* window class */
  30.                       "CreateCaret Sample Application", /* window name */
  31.                       WS_OVERLAPPEDWINDOW,              /* window style */
  32.                       CW_USEDEFAULT,                    /* x position */
  33.                       CW_USEDEFAULT,                    /* y position */
  34.                       CW_USEDEFAULT,                    /* width */
  35.                       CW_USEDEFAULT,                    /* height */
  36.                       NULL,                             /* parent handle */
  37.                       NULL,                             /* menu or child */
  38.                       hInstance,                        /* instance */
  39.                       NULL);                            /* additional info */
  40.  
  41.   if (!hWnd)
  42.     return (NULL);
  43.  
  44.   ShowWindow (hWnd, nCmdShow);
  45.   UpdateWindow (hWnd);
  46.  
  47.   while (GetMessage (&msg, NULL, NULL, NULL))
  48.     {
  49.     TranslateMessage (&msg);
  50.     DispatchMessage (&msg);
  51.     }
  52.   return (msg.wParam);
  53.   }
  54.  
  55. /* register the window */
  56. BOOL CrCaretInit (hInstance)
  57. HANDLE hInstance; /* current instance */
  58.   {
  59.   HANDLE hMemory;
  60.   PWNDCLASS pWndClass;
  61.   BOOL bSuccess;
  62.  
  63.   hMemory = LocalAlloc (LPTR, sizeof (WNDCLASS));
  64.   pWndClass = (PWNDCLASS) LocalLock (hMemory);
  65.  
  66.   pWndClass->lpszClassName = (LPSTR)  "CrCaret";
  67.   pWndClass->hInstance = hInstance;
  68.   pWndClass->lpfnWndProc = CrCaretWndProc;
  69.   pWndClass->style = NULL;
  70.   pWndClass->hbrBackground = GetStockObject (WHITE_BRUSH);
  71.   pWndClass->hCursor = LoadCursor (NULL, IDC_ARROW);
  72.   pWndClass->hIcon = LoadIcon (NULL, IDI_APPLICATION);
  73.   pWndClass->lpszMenuName = (LPSTR) NULL;
  74.  
  75.   bSuccess = RegisterClass (pWndClass);
  76.  
  77.   LocalUnlock (hMemory);
  78.   LocalFree (hMemory);
  79.  
  80.   return (bSuccess);
  81.   }
  82.  
  83.  
  84. /* process messages to the window */
  85.  
  86. long    FAR PASCAL CrCaretWndProc (hWnd, message, wParam, lParam)
  87. HWND hWnd;                /* window handle */
  88. unsigned    message;            /* type of message */
  89. WORD wParam;                /* additional information */
  90. LONG lParam;                /* additional information */
  91.   {
  92.   FARPROC lpProcAbout;        /* pointer to "About" procedure */
  93.   HMENU hMenu;            /* menu handle */
  94.  
  95.   switch (message)
  96.     {
  97.     case WM_CREATE :
  98.           /* add command to system menu */
  99.       hMenu = GetSystemMenu (hWnd, FALSE);
  100.       ChangeMenu (hMenu, NULL, NULL, NULL, MF_APPEND | MF_SEPARATOR);
  101.       ChangeMenu (hMenu, NULL, "A&bout CreateCaret...",
  102.                   ID_ABOUT, MF_APPEND | MF_STRING);
  103.       break;
  104.  
  105.     case WM_SYSCOMMAND :
  106.       switch (wParam)
  107.         {
  108.         case ID_ABOUT:
  109.           lpProcAbout = MakeProcInstance (About, hInst);
  110.           DialogBox (hInst, "AboutBox", hWnd, lpProcAbout);
  111.           FreeProcInstance (lpProcAbout);
  112.           break;
  113.  
  114.         default:
  115.           return (DefWindowProc (hWnd, message, wParam, lParam));
  116.         }
  117.       break;
  118.  
  119.     case WM_SETFOCUS:             /* create and show caret */
  120.       CreateCaret (hWnd, NULL, 0, 5);
  121.       SetCaretPos (20, 20);
  122.       ShowCaret (hWnd);
  123.       break;
  124.  
  125.     case WM_KILLFOCUS:            /* destroy the caret */
  126.       DestroyCaret ();
  127.       break;
  128.  
  129.     case WM_DESTROY:              /* quit application */
  130.       PostQuitMessage (NULL);      /* notify windows */
  131.       break;
  132.  
  133.     default:                      /* pass it on if unprocessed */
  134.       return (DefWindowProc (hWnd, message, wParam, lParam));
  135.     }
  136.   return (NULL);
  137.   }
  138.  
  139.  
  140. /* this function handles the "About" box */
  141.  
  142. BOOL FAR PASCAL About (hDlg, message, wParam, lParam)
  143. HWND hDlg;
  144. unsigned    message;            /* type of message */
  145. WORD wParam;                /* additional information */
  146. LONG lParam;                /* additional information */
  147.   {
  148.   switch (message)
  149.     {
  150.     case WM_INITDIALOG:           /* init dialog box */
  151.       return (TRUE);               /* don't need to do anything */
  152.     case WM_COMMAND:              /* received a command */
  153.       if (wParam == IDOK)         /* OK box selected? */
  154.         {
  155.         EndDialog (hDlg, NULL);           /* then exit */
  156.         return (TRUE);
  157.         }
  158.       break;
  159.     }
  160.   return (FALSE);                        /* didn't process a message */
  161.   }
  162.