|
|
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: | POLYBEZIER MODULE 14: | This module contains the routines for demo 15: \*---------------------------------------------------------------------------*/ 16: 17: #include <windows.h> 18: #include "gdidemo.h" 19: #include "poly.h" 20: 21: /*---------------------------------------------------------------------------*\ 22: | CREATE BEZIER WINDOW PROCEDURE 23: | Create the bezier MDI-child window. 24: \*---------------------------------------------------------------------------*/ 25: HWND FAR CreatePolyWindow(HWND hWndClient, int nItem) 26: { 27: HANDLE hInstance; 28: MDICREATESTRUCT mcs; 29: 30: 31: hInstance = GETINSTANCE(hWndClient); 32: 33: /* 34: ** Initialize the MDI create struct for creation of the 35: ** test window. 36: */ 37: mcs.szClass = POLYCLASS; 38: mcs.szTitle = POLYTITLE; 1.1.1.3 ! root 39: mcs.hOwner = hInstance; 1.1 root 40: mcs.x = CW_USEDEFAULT; 41: mcs.y = CW_USEDEFAULT; 42: mcs.cx = CW_USEDEFAULT; 43: mcs.cy = CW_USEDEFAULT; 1.1.1.3 ! root 44: mcs.style = 0l; 1.1 root 45: mcs.lParam = (LONG)nItem; 46: 1.1.1.3 ! root 47: return((HWND)SendMessage(hWndClient,WM_MDICREATE,0,(LONG)(LPMDICREATESTRUCT)&mcs)); 1.1 root 48: } 49: 50: 51: /*---------------------------------------------------------------------------*\ 52: | POLYBEZIER WINDOW PROCEDURE 53: | This is the main window function for the polybezier demo window. 54: \*---------------------------------------------------------------------------*/ 55: LONG APIENTRY PolyProc(HWND hWnd, UINT wMsg, WPARAM wParam, LONG lParam) 56: { 57: switch(wMsg) 58: { 59: case WM_CREATE: 60: PolyCreateProc(hWnd); 61: break; 62: 63: 64: case WM_COMMAND: 65: PolyCommandProc(hWnd,wParam,lParam); 66: break; 67: 68: case WM_MOVE: 69: PolyRedraw(hWnd); 70: break; 71: 72: 73: case WM_TIMER: 74: PolyDrawBez(hWnd); 75: break; 76: 77: 78: case WM_PAINT: 79: PolyPaintProc(hWnd); 80: break; 81: 82: 83: case WM_DESTROY: 84: PolyDestroyProc(hWnd); 85: break; 86: 87: 88: default: 89: return(DefMDIChildProc(hWnd,wMsg,wParam,lParam)); 90: } 91: return(0l); 92: } 93: 94: 95: /*---------------------------------------------------------------------------*\ 96: | POLYBEZIER CREATE PROCEDURE 97: | Create the polybezier window for the demo application. This is a child 98: | of the MDI client window. Allocate the extra object information for 99: | handling of the polybezier demo. 100: \*---------------------------------------------------------------------------*/ 101: BOOL PolyCreateProc(HWND hWnd) 102: { 103: PPOLYDATA ppd; 104: 105: 106: if(AllocWindowInfo(hWnd,sizeof(POLYDATA))) 107: { 108: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd)) 109: { 110: ppd->nBezTotal = 20; 111: ppd->nBezCurr = 0; 112: ppd->nColor = 0; 113: ppd->hBezBuffer = GlobalAlloc(GHND,(DWORD)(sizeof(BEZBUFFER) * MAX_BEZIER)); 114: 115: UnlockWindowInfo(hWnd); 116: 117: PolyInitPoints(hWnd); 118: 119: SetTimer(hWnd,1,50,NULL); 120: return(TRUE); 121: } 122: FreeWindowInfo(hWnd); 123: } 124: return(FALSE); 125: } 126: 127: 128: /*---------------------------------------------------------------------------*\ 129: | POLYBEZIER COMMAND PROCEDURE 130: | Process polybezier commands. This is a NOP for now. But who knows what 131: | tomorrow may bring. 132: \*---------------------------------------------------------------------------*/ 133: BOOL PolyCommandProc(HWND hWnd, WPARAM wParam, LONG lParam) 134: { 135: hWnd = hWnd; 136: wParam = wParam; 137: lParam = lParam; 138: 139: return(TRUE); 140: } 141: 142: 143: /*---------------------------------------------------------------------------*\ 144: | POLYBEZIER PAINT PROCEDURE 145: | Repaint the bezier window. All we really do here is validate our window, 146: | and reset the array of bezier objects. 147: \*---------------------------------------------------------------------------*/ 148: VOID PolyPaintProc(HWND hWnd) 149: { 150: HDC hDC; 151: PAINTSTRUCT ps; 152: 153: 154: if(hDC = BeginPaint(hWnd,&ps)) 155: EndPaint(hWnd,&ps); 156: 157: PolyRedraw(hWnd); 158: 159: return; 160: } 161: 162: 163: /*---------------------------------------------------------------------------*\ 164: | POLYBEZIER DESTROY PROCEDURE 165: | Kill the polybezier demo. Free up the resources allocated on behalf of 166: | this object. 167: \*---------------------------------------------------------------------------*/ 168: VOID PolyDestroyProc(HWND hWnd) 169: { 170: PPOLYDATA ppd; 171: 172: 173: KillTimer(hWnd,1); 174: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd)) 175: { 176: GlobalFree(ppd->hBezBuffer); 177: UnlockWindowInfo(hWnd); 178: } 179: FreeWindowInfo(hWnd); 180: 181: return; 182: } 183: 184: 185: /*---------------------------------------------------------------------------*\ 186: | GET NEW VELOCITY 187: | This routine creates a new velocity for the bezier points. Each bezier 188: | point is randomly chosen. The two inside points should have a speed 189: | less then the endpoints (most of the time-better effect). 190: \*---------------------------------------------------------------------------*/ 191: int PolyNewVel(int i) 192: { 193: int nRet; 194: 195: 196: if ((i == 1) || (i == 2)) 197: nRet = (int)((lRandom() % VELMAX) / 3) + VELMIN; 198: else 199: nRet = (int)(lRandom() % VELMAX) + VELMIN; 200: 201: return((nRet < 0) ? -nRet : nRet); 202: } 203: 204: 205: /*---------------------------------------------------------------------------*\ 206: | INITIALIZE POLYBEZIER POINTS 207: | This routine initializes the polybezier points for the first object. This 208: | is performed on startup of the window. 209: \*---------------------------------------------------------------------------*/ 210: VOID PolyInitPoints(HWND hWnd) 211: { 212: PPOLYDATA ppd; 213: LPBEZBUFFER lpBez; 214: int idx; 215: RECT rect; 216: 217: 218: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd)) 219: { 220: if(lpBez = (LPBEZBUFFER)GlobalLock(ppd->hBezBuffer)) 221: { 222: GetClientRect(hWnd,&rect); 223: 1.1.1.2 root 224: for(idx=0; idx < BEZ_PTS-1; idx++) 1.1 root 225: { 226: lpBez->pPts[idx].x = lRandom() % rect.right; 227: lpBez->pPts[idx].y = lRandom() % rect.bottom; 228: 229: ppd->pVel[idx].x = PolyNewVel(idx); 230: ppd->pVel[idx].y = PolyNewVel(idx); 231: } 232: GlobalUnlock(ppd->hBezBuffer); 233: } 234: UnlockWindowInfo(hWnd); 235: } 236: return; 237: } 238: 239: 240: /*---------------------------------------------------------------------------*\ 241: | POLYBEZIER REDRAW 242: | This routine resets the bezier curves and redraws the poly-bezier client 243: | area. 244: \*---------------------------------------------------------------------------*/ 245: VOID PolyRedraw(HWND hWnd) 246: { 247: PPOLYDATA ppd; 248: LPBEZBUFFER lpBez,lpCurr; 249: HDC hDC; 250: int i,j; 251: RECT rect; 252: 253: 254: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd)) 255: { 256: if(lpBez = (LPBEZBUFFER)GlobalLock(ppd->hBezBuffer)) 257: { 258: if(hDC = GetDC(hWnd)) 259: { 260: /* 261: ** Save the current bezier. Set the first bezier in the 262: ** array to that curve, and use it as a basis for the next 263: ** series. 264: */ 265: lpCurr = lpBez+ppd->nBezCurr; 266: *lpBez = *lpCurr; 267: ppd->nBezCurr = 0; 268: 269: 270: /* 271: ** Clean the curves (all but the first curve). 272: */ 273: for(j=1; j < ppd->nBezTotal; j++) 274: { 1.1.1.2 root 275: for(i=0; i < BEZ_PTS; i++) 1.1 root 276: { 277: (lpBez+j)->pPts[i].x = -1; 278: (lpBez+j)->pPts[i].y = 0; 279: } 280: } 281: 282: 283: 284: /* 285: ** Clear the display. 286: */ 287: GetClientRect(hWnd,&rect); 288: BitBlt(hDC,0,0,rect.right, rect.bottom,(HDC)0,0,0,0); 289: 290: 291: /* 292: ** Draw the first curve in the bezier array. 293: */ 1.1.1.3 ! root 294: #if defined(_WIN32) && defined(WIN32) 1.1.1.2 root 295: PolyBezier(hDC,lpBez->pPts,BEZ_PTS); 1.1 root 296: #else 1.1.1.2 root 297: Polyline(hDC,lpBez->pPts,BEZ_PTS); 1.1 root 298: #endif 299: ReleaseDC(hWnd,hDC); 300: } 301: GlobalUnlock(ppd->hBezBuffer); 302: } 303: UnlockWindowInfo(hWnd); 304: } 305: return; 306: } 307: 308: 309: 310: 311: VOID PolyDrawBez(HWND hWnd) 312: { 313: PPOLYDATA ppd; 314: LPBEZBUFFER lpBez,lpCurr,lpPrev; 315: int idx,x,y; 316: RECT rect; 317: HDC hDC; 318: HPEN hPen; 319: 320: static COLORREF crColor[] = {0x000000FF,0x0000FF00,0x00FF0000,0x0000FFFF, 321: 0x00FF00FF,0x00FFFF00,0x00FFFFFF,0x00000080, 322: 0x00008000,0x00800000,0x00008080,0x00800080, 323: 0x00808000,0x00808080,0x000000FF,0x0000FF00, 324: 0x00FF0000,0x0000FFFF,0x00FF00FF,0x00FFFF00}; 325: 326: 327: if(ppd = (PPOLYDATA)LockWindowInfo(hWnd)) 328: { 329: if(lpBez = (LPBEZBUFFER)GlobalLock(ppd->hBezBuffer)) 330: { 331: if(hDC = GetDC(hWnd)) 332: { 333: GetClientRect(hWnd,&rect); 334: 335: lpPrev = lpBez+ppd->nBezCurr; 336: 337: ppd->nBezCurr += 1; 338: 339: if(ppd->nBezCurr >= ppd->nBezTotal) 340: { 341: ppd->nBezCurr = 0; 342: ppd->nColor = (++ppd->nColor % 20); 343: } 344: lpCurr = lpBez+ppd->nBezCurr; 345: 346: 347: if(lpCurr->pPts[0].x != -1) 348: { 349: hPen = SelectObject(hDC,GetStockObject(BLACK_PEN)); 1.1.1.3 ! root 350: #if defined(_WIN32) && defined(WIN32) 1.1.1.2 root 351: PolyBezier(hDC,lpCurr->pPts,BEZ_PTS); 1.1 root 352: #else 1.1.1.2 root 353: Polyline(hDC,lpCurr->pPts,BEZ_PTS); 1.1 root 354: #endif 355: SelectObject(hDC,hPen); 356: } 357: 1.1.1.2 root 358: for(idx=0; idx < BEZ_PTS; idx++) 1.1 root 359: { 360: x = lpPrev->pPts[idx].x; 361: y = lpPrev->pPts[idx].y; 362: x += ppd->pVel[idx].x; 363: y += ppd->pVel[idx].y; 364: 365: if(x >= rect.right) 366: { 367: x = rect.right - ((x - rect.right)+1); 368: ppd->pVel[idx].x = -PolyNewVel(idx); 369: } 370: 371: if(x <= rect.left) 372: { 373: x = rect.left + ((rect.left - x)+1); 374: ppd->pVel[idx].x = PolyNewVel(idx); 375: } 376: 377: if(y >= rect.bottom) 378: { 379: y = rect.bottom - ((y - rect.bottom)+1); 380: ppd->pVel[idx].y = -PolyNewVel(idx); 381: } 382: 383: if(y <= rect.top) 384: { 385: y = rect.top + ((rect.top - y)+1); 386: ppd->pVel[idx].y = PolyNewVel(idx); 387: } 388: 389: lpCurr->pPts[idx].x = x; 390: lpCurr->pPts[idx].y = y; 1.1.1.2 root 391: 1.1 root 392: } 393: 394: hPen = SelectObject(hDC,CreatePen(PS_SOLID,1,crColor[ppd->nColor])); 1.1.1.3 ! root 395: #if defined(_WIN32) && defined(WIN32) 1.1.1.2 root 396: PolyBezier(hDC,lpCurr->pPts,BEZ_PTS); 1.1 root 397: #else 1.1.1.2 root 398: Polyline(hDC,lpCurr->pPts,BEZ_PTS); 1.1 root 399: #endif 400: DeleteObject(SelectObject(hDC,hPen)); 401: 1.1.1.3 ! root 402: #if defined(_WIN32) && defined(WIN32) 1.1 root 403: SetROP2(hDC,R2_COPYPEN); 404: #endif 405: ReleaseDC(hWnd,hDC); 406: } 407: GlobalUnlock(ppd->hBezBuffer); 408: } 409: UnlockWindowInfo(hWnd); 410: } 411: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.