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