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