|
|
1.1 ! root 1: /************************************************************************\ ! 2: * ! 3: * MODULE: SPINCUBE.C ! 4: * ! 5: * PURPOSE: Demonstrates how to create a custom control library which ! 6: * may be used by applications and the Dialog Editor. ! 7: * ! 8: * FUNCTIONS: DLLEntryPoint() - Registers spincube class when a ! 9: * process loads this DLL. ! 10: * CustomControlInfoA() - Called by DLGEDIT to initialize ! 11: * a CCINFO structure(s). ! 12: * SpincubeStyle() - Brings up dialog box which allows ! 13: * user to modify control style. ! 14: * SpincubeSizeToText() - Called by DLGEDIT if user requests ! 15: * that control be sized to fit text. ! 16: * SpincubeWndProc() - Window procedure for spincube ! 17: * control. ! 18: * SpincubeDlgProc() - Procedure for control style dialog. ! 19: * ! 20: * COMMMENTS: The dialog editor interface has changed since Win 3.0. ! 21: * I recommend browsing the NT CUSTCNTL.H file to get an ! 22: * idea of the new interface. ! 23: * ! 24: \************************************************************************/ ! 25: ! 26: #include <windows.h> ! 27: #include "spincube.h" ! 28: ! 29: void Paint (HWND); /* function prototype for Paint() in PAINT.C */ ! 30: ! 31: ! 32: ! 33: /************************************************************************\ ! 34: * ! 35: * FUNCTION: DLLEntryPoint ! 36: * ! 37: * INPUTS: hDLL - DLL module handle ! 38: * dwReason - reason being called (e.g. process attaching) ! 39: * lpReserved - reserved ! 40: * ! 41: * RETURNS: (Only used when dwReason == DLL_PROCESS_ATTACH) ! 42: * TRUE if initialization passed, or ! 43: * FALSE if initialization failed. ! 44: * ! 45: * GLOBAL VARS: hMod - DLL module handle ! 46: * ! 47: * COMMENTS: On DLL_PROCESS_ATTACH registers the SPINCUBECLASS ! 48: * ! 49: \************************************************************************/ ! 50: ! 51: BOOL DLLEntryPoint (HANDLE hDLL, DWORD dwReason, LPVOID lpReserved) ! 52: { ! 53: hMod = hDLL; ! 54: switch (dwReason) ! 55: { ! 56: case DLL_PROCESS_ATTACH: ! 57: { ! 58: WNDCLASS wc; ! 59: ! 60: wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_CLASSDC | ! 61: CS_GLOBALCLASS ; ! 62: wc.lpfnWndProc = (WNDPROC) SpincubeWndProc; ! 63: wc.cbClsExtra = 0; ! 64: wc.cbWndExtra = SPINCUBE_EXTRA; ! 65: wc.hInstance = hDLL; ! 66: wc.hIcon = NULL; ! 67: wc.hCursor = LoadCursor (NULL, IDC_ARROW); ! 68: wc.hbrBackground = NULL; ! 69: wc.lpszMenuName = (LPSTR) NULL; ! 70: wc.lpszClassName = (LPSTR) SPINCUBECLASS; ! 71: ! 72: if (!RegisterClass (&wc)) ! 73: { ! 74: MessageBox (NULL, "DLLEntryPoint(): RegisterClass() failed", ! 75: "Err! - SPINCUBE.DLL", MB_OK | MB_ICONEXCLAMATION); ! 76: return FALSE; ! 77: } ! 78: break; ! 79: } ! 80: default: ! 81: break; ! 82: } ! 83: return TRUE; ! 84: } ! 85: ! 86: ! 87: ! 88: /************************************************************************\ ! 89: * ! 90: * FUNCTION: CustomControlInfoA ! 91: * ! 92: * INPUTS: acci - pointer to an array od CCINFOA structures ! 93: * ! 94: * RETURNS: Number of controls supported by this DLL ! 95: * ! 96: * COMMENTS: See CUSTCNTL.H for more info ! 97: * ! 98: \************************************************************************/ ! 99: ! 100: UINT CALLBACK CustomControlInfoA (LPCCINFOA acci) ! 101: { ! 102: if (!acci) /* dlgedit is querying the number of controls this DLL */ ! 103: return 1; /* supports, so return 1. Then we'll get called */ ! 104: /* again with a valid "acci" */ ! 105: ! 106: acci[0].flOptions = 0; ! 107: acci[0].cxDefault = 40; /* default width (dialog units) */ ! 108: acci[0].cyDefault = 40; /* default height (dialog units) */ ! 109: acci[0].flStyleDefault = WS_CHILD | ! 110: WS_VISIBLE; ! 111: acci[0].flExtStyleDefault = 0; ! 112: acci[0].flCtrlTypeMask = 0; ! 113: acci[0].cStyleFlags = 1; /* entries in following style table */ ! 114: ! 115: if (!(acci[0].aStyleFlags = (LPCCSTYLEFLAG) ! 116: LocalAlloc (LPTR, sizeof(CCSTYLEFLAG) + ! 117: sizeof(STYLEDEFSTR) + 1))) ! 118: return 0; ! 119: ! 120: acci[0].aStyleFlags[0].flStyle = 0; ! 121: acci[0].aStyleFlags[0].flStyleMask = 0; ! 122: acci[0].aStyleFlags[0].pszStyle = (LPSTR) &acci[1]; ! 123: acci[0].lpfnStyle = SpincubeStyle; ! 124: acci[0].lpfnSizeToText = SpincubeSizeToText; ! 125: acci[0].dwReserved1 = 0; ! 126: acci[0].dwReserved2 = 0; ! 127: ! 128: lstrcpy (acci[0].szClass, SPINCUBECLASS); ! 129: lstrcpy (acci[0].szDesc, SPINCUBEDESCRIPTION); ! 130: lstrcpy (acci[0].szTextDefault, SPINCUBEDEFTEXT); ! 131: lstrcpy ((char *)&acci[1], STYLEDEFSTR); ! 132: ! 133: return 1; /* return the number of controls that the DLL supports */ ! 134: } ! 135: ! 136: ! 137: ! 138: /************************************************************************\ ! 139: * ! 140: * FUNCTION: SpincubeStyle ! 141: * ! 142: * INPUTS: hWndParent - handle of parent window (dialog editor) ! 143: * pccs - pointer to a CCSTYLE structure ! 144: * ! 145: * RETURNS: TRUE if success, ! 146: * FALSE if error occured ! 147: * ! 148: * GLOBAL VARS: gpccs - global pointer to a CCSTYLE structure ! 149: * ! 150: * LOCAL VARS: rc - return code from DialogBox ! 151: * ! 152: \************************************************************************/ ! 153: ! 154: BOOL CALLBACK SpincubeStyle (HWND hWndParent, LPCCSTYLE pccs) ! 155: { ! 156: int rc; ! 157: ! 158: gpccs = pccs; ! 159: ! 160: if ((rc = DialogBox (hMod, "SpincubeStyle", hWndParent, ! 161: (DLGPROC)SpincubeDlgProc)) == -1) ! 162: { ! 163: MessageBox (hWndParent, "SpincubeStyle(): DialogBox failed", ! 164: "Err!- Spincube.dll", ! 165: MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL); ! 166: rc = 0; ! 167: } ! 168: return (BOOL) rc; ! 169: } ! 170: ! 171: ! 172: ! 173: /************************************************************************\ ! 174: * ! 175: * FUNCTION: SpincubeSizeToText ! 176: * ! 177: * INPUTS: flStyle - control style ! 178: * flExtStyle - control extended style ! 179: * hFont - handle of font used to draw text ! 180: * pszText - control text ! 181: * ! 182: * RETURNS: Width (in pixels) control must be to accomodate text, or ! 183: * -1 if an error occurs. ! 184: * ! 185: * COMMENTS: Just no-op here ! 186: * ! 187: \************************************************************************/ ! 188: ! 189: INT CALLBACK SpincubeSizeToText (DWORD flStyle, DWORD flExtStyle, ! 190: HFONT hFont, LPSTR pszText) ! 191: { ! 192: return -1; ! 193: } ! 194: ! 195: ! 196: ! 197: ! 198: /************************************************************************\ ! 199: * ! 200: * FUNCTION: SpincubeWndProc (standard window procedure INPUTS/RETURNS) ! 201: * ! 202: * COMMENTS: This is the window procedure for our custom control. At ! 203: * creation we alloc a SPINCUBEINFO struct, initialize it, ! 204: * and associate it with this particular control. We also ! 205: * start a timer which will invalidate the window every so ! 206: * often; this causes a repaint, and the cube gets drawn in ! 207: * a new position. Left button clicks will toggle the motion ! 208: * state of the control (and turn the timer on/off). ! 209: * ! 210: \************************************************************************/ ! 211: ! 212: LONG SpincubeWndProc (HWND hWnd, UINT msg, UINT wParam, LONG lParam) ! 213: { ! 214: switch (msg) ! 215: { ! 216: case WM_CREATE: ! 217: { ! 218: /******************************************************************\ ! 219: * Alloc & init a SPINCUBEINFO struct for this particular control ! 220: \******************************************************************/ ! 221: ! 222: HANDLE hSCI = LocalAlloc (LHND, sizeof(SPINCUBEINFO)); ! 223: PSPINCUBEINFO pSCI = (PSPINCUBEINFO) LocalLock (hSCI); ! 224: pSCI->xRotAngle = (float) 0.0; ! 225: pSCI->xRotAngle = (float) 0.0; ! 226: pSCI->xRotAngle = (float) 0.0; ! 227: pSCI->inMotion = TRUE; ! 228: LocalUnlock (hSCI); ! 229: SetWindowLong (hWnd, GWL_SPINCUBEDATA, (LONG) hSCI); ! 230: ! 231: SetTimer (hWnd, SPIN_EVENT, SPIN_INTERVAL, NULL); ! 232: break; ! 233: } ! 234: ! 235: case WM_PAINT: ! 236: ! 237: Paint (hWnd); ! 238: break; ! 239: ! 240: case WM_TIMER: ! 241: ! 242: switch (wParam) ! 243: { ! 244: case SPIN_EVENT: ! 245: InvalidateRect (hWnd, NULL, FALSE); ! 246: break; ! 247: } ! 248: break; ! 249: ! 250: case WM_LBUTTONDOWN: ! 251: { ! 252: /******************************************************************\ ! 253: * Change the inMotion state of the control ! 254: \******************************************************************/ ! 255: ! 256: HANDLE hSCI = (HANDLE) GetWindowLong (hWnd, GWL_SPINCUBEDATA); ! 257: PSPINCUBEINFO pSCI = (PSPINCUBEINFO) LocalLock (hSCI); ! 258: if (pSCI->inMotion) ! 259: { ! 260: KillTimer (hWnd, SPIN_EVENT); ! 261: pSCI->inMotion = FALSE; ! 262: } ! 263: else ! 264: { ! 265: SetTimer (hWnd, SPIN_EVENT, SPIN_INTERVAL, NULL); ! 266: pSCI->inMotion = TRUE; ! 267: } ! 268: LocalUnlock (hSCI); ! 269: ! 270: break; ! 271: } ! 272: case WM_DESTROY: ! 273: { ! 274: HANDLE hSCI = (HANDLE) GetWindowLong (hWnd, GWL_SPINCUBEDATA); ! 275: LocalFree (hSCI); ! 276: break; ! 277: } ! 278: default: ! 279: ! 280: return (DefWindowProc(hWnd, msg, wParam, lParam)); ! 281: } ! 282: return ((LONG) TRUE); ! 283: } ! 284: ! 285: ! 286: ! 287: /************************************************************************\ ! 288: * ! 289: * FUNCTION: SpincubeDlgProc (standard dialog procedure INPUTS/RETURNS) ! 290: * ! 291: * GLOBAL VARS: gpccs - pointer to a CCSTYLE structure ! 292: * ! 293: * COMMENTS: The dialog comes up in response to a user requesting to ! 294: * modify the control style. This sample allows for changing ! 295: * the control's text, and this is done by modifying the ! 296: * CCSTYLE structure pointed at by "gpccs" (a pointer ! 297: * that was passed to us by dlgedit). ! 298: * ! 299: \************************************************************************/ ! 300: ! 301: BOOL SpincubeDlgProc (HWND hDlg, UINT msg, UINT wParam, LONG lParam) ! 302: { ! 303: switch (msg) ! 304: { ! 305: case WM_INITDIALOG : ! 306: { ! 307: SetDlgItemText (hDlg, DID_EDIT, gpccs->szText); ! 308: break; ! 309: } ! 310: ! 311: case WM_COMMAND: ! 312: ! 313: switch (LOWORD(wParam)) ! 314: { ! 315: case DID_CANCEL: ! 316: ! 317: EndDialog (hDlg, 0); ! 318: break; ! 319: ! 320: case DID_OK: ! 321: ! 322: GetDlgItemText (hDlg, DID_EDIT, gpccs->szText, ! 323: CCHCCTEXT); ! 324: EndDialog (hDlg, 1); ! 325: break; ! 326: } ! 327: break; ! 328: } ! 329: return FALSE; ! 330: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.