DEMO.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:9k
源码类别:

Windows编程

开发平台:

Visual C++

  1. /******************************************************************************
  2. *       This is a part of the Microsoft Source Code Samples. 
  3. *       Copyright (C) 1993-1997 Microsoft Corporation.
  4. *       All rights reserved. 
  5. *       This source code is only intended as a supplement to 
  6. *       Microsoft Development Tools and/or WinHelp documentation.
  7. *       See these sources for detailed information regarding the 
  8. *       Microsoft samples programs.
  9. ******************************************************************************/
  10. /****************************************************************************
  11.     PROGRAM: Demo.c
  12.     PURPOSE: Demonstrates how to manipulate a cursor and select a region
  13.     FUNCTIONS:
  14.         WinMain() - calls initialization function, processes message loop
  15.         DemoInit() - initializes window data and registers window
  16.         DemoWndProc() - processes messages
  17.         About() - processes messages for "About" dialog box
  18.     COMMENTS:
  19.         This code is a modified version of the CURSOR.C program.  Instead of
  20.         using inline code for drawing the shape, the routines from the Select
  21.         library are called.
  22. ****************************************************************************/
  23. #include "windows.h"
  24. #include "demo.h"
  25. #include "select.h"
  26. HANDLE hInst;
  27. BOOL bTrack = FALSE;
  28. INT OrgX = 0, OrgY = 0;
  29. INT PrevX = 0, PrevY = 0;
  30. INT X = 0, Y = 0;
  31. RECT Rect;
  32. INT Shape = SL_BLOCK;                          /* Shape to use for rectangle */
  33. BOOL RetainShape = FALSE;                      /* Retain or destroy shape    */
  34. int APIENTRY WinMain(HINSTANCE hInstance,
  35.                      HINSTANCE hPrevInstance,
  36.                      LPSTR lpCmdLine,
  37.                      int nCmdShow)
  38. {
  39.     HWND hWnd;
  40.     MSG msg;
  41.     CHAR szAppName[64];
  42.     UNREFERENCED_PARAMETER(lpCmdLine);
  43.     if (!hPrevInstance)
  44.         if (!DemoInit(hInstance))
  45.             return (0);
  46.     hInst = hInstance;
  47.     LoadString(hInstance, IDS_APPNAME, szAppName, sizeof(szAppName));
  48.     hWnd = CreateWindow("Demo",
  49.         szAppName,
  50.         WS_OVERLAPPEDWINDOW,
  51.         CW_USEDEFAULT,
  52.         CW_USEDEFAULT,
  53.         CW_USEDEFAULT,
  54.         CW_USEDEFAULT,
  55.         NULL,
  56.         NULL,
  57.         hInstance,
  58.         NULL);
  59.     if (!hWnd)
  60.         return (0);
  61.     ShowWindow(hWnd, nCmdShow);
  62.     UpdateWindow(hWnd);
  63.     while (GetMessage(&msg, NULL, 0, 0)) {
  64.         TranslateMessage(&msg);
  65.         DispatchMessage(&msg);
  66.     }
  67.     return (msg.wParam);
  68. }
  69. /****************************************************************************
  70.     FUNCTION: DemoInit(HANDLE)
  71.     PURPOSE: Initializes window data and registers window class
  72. ****************************************************************************/
  73. BOOL DemoInit(HANDLE hInstance)
  74. {
  75.     HANDLE hMemory;
  76.     PWNDCLASS pWndClass;
  77.     BOOL bSuccess;
  78.     CHAR lpBuffer[128];
  79.     hMemory = LocalAlloc(LPTR, sizeof(WNDCLASS));
  80.     if(!hMemory){
  81.         LoadString(hInst, IDS_NOMEM, lpBuffer, sizeof(lpBuffer));
  82.         MessageBox(NULL, lpBuffer, NULL, MB_OK | MB_ICONHAND);
  83.         return(FALSE);
  84.     }
  85.     pWndClass = (PWNDCLASS) LocalLock(hMemory);
  86.     pWndClass->hCursor = LoadCursor(NULL, IDC_ARROW);
  87.     pWndClass->hIcon = LoadIcon(NULL, IDI_APPLICATION);
  88.     pWndClass->lpszMenuName = (LPSTR) "Menu";
  89.     pWndClass->lpszClassName = (LPSTR) "Demo";
  90.     pWndClass->hbrBackground = GetStockObject(WHITE_BRUSH);
  91.     pWndClass->hInstance = hInstance;
  92.     pWndClass->style = 0;
  93.     pWndClass->lpfnWndProc = (WNDPROC)DemoWndProc;
  94.     bSuccess = RegisterClass(pWndClass);
  95.     LocalUnlock(hMemory);
  96.     LocalFree(hMemory);
  97.     return (bSuccess);
  98. }
  99. /****************************************************************************
  100.     FUNCTION: DemoWndProc(HWND, unsigned, WORD, LONG)
  101.     PURPOSE:  Processes messages
  102.     MESSAGES:
  103.         WM_SYSCOMMAND - system menu (About dialog box)
  104.         WM_CREATE     - create window
  105.         WM_DESTROY    - destroy window
  106.         WM_LBUTTONDOWN - left mouse button
  107.         WM_MOUSEMOVE   - mouse movement
  108.         WM_LBUTTONUP   - left button released
  109.         WM_COMMAND messages:
  110.             IDM_BOX    - use inverted box for selecting a region
  111.             IDM_BLOCK  - use empty box for selecting a region
  112.             IDM_RETAIN - retain/delete selection on button release
  113.     COMMENTS:
  114.         When the left mouse button is pressed, btrack is set to TRUE so that
  115.         the code for WM_MOUSEMOVE will keep track of the mouse and update the
  116.         box accordingly.  Once the button is released, btrack is set to
  117.         FALSE, and the current position is saved.  Holding the SHIFT key
  118.         while pressing the left button will extend the current box rather
  119.         then erasing it and starting a new one.  The exception is when the
  120.         retain shape option is enabled.  With this option, the rectangle is
  121.         zeroed whenever the mouse is released so that it can not be erased or
  122.         extended.
  123. ****************************************************************************/
  124. LONG APIENTRY DemoWndProc(
  125.     HWND hWnd,
  126.     UINT message,
  127.     UINT wParam,
  128.     LONG lParam)
  129. {
  130.     FARPROC lpProcAbout;
  131.     HMENU hMenu;
  132.     switch (message) {
  133.         case WM_COMMAND:
  134.             // LOWORD added for portability
  135.             switch (LOWORD(wParam)) {
  136.                 case IDM_BOX:
  137.                     Shape = SL_BOX;
  138.                     hMenu = GetMenu(hWnd);
  139.                     CheckMenuItem(hMenu, IDM_BOX, MF_CHECKED);
  140.                     CheckMenuItem(hMenu, IDM_BLOCK, MF_UNCHECKED);
  141.                     break;
  142.                 case IDM_BLOCK:
  143.                     Shape = SL_BLOCK;
  144.                     hMenu = GetMenu(hWnd);
  145.                     CheckMenuItem(hMenu, IDM_BOX, MF_UNCHECKED);
  146.                     CheckMenuItem(hMenu, IDM_BLOCK, MF_CHECKED);
  147.                     break;
  148.                 case IDM_RETAIN:
  149.                     if (RetainShape) {
  150.                         hMenu = GetMenu(hWnd);
  151.                         CheckMenuItem(hMenu, IDM_RETAIN, MF_UNCHECKED);
  152.                         RetainShape = FALSE;
  153.                     }
  154.                     else {
  155.                         hMenu = GetMenu(hWnd);
  156.                         CheckMenuItem(hMenu, IDM_RETAIN, MF_CHECKED);
  157.                         RetainShape = TRUE;
  158.                     }
  159.                     break;
  160.                 case IDM_ABOUT:
  161.                     lpProcAbout = MakeProcInstance((FARPROC)About, hInst);
  162.                     DialogBox(hInst, "AboutBox", hWnd, (DLGPROC)lpProcAbout);
  163.                     FreeProcInstance(lpProcAbout);
  164.                     break;
  165.             }
  166.             break;
  167.         case WM_LBUTTONDOWN:
  168.             bTrack = TRUE;               /* user has pressed the left button */
  169.             /* If you don't want the shape cleared, you must clear the Rect
  170.              * coordinates before calling StartSelection
  171.              */
  172.             if (RetainShape)
  173.                 SetRectEmpty(&Rect);
  174.             StartSelection(hWnd, MAKEMPOINT(lParam), &Rect,
  175.                 (wParam & MK_SHIFT) ? SL_EXTEND | Shape : Shape);
  176.             break;
  177.         case WM_MOUSEMOVE:
  178.             if (bTrack)
  179.                 UpdateSelection(hWnd, MAKEMPOINT(lParam), &Rect, Shape);
  180.             break;
  181.         case WM_LBUTTONUP:
  182.        if (bTrack) 
  183.                EndSelection(MAKEMPOINT(lParam), &Rect);
  184.          bTrack = FALSE;
  185.             break;
  186.    case WM_SIZE:
  187.       switch (wParam) {
  188.          case SIZEICONIC:
  189.             /* If we aren't in retain mode we want to clear the 
  190.              * current rectangle now! 
  191.              */
  192.             if (!RetainShape)
  193.                SetRectEmpty(&Rect);
  194.       }
  195.       break;
  196.         case WM_DESTROY:
  197.             PostQuitMessage(0);
  198.             break;
  199.         default:
  200.             return (DefWindowProc(hWnd, message, wParam, lParam));
  201.     }
  202.     return (0);
  203. }
  204. /****************************************************************************
  205.     FUNCTION: About(HWND, unsigned, WORD, LONG)
  206.     PURPOSE:  Processes messages for "About" dialog box
  207.     MESSAGES:
  208.         WM_INITDIALOG - initialize dialog box
  209.         WM_COMMAND    - Input received
  210. ****************************************************************************/
  211. BOOL APIENTRY About(
  212.     HWND hDlg,
  213.     UINT message,
  214.     UINT wParam,
  215.     LONG lParam)
  216. {
  217.     switch (message) {
  218.         case WM_INITDIALOG:
  219.             return (TRUE);
  220.         case WM_COMMAND:
  221.             // LOWORD added for portability
  222.             if (LOWORD(wParam) == IDOK
  223.              || LOWORD(wParam) == IDCANCEL) {
  224.                 EndDialog(hDlg, TRUE);
  225.                 return (TRUE);
  226.             }
  227.             return (TRUE);
  228.     }
  229.     return (FALSE);
  230.         UNREFERENCED_PARAMETER(lParam);
  231. }