|
|
1.1 ! root 1: /**************************************************************************** ! 2: * ! 3: * ! 4: * PROGRAM: CmnDlg.c ! 5: * ! 6: * PURPOSE: Sample demonstrating the use of the common dialogs in Windows ! 7: * ! 8: * FUNCTIONS: ! 9: * ! 10: * WinMain() - calls initialization function, processes message loop ! 11: * InitApplication() - initializes window data and registers window ! 12: * InitInstance() - saves instance handle and creates main window ! 13: * MainWndProc() - processes messages ! 14: * About() - processes messages for "About" dialog box ! 15: * OpenNewFile() - opens a new file ! 16: * SaveToFile() - saves the current text buffer to the current filename ! 17: * SaveAs() - saves the current text buffer to a new file name ! 18: * EnterNew() - to enter new text into the text buffer ! 19: * FileOpenHookProc() - Hook procedure for GetOpenFileName() common dialog ! 20: * FileSaveHookProc() - Hook procedure for GetSaveFileName() common dialog ! 21: * ChooseFontHookProc() - Hook procedure for ChooseFont() common dialog ! 22: * FindTextHookProc() - Hook procedure for FindText() common dialog ! 23: * ReplaceTextHookProc() - Hook procedure for the ReplaceText() common dialog ! 24: * PrintDlgHookProc() - Hook procedure for the PrintDlg() common dialog ! 25: * PrintSetupHookProc() - Hook procedure for the PrintDlg() setup common dialog ! 26: * SearchFile() - Searches for the specified text in the file buffer ! 27: * ChooseNewFont() - chooses a new font for display ! 28: * ChooseNewColor() - chooses a new color for display ! 29: * PrintFile() - prints the current text in the file buffer ! 30: * CallFindText() - calls the FindText() common dialog function ! 31: * CallReplaceText() - calls the ReplaceText() common dialog function ! 32: * ProcessCDError() - uses CommonDialogExtendedError() to output useful error messages ! 33: * ! 34: * COMMENTS: ! 35: * ! 36: * ! 37: * The common dialog APIs demonstrated in the sample include: ! 38: * ! 39: * ChooseColor() ! 40: * ChooseFont() ! 41: * FindText() ! 42: * GetOpenFileName() ! 43: * GetSaveFileName() ! 44: * PrintDlg() ! 45: * ReplaceText() ! 46: * ! 47: * ! 48: * Each dialog box is demonstrated being used in three different ways: ! 49: * standard, using a modified template and using a hook function. ! 50: * Currently, a hook procedure will not work with the ChooseColor() ! 51: * common dialog, so this option is disabled when the user chooses ! 52: * either Hook or Custom as their current mode. ! 53: * ! 54: * ! 55: ****************************************************************************/ ! 56: ! 57: #include <windows.h> // includes basic windows functionality ! 58: #include <commdlg.h> // includes common dialog functionality ! 59: #include <dlgs.h> // includes common dialog template defines ! 60: #include <stdio.h> // includes standard file i/o functionality ! 61: #include <string.h> // includes string functions ! 62: #include <cderr.h> // includes the common dialog error codes ! 63: #include "cmndlg.h" // includes my common dialog functions ! 64: ! 65: HANDLE hInst; ! 66: OPENFILENAME OpenFileName; ! 67: CHAR szDirName[256] = ""; ! 68: CHAR szFile[256] = "\0"; ! 69: CHAR szFileTitle[256]; ! 70: ! 71: // Filter specification for the OPENFILENAME struct ! 72: // This is portable for i386 and MIPS ! 73: // Leaving out the \0 terminator will cause improper DWORD alignment ! 74: // and cause a failure under MIPS ! 75: CHAR szFilter[] = "Text Files (*.TXT)\0*.TXT\0All Files (*.*)\0*.*\0"; ! 76: ! 77: CHAR FileBuf[FILE_LEN]; ! 78: DWORD dwFileSize; ! 79: UINT FindReplaceMsg; ! 80: CHAR szFindString[64] = ""; ! 81: CHAR szReplaceString[64] = ""; ! 82: FINDREPLACE frText; ! 83: LPFINDREPLACE lpFR; ! 84: CHAR * BufPtr; ! 85: CHOOSEFONT chf; ! 86: CHOOSECOLOR chsclr; ! 87: COLORREF crColor; ! 88: LOGFONT lf; ! 89: WORD wMode = IDM_STANDARD; ! 90: HWND hDlgFR; ! 91: PRINTDLG pd; ! 92: ! 93: /**************************************************************************** ! 94: * ! 95: * FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int) ! 96: * ! 97: * PURPOSE: calls initialization function, processes message loop ! 98: * ! 99: * COMMENTS: ! 100: * ! 101: * ! 102: ****************************************************************************/ ! 103: ! 104: int APIENTRY WinMain( ! 105: HANDLE hInstance, ! 106: HANDLE hPrevInstance, ! 107: LPSTR lpCmdLine, ! 108: int nCmdShow ! 109: ) ! 110: { ! 111: ! 112: MSG msg; /* message */ ! 113: ! 114: if (!hPrevInstance) /* Other instances of app running? */ ! 115: if (!InitApplication(hInstance)) /* Initialize shared things */ ! 116: return (FALSE); /* Exits if unable to initialize */ ! 117: ! 118: hInst = hInstance; ! 119: ! 120: /* Perform initializations that apply to a specific instance */ ! 121: ! 122: if (!InitInstance(hInstance, nCmdShow)) ! 123: return (FALSE); ! 124: ! 125: // register window message for FindText() and ReplaceText() hook procs ! 126: FindReplaceMsg = RegisterWindowMessage( (LPSTR) FINDMSGSTRING ); ! 127: ! 128: /* Acquire and dispatch messages until a WM_QUIT message is received. */ ! 129: ! 130: while (GetMessage(&msg, /* message structure */ ! 131: NULL, /* handle of window receiving the message */ ! 132: NULL, /* lowest message to examine */ ! 133: NULL)) /* highest message to examine */ ! 134: { ! 135: TranslateMessage(&msg); /* Translates virtual key codes */ ! 136: DispatchMessage(&msg); /* Dispatches message to window */ ! 137: } ! 138: return (msg.wParam); /* Returns the value from PostQuitMessage */ ! 139: ! 140: // avoid compiler warnings at W3 ! 141: lpCmdLine; ! 142: } ! 143: ! 144: ! 145: /**************************************************************************** ! 146: * ! 147: * FUNCTION: InitApplication(HANDLE) ! 148: * ! 149: * PURPOSE: Initializes window data and registers window class ! 150: * ! 151: * COMMENTS: ! 152: * ! 153: * In this function, we initialize a window class by filling out a data ! 154: * structure of type WNDCLASS and calling the Windows RegisterClass() ! 155: * function. ! 156: * ! 157: ****************************************************************************/ ! 158: ! 159: BOOL InitApplication(HANDLE hInstance) /* current instance */ ! 160: { ! 161: WNDCLASS wc; ! 162: ! 163: /* Fill in window class structure with parameters that describe the */ ! 164: /* main window. */ ! 165: ! 166: wc.style = NULL; /* Class style(s). */ ! 167: wc.lpfnWndProc = (WNDPROC)MainWndProc; /* Function to retrieve messages for */ ! 168: /* windows of this class. */ ! 169: wc.cbClsExtra = 0; /* No per-class extra data. */ ! 170: wc.cbWndExtra = 0; /* No per-window extra data. */ ! 171: wc.hInstance = hInstance; /* Application that owns the class. */ ! 172: wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); ! 173: wc.hCursor = LoadCursor(NULL, IDC_ARROW); ! 174: wc.hbrBackground = GetStockObject(WHITE_BRUSH); ! 175: wc.lpszMenuName = "CmnDlgMenu"; /* Name of menu resource in .RC file. */ ! 176: wc.lpszClassName = "CmnDlgWClass"; /* Name used in call to CreateWindow. */ ! 177: ! 178: /* Register the window class and return success/failure code. */ ! 179: ! 180: return (RegisterClass(&wc)); ! 181: ! 182: } ! 183: ! 184: ! 185: /**************************************************************************** ! 186: * ! 187: * FUNCTION: InitInstance(HANDLE, int) ! 188: * ! 189: * PURPOSE: Saves instance handle and creates main window ! 190: * ! 191: * COMMENTS: ! 192: * ! 193: * In this function, we save the instance handle in a static variable and ! 194: * create and display the main program window. ! 195: * ! 196: ****************************************************************************/ ! 197: ! 198: BOOL InitInstance( ! 199: HANDLE hInstance, /* Current instance identifier. */ ! 200: int nCmdShow) /* Param for first ShowWindow() call. */ ! 201: { ! 202: HWND hWnd; /* Main window handle. */ ! 203: ! 204: /* Save the instance handle in static variable, which will be used in */ ! 205: /* many subsequence calls from this application to Windows. */ ! 206: ! 207: hInst = hInstance; ! 208: ! 209: /* Create a main window for this application instance. */ ! 210: ! 211: hWnd = CreateWindow( ! 212: "CmnDlgWClass", /* See RegisterClass() call. */ ! 213: "Common Dialogs Sample Application", /* Text for window title bar. */ ! 214: WS_OVERLAPPEDWINDOW, /* Window style. */ ! 215: CW_USEDEFAULT, /* Default horizontal position. */ ! 216: CW_USEDEFAULT, /* Default vertical position. */ ! 217: CW_USEDEFAULT, /* Default width. */ ! 218: CW_USEDEFAULT, /* Default height. */ ! 219: NULL, /* Overlapped windows have no parent. */ ! 220: NULL, /* Use the window class menu. */ ! 221: hInstance, /* This instance owns this window. */ ! 222: NULL /* Pointer not needed. */ ! 223: ); ! 224: ! 225: /* If window could not be created, return "failure" */ ! 226: ! 227: if (!hWnd) ! 228: return (FALSE); ! 229: ! 230: /* Make the window visible; update its client area; and return "success" */ ! 231: ! 232: ShowWindow(hWnd, nCmdShow); /* Show the window */ ! 233: UpdateWindow(hWnd); /* Sends WM_PAINT message */ ! 234: return (TRUE); /* Returns the value from PostQuitMessage */ ! 235: ! 236: } ! 237: ! 238: /**************************************************************************** ! 239: * ! 240: * FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG) ! 241: * ! 242: * PURPOSE: Processes messages ! 243: * ! 244: * COMMENTS: ! 245: * ! 246: * This function processes all messags sent to the window. When the ! 247: * user choses one of the options from one of the menus, the command ! 248: * is processed here and passed onto the function for that command. ! 249: * This function also processes the special "FindReplace" message that ! 250: * this application registers for hook processing of the FindText() ! 251: * and ReplaceText() common dialog functions. ! 252: * ! 253: ****************************************************************************/ ! 254: ! 255: LONG APIENTRY MainWndProc( ! 256: HWND hWnd, /* window handle */ ! 257: UINT message, /* type of message */ ! 258: UINT wParam, /* additional information */ ! 259: LONG lParam) /* additional information */ ! 260: { ! 261: FARPROC lpProcAbout; /* pointer to the "About" function */ ! 262: FARPROC lpProcEnterNew; /* pointer to the "EnterNew" function */ ! 263: HDC hDC; ! 264: PAINTSTRUCT ps; ! 265: INT nDrawX; ! 266: INT nDrawY; ! 267: HFONT hFont; ! 268: HANDLE Handle; ! 269: static BOOL NewFont; ! 270: ! 271: switch (message) { ! 272: ! 273: ! 274: case WM_CREATE: ! 275: //initialize the output on the screen ! 276: strcpy( FileBuf, "Hello World!"); ! 277: dwFileSize = strlen(FileBuf); ! 278: crColor = 0; ! 279: NewFont = FALSE; ! 280: break; ! 281: ! 282: ! 283: case WM_PAINT: ! 284: /* Set up a display context to begin painting */ ! 285: hDC = BeginPaint (hWnd, &ps); ! 286: ! 287: /* Initialize drawing position to 1/4 inch from the top */ ! 288: /* and from the left of the top, left corner of the */ ! 289: /* client area of the main windows. */ ! 290: nDrawX = GetDeviceCaps(hDC, LOGPIXELSX) / 4; /* 1/4 inch */ ! 291: nDrawY = GetDeviceCaps(hDC, LOGPIXELSY) / 4; /* 1/4 inch */ ! 292: ! 293: if ( NewFont == TRUE ) ! 294: { ! 295: hFont = CreateFontIndirect( &lf ); ! 296: Handle = SelectObject( hDC, hFont ); ! 297: } ! 298: ! 299: SetTextColor( hDC, crColor ); ! 300: TextOut (hDC, nDrawX, nDrawY, (LPSTR)&FileBuf[0], dwFileSize); ! 301: ! 302: ! 303: // end painting and release hDC ! 304: EndPaint( hWnd, &ps ); ! 305: break; ! 306: ! 307: ! 308: case WM_COMMAND: /* message: command from application menu */ ! 309: ! 310: switch( LOWORD( wParam )) ! 311: { ! 312: case IDM_OPENFILE: ! 313: if ( OpenNewFile( hWnd ) == TRUE ) ! 314: { ! 315: // enable the Save As and Print menu items ! 316: EnableMenuItem( GetMenu( hWnd ), IDM_SAVEFILE, ! 317: MF_BYCOMMAND | MF_ENABLED ); ! 318: EnableMenuItem( GetMenu( hWnd ), IDM_PRINT, ! 319: MF_BYCOMMAND | MF_ENABLED ); ! 320: DrawMenuBar( hWnd); ! 321: // reset the title in the title bar to reflect the ! 322: // new open file ! 323: SetWindowText( hWnd, OpenFileName.lpstrFile ); ! 324: // reset the current color and current font to the ! 325: // default ! 326: crColor = 0; ! 327: NewFont = FALSE; ! 328: InvalidateRect( hWnd, NULL, TRUE ); ! 329: } ! 330: break; ! 331: ! 332: case IDM_SAVEFILE: ! 333: OpenFileName.Flags = 0L; ! 334: SaveToFile( hWnd ); ! 335: break; ! 336: ! 337: case IDM_SAVEFILEAS: ! 338: if ( SaveAs( hWnd ) == TRUE ) ! 339: { ! 340: EnableMenuItem( GetMenu( hWnd ), IDM_SAVEFILE, ! 341: MF_BYCOMMAND | MF_ENABLED ); ! 342: SetWindowText( hWnd, OpenFileName.lpstrFile ); ! 343: DrawMenuBar( hWnd ); ! 344: } ! 345: break; ! 346: ! 347: case IDM_EXIT: ! 348: PostQuitMessage(0); ! 349: break; ! 350: ! 351: case IDM_PRINT: ! 352: PrintFile( hWnd ); ! 353: break; ! 354: ! 355: case IDM_CHOOSECOLOR: ! 356: if (ChooseNewColor( hWnd )) ! 357: InvalidateRect( hWnd, NULL, TRUE ); ! 358: break; ! 359: ! 360: case IDM_CHOOSEFONT: ! 361: if (NewFont = ChooseNewFont( hWnd )) ! 362: InvalidateRect( hWnd, NULL, TRUE ); ! 363: ! 364: break; ! 365: ! 366: case IDM_FINDTEXT: ! 367: CallFindText( hWnd ); ! 368: break; ! 369: ! 370: case IDM_REPLACETEXT: ! 371: CallReplaceText( hWnd ); ! 372: break; ! 373: ! 374: case IDM_STANDARD: ! 375: ! 376: #ifdef NOTIMPLEMENTED ! 377: // enable the ChooseColor() option ! 378: EnableMenuItem(GetMenu(hWnd), IDM_CHOOSECOLOR, ! 379: MF_BYCOMMAND | MF_ENABLED ); ! 380: // uncheck previous selection ! 381: CheckMenuItem( GetMenu( hWnd ), wMode, MF_UNCHECKED | MF_BYCOMMAND); ! 382: //reset mode ! 383: wMode = LOWORD(wParam); ! 384: //check new selection ! 385: CheckMenuItem( GetMenu( hWnd ), wMode, MF_CHECKED | MF_BYCOMMAND); ! 386: DrawMenuBar( hWnd); ! 387: break; ! 388: #endif ! 389: case IDM_HOOK: ! 390: case IDM_CUSTOM: ! 391: #ifdef NOTIMPLEMENTED ! 392: // disable the ChooseColor() option ! 393: EnableMenuItem(GetMenu(hWnd), IDM_CHOOSECOLOR, ! 394: MF_BYCOMMAND | MF_GRAYED ); ! 395: #endif ! 396: // uncheck previous selection ! 397: CheckMenuItem( GetMenu( hWnd ), wMode, MF_UNCHECKED | MF_BYCOMMAND); ! 398: //reset mode ! 399: wMode = LOWORD(wParam); ! 400: //check new selection ! 401: CheckMenuItem( GetMenu( hWnd ), wMode, MF_CHECKED | MF_BYCOMMAND); ! 402: DrawMenuBar( hWnd); ! 403: break; ! 404: ! 405: case IDM_ENTERNEW: ! 406: lpProcEnterNew = MakeProcInstance((FARPROC)EnterNew, hInst); ! 407: ! 408: if (DialogBox(hInst, /* current instance */ ! 409: "EnterNewBox", /* resource to use */ ! 410: hWnd, /* parent handle */ ! 411: (WNDPROC)lpProcEnterNew) == TRUE) ! 412: ! 413: InvalidateRect( hWnd, NULL, TRUE ); ! 414: ! 415: FreeProcInstance(lpProcEnterNew); ! 416: break; ! 417: ! 418: case IDM_ABOUT: ! 419: lpProcAbout = MakeProcInstance((FARPROC)About, hInst); ! 420: ! 421: DialogBox(hInst, /* current instance */ ! 422: "AboutBox", /* resource to use */ ! 423: hWnd, /* parent handle */ ! 424: (WNDPROC)lpProcAbout); /* About() instance address */ ! 425: ! 426: FreeProcInstance(lpProcAbout); ! 427: break; ! 428: ! 429: default: ! 430: return (DefWindowProc(hWnd, message, wParam, lParam)); ! 431: ! 432: } ! 433: break; ! 434: ! 435: case WM_DESTROY: /* message: window being destroyed */ ! 436: PostQuitMessage(0); ! 437: break; ! 438: ! 439: ! 440: default: ! 441: // Handle the special findreplace message (FindReplaceMsg) which ! 442: // was registered at initialization time. ! 443: if ( message == FindReplaceMsg ) ! 444: { ! 445: lpFR = (LPFINDREPLACE) lParam; ! 446: if (lpFR->Flags & FR_DIALOGTERM ) // terminating dialog ! 447: return (NULL); ! 448: SearchFile( lpFR ); ! 449: InvalidateRect( hWnd, NULL, TRUE ); ! 450: return (NULL); ! 451: } ! 452: ! 453: return (DefWindowProc(hWnd, message, wParam, lParam)); ! 454: } ! 455: return (NULL); ! 456: } ! 457: ! 458: ! 459: /**************************************************************************** ! 460: * ! 461: * FUNCTION: EnterNew(HWND, UINT, UINT, LONG) ! 462: * ! 463: * PURPOSE: Processes messages for "EnterNew" dialog box ! 464: * ! 465: * COMMENTS: ! 466: * ! 467: * This function allows the user to enter new text in the current ! 468: * window. This text is stored in the global current buffer. ! 469: * ! 470: ****************************************************************************/ ! 471: ! 472: BOOL APIENTRY EnterNew( ! 473: HWND hDlg, /* window handle of the dialog box */ ! 474: UINT message, /* type of message */ ! 475: UINT wParam, /* message-specific information */ ! 476: LONG lParam) ! 477: { ! 478: CHAR Buf[FILE_LEN-1]; ! 479: ! 480: switch (message) ! 481: { ! 482: case WM_INITDIALOG: /* message: initialize dialog box */ ! 483: return (TRUE); ! 484: ! 485: case WM_COMMAND: /* message: received a command */ ! 486: if (LOWORD(wParam) == IDOK) ! 487: { ! 488: GetDlgItemText( hDlg, IDEDIT, Buf, FILE_LEN-1); ! 489: strcpy( FileBuf, Buf); ! 490: dwFileSize = strlen(FileBuf); ! 491: EndDialog( hDlg, TRUE ); ! 492: return (TRUE); ! 493: } ! 494: else if (LOWORD(wParam) == IDCANCEL) ! 495: { /* System menu close command? */ ! 496: EndDialog(hDlg, FALSE); /* Exits the dialog box */ ! 497: return (TRUE); ! 498: } ! 499: break; ! 500: } ! 501: return (FALSE); /* Didn't process a message */ ! 502: ! 503: // avoid compiler warnings at W3 ! 504: lParam; ! 505: } ! 506: ! 507: ! 508: /**************************************************************************** ! 509: * ! 510: * FUNCTION: About(HWND, UINT, UINT, LONG) ! 511: * ! 512: * PURPOSE: Processes messages for "About" dialog box ! 513: * ! 514: * COMMENTS: ! 515: * ! 516: * No initialization is needed for this particular dialog box, but TRUE ! 517: * must be returned to Windows. ! 518: * ! 519: * Wait for user to click on "Ok" button, then close the dialog box. ! 520: * ! 521: ****************************************************************************/ ! 522: ! 523: BOOL APIENTRY About( ! 524: HWND hDlg, /* window handle of the dialog box */ ! 525: UINT message, /* type of message */ ! 526: UINT wParam, /* message-specific information */ ! 527: LONG lParam) ! 528: { ! 529: switch (message) ! 530: { ! 531: case WM_INITDIALOG: /* message: initialize dialog box */ ! 532: return (TRUE); ! 533: ! 534: case WM_COMMAND: /* message: received a command */ ! 535: if (LOWORD(wParam) == IDOK /* "OK" box selected? */ ! 536: || LOWORD(wParam) == IDCANCEL) { /* System menu close command? */ ! 537: EndDialog(hDlg, TRUE); /* Exits the dialog box */ ! 538: return (TRUE); ! 539: } ! 540: break; ! 541: } ! 542: return (FALSE); /* Didn't process a message */ ! 543: ! 544: // avoid compiler warnings at W3 ! 545: lParam; ! 546: } ! 547: ! 548: /**************************************************************************** ! 549: * ! 550: * FUNCTION: FileOpenHookProc(HWND, UINT, UINT, LONG) ! 551: * ! 552: * PURPOSE: Processes messages for GetFileNameOpen() common dialog box ! 553: * ! 554: * COMMENTS: ! 555: * ! 556: * This function will prompt the user if they are sure they want ! 557: * to open the file if the OFN_ENABLEHOOK flag is set. ! 558: * ! 559: * If the current option mode is CUSTOM, the user is allowed to check ! 560: * a box in the dialog prompting them whether or not they would like ! 561: * the file created. If they check this box, the file is created and ! 562: * the string 'Empty' is written to it. ! 563: * ! 564: * RETURN VALUES: ! 565: * TRUE - User chose 'Yes' from the "Are you sure message box". ! 566: * FALSE - User chose 'No'; return to the dialog box. ! 567: * ! 568: ****************************************************************************/ ! 569: ! 570: BOOL APIENTRY FileOpenHookProc( ! 571: HWND hDlg, /* window handle of the dialog box */ ! 572: UINT message, /* type of message */ ! 573: UINT wParam, /* message-specific information */ ! 574: LONG lParam) ! 575: { ! 576: ! 577: int hFile; ! 578: CHAR szTempText[256]; ! 579: CHAR szString[256]; ! 580: OFSTRUCT OfStruct; ! 581: ! 582: switch (message) ! 583: { ! 584: ! 585: case WM_COMMAND: ! 586: if (LOWORD(wParam) == IDOK) ! 587: { ! 588: GetDlgItemText( hDlg, edt1, szTempText, ! 589: sizeof( szTempText ) - 1); ! 590: ! 591: if ( OpenFileName.Flags & OFN_PATHMUSTEXIST ) ! 592: { ! 593: sprintf( szString, "Are you sure you want to open %s?", ! 594: szTempText); ! 595: if ( MessageBox( hDlg, szString, "Information", ! 596: MB_YESNO ) == IDYES ) ! 597: ! 598: break; ! 599: return (TRUE); ! 600: } ! 601: ! 602: // check to see if the Create File box has been checked ! 603: if ( (BOOL)(SendMessage( GetDlgItem(hDlg, chx2), ! 604: BM_GETCHECK, 0, 0L )) == TRUE ) ! 605: { ! 606: // if so, create the file ! 607: if ((hFile = OpenFile(szTempText, ! 608: &OfStruct, ! 609: OF_CREATE)) == -1) ! 610: { ! 611: MessageBox( hDlg, ! 612: "Directory could not be created.", ! 613: NULL, ! 614: MB_OK ); ! 615: return (FALSE); ! 616: } ! 617: ! 618: strcpy(FileBuf, "Empty"); ! 619: dwFileSize = strlen(FileBuf); ! 620: if (_lwrite( hFile, (LPSTR)&FileBuf[0], dwFileSize)==-1) ! 621: MessageBox( hDlg, "Error writing file.", NULL, MB_OK ); ! 622: ! 623: // close the file ! 624: _lclose( hFile ); ! 625: } ! 626: ! 627: } ! 628: break; ! 629: } ! 630: return (FALSE); ! 631: ! 632: // avoid compiler warnings at W3 ! 633: lParam; ! 634: ! 635: } ! 636: ! 637: /**************************************************************************** ! 638: * ! 639: * FUNCTION: OpenNewFile(HWND) ! 640: * ! 641: * PURPOSE: Invokes common dialog function to open a file and opens it. ! 642: * ! 643: * COMMENTS: ! 644: * ! 645: * This function initializes the OPENFILENAME structure and calls ! 646: * the GetOpenFileName() common dialog function. This function will ! 647: * work regardless of the mode: standard, using a hook or using a ! 648: * customized template. ! 649: * ! 650: * RETURN VALUES: ! 651: * TRUE - The file was opened successfully and read into the buffer. ! 652: * FALSE - No files were opened. ! 653: * ! 654: ****************************************************************************/ ! 655: BOOL OpenNewFile( HWND hWnd ) ! 656: { ! 657: int hFile; ! 658: int cBufLen; ! 659: OFSTRUCT OfStruct; ! 660: WORD wStyle; ! 661: ! 662: strcpy( szFile, ""); ! 663: strcpy( szFileTitle, ""); ! 664: ! 665: OpenFileName.lStructSize = sizeof(OPENFILENAME); ! 666: OpenFileName.hwndOwner = hWnd; ! 667: OpenFileName.hInstance = (HANDLE) hInst; ! 668: OpenFileName.lpstrFilter = szFilter; ! 669: OpenFileName.lpstrCustomFilter = (LPSTR) NULL; ! 670: OpenFileName.nMaxCustFilter = 0L; ! 671: OpenFileName.nFilterIndex = 1L; ! 672: OpenFileName.lpstrFile = szFile; ! 673: OpenFileName.nMaxFile = sizeof(szFile); ! 674: OpenFileName.lpstrFileTitle = szFileTitle; ! 675: OpenFileName.nMaxFileTitle = sizeof(szFileTitle); ! 676: OpenFileName.lpstrInitialDir = NULL; ! 677: OpenFileName.lpstrTitle = "Open a File"; ! 678: OpenFileName.nFileOffset = 0; ! 679: OpenFileName.nFileExtension = 0; ! 680: OpenFileName.lpstrDefExt = "*.txt"; ! 681: OpenFileName.lCustData = 0; ! 682: ! 683: switch( wMode ) ! 684: { ! 685: case IDM_STANDARD: ! 686: OpenFileName.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | ! 687: OFN_FILEMUSTEXIST | OFN_HIDEREADONLY; ! 688: break; ! 689: ! 690: case IDM_HOOK: ! 691: OpenFileName.Flags = OFN_SHOWHELP | OFN_PATHMUSTEXIST | ! 692: OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_ENABLEHOOK; ! 693: OpenFileName.lpfnHook = (LPOFNHOOKPROC)MakeProcInstance(FileOpenHookProc, NULL); ! 694: break; ! 695: ! 696: case IDM_CUSTOM: ! 697: OpenFileName.Flags = OFN_SHOWHELP | OFN_ENABLEHOOK | ! 698: OFN_HIDEREADONLY | OFN_ENABLETEMPLATE; ! 699: OpenFileName.lpfnHook = (LPOFNHOOKPROC)MakeProcInstance(FileOpenHookProc, NULL); ! 700: OpenFileName.lpTemplateName = (LPSTR)MAKEINTRESOURCE(FILEOPENORD); ! 701: break; ! 702: } ! 703: ! 704: if (GetOpenFileName(&OpenFileName)) ! 705: { ! 706: if (OpenFileName.Flags & OFN_READONLY ) ! 707: wStyle = OF_READ; ! 708: else ! 709: wStyle = OF_READWRITE; ! 710: ! 711: // open the file ! 712: hFile = OpenFile(OpenFileName.lpstrFile, &OfStruct, wStyle ); ! 713: if (hFile == -1) ! 714: { ! 715: MessageBox( hWnd, "File open failed.", NULL, MB_OK ); ! 716: return FALSE; ! 717: } ! 718: // read it's contents into a buffer ! 719: cBufLen = _lread( hFile, FileBuf, FILE_LEN ); ! 720: ! 721: if (cBufLen == -1) ! 722: { ! 723: MessageBox( hWnd, "Zero bytes read.", NULL, MB_OK ); ! 724: return FALSE; ! 725: } ! 726: // close the file ! 727: _lclose( hFile ); ! 728: ! 729: dwFileSize = cBufLen; ! 730: } ! 731: else ! 732: { ! 733: ProcessCDError(CommDlgExtendedError(), hWnd ); ! 734: return FALSE; ! 735: } ! 736: return TRUE; ! 737: } ! 738: ! 739: /**************************************************************************** ! 740: * ! 741: * FUNCTION: SaveToFile( HWND ) ! 742: * ! 743: * PURPOSE: Saves the current buffer to the current file. ! 744: * ! 745: * COMMENTS: ! 746: * ! 747: * This function will save the current text buffer into the file ! 748: * specified from the GetSaveFileName() common dialog function. ! 749: * ! 750: * RETURN VALUES: ! 751: * TRUE - The file was saved successfully. ! 752: * FALSE - The buffer was not saved to a file. ! 753: * ! 754: ****************************************************************************/ ! 755: BOOL SaveToFile( HWND hWnd ) ! 756: { ! 757: int hFile; ! 758: OFSTRUCT OfStruct; ! 759: WORD wStyle; ! 760: CHAR buf[256]; ! 761: ! 762: if (OpenFileName.Flags | OFN_FILEMUSTEXIST) ! 763: wStyle = OF_READWRITE; ! 764: else ! 765: wStyle = OF_READWRITE | OF_CREATE; ! 766: ! 767: if ((hFile = OpenFile(OpenFileName.lpstrFile, &OfStruct, ! 768: wStyle)) == -1) ! 769: { ! 770: sprintf( buf, "Could not create file %s", OpenFileName.lpstrFile ); ! 771: MessageBox( hWnd, buf, NULL, MB_OK ); ! 772: return FALSE; ! 773: } ! 774: // write it's contents into a file ! 775: if (_lwrite( hFile, (LPSTR)&FileBuf[0], dwFileSize)==-1) ! 776: { ! 777: MessageBox( hWnd, "Error writing file.", NULL, MB_OK ); ! 778: return FALSE; ! 779: } ! 780: ! 781: // close the file ! 782: _lclose( hFile ); ! 783: ! 784: sprintf( buf, "%s", OpenFileName.lpstrFile ); ! 785: MessageBox( hWnd, buf, "File Saved", MB_OK ); ! 786: return TRUE; ! 787: } ! 788: ! 789: ! 790: /**************************************************************************** ! 791: * ! 792: * FUNCTION: FileSaveHookProc(HWND, UINT, UINT, LONG) ! 793: * ! 794: * PURPOSE: Processes messages for FileSave common dialog box ! 795: * ! 796: * COMMENTS: ! 797: * ! 798: * This hook procedure prompts the user if they want to save the ! 799: * current file. If they choose YES, the file is saved and the dialog ! 800: * is dismissed. If they choose NO, they are returned to the ! 801: * GetSaveFileName() common dialog. ! 802: * ! 803: * If the current mode calls for a customized template, this function ! 804: * will test the 'Create File?' checkbox. If the user choses no, the ! 805: * OFN_FILEMUSTEXIST flag is set. ! 806: * ! 807: * RETURN VALUES: ! 808: * TRUE - User chose 'Yes' from the "Are you sure message box". ! 809: * FALSE - User chose 'No'; return to the dialog box. ! 810: * ! 811: * ! 812: ****************************************************************************/ ! 813: ! 814: BOOL APIENTRY FileSaveHookProc( ! 815: HWND hDlg, /* window handle of the dialog box */ ! 816: UINT message, /* type of message */ ! 817: UINT wParam, /* message-specific information */ ! 818: LONG lParam) ! 819: { ! 820: CHAR szTempText[256]; ! 821: CHAR szString[256]; ! 822: ! 823: switch (message) ! 824: { ! 825: case WM_COMMAND: ! 826: if (LOWORD(wParam) == IDOK) ! 827: { ! 828: GetDlgItemText( hDlg, edt1, szTempText, ! 829: sizeof( szTempText ) - 1); ! 830: if ( OpenFileName.Flags & OFN_ENABLETEMPLATE ) ! 831: { ! 832: // check to see if the Create File box has been checked ! 833: if ( (BOOL)(SendMessage( GetDlgItem(hDlg, chx2), ! 834: BM_GETCHECK, 0, 0L )) == FALSE ) ! 835: OpenFileName.Flags |= OFN_FILEMUSTEXIST; ! 836: break; ! 837: ! 838: } ! 839: else ! 840: { ! 841: sprintf( szString, "Are you sure you want to save %s?", ! 842: szTempText); ! 843: if ( MessageBox( hDlg, szString, "Information", ! 844: MB_YESNO ) == IDYES ) ! 845: break; ! 846: return(TRUE); ! 847: } ! 848: ! 849: } ! 850: break; ! 851: } ! 852: return (FALSE); ! 853: ! 854: // avoid compiler warnings at W3 ! 855: lParam; ! 856: ! 857: } ! 858: ! 859: /**************************************************************************** ! 860: * ! 861: * FUNCTION: SaveAs(HWND) ! 862: * ! 863: * PURPOSE: Invokes the common dialog function to save the current ! 864: * buffer to a file. ! 865: * COMMENTS: ! 866: * ! 867: * This function initializes the OPENFILENAME structure for any ! 868: * mode selected by the user: standard, using a hook or using a ! 869: * customized template. It then calls the GetSaveFileName() ! 870: * common dialog function. ! 871: * ! 872: * RETURN VALUES: ! 873: * TRUE - The file was saved successfully. ! 874: * FALSE - The buffer was not saved to a file. ! 875: * ! 876: ****************************************************************************/ ! 877: BOOL SaveAs( HWND hWnd ) ! 878: { ! 879: ! 880: strcpy( szFile, ""); ! 881: strcpy( szFileTitle, ""); ! 882: ! 883: OpenFileName.lStructSize = sizeof(OPENFILENAME); ! 884: OpenFileName.hwndOwner = hWnd; ! 885: OpenFileName.hInstance = (HANDLE) hInst; ! 886: OpenFileName.lpstrFilter = szFilter; ! 887: OpenFileName.lpstrCustomFilter = (LPSTR) NULL; ! 888: OpenFileName.nMaxCustFilter = 0L; ! 889: OpenFileName.nFilterIndex = 1L; ! 890: OpenFileName.lpstrFile = szFile; ! 891: OpenFileName.nMaxFile = sizeof(szFile); ! 892: OpenFileName.lpstrFileTitle = szFileTitle; ! 893: OpenFileName.nMaxFileTitle = sizeof(szFileTitle); ! 894: OpenFileName.lpstrInitialDir = NULL; ! 895: OpenFileName.lpstrTitle = "Save File As"; ! 896: OpenFileName.nFileOffset = 0; ! 897: OpenFileName.nFileExtension = 0; ! 898: OpenFileName.lpstrDefExt = "txt"; ! 899: OpenFileName.lCustData = 0; ! 900: ! 901: switch( wMode ) ! 902: { ! 903: case IDM_STANDARD: ! 904: OpenFileName.Flags = 0L; ! 905: OpenFileName.lpfnHook = (LPOFNHOOKPROC)(FARPROC)NULL; ! 906: OpenFileName.lpTemplateName = (LPSTR)NULL; ! 907: break; ! 908: ! 909: case IDM_HOOK: ! 910: OpenFileName.Flags = OFN_ENABLEHOOK; ! 911: OpenFileName.lpfnHook = (LPOFNHOOKPROC)MakeProcInstance(FileSaveHookProc, NULL); ! 912: OpenFileName.lpTemplateName = (LPSTR)NULL; ! 913: break; ! 914: ! 915: case IDM_CUSTOM: ! 916: OpenFileName.Flags = OFN_ENABLEHOOK | OFN_ENABLETEMPLATE; ! 917: OpenFileName.lpfnHook = (LPOFNHOOKPROC)MakeProcInstance(FileSaveHookProc, NULL); ! 918: OpenFileName.lpTemplateName = (LPSTR)MAKEINTRESOURCE(FILEOPENORD); ! 919: break; ! 920: } ! 921: ! 922: if ( GetSaveFileName( &OpenFileName )) ! 923: return(SaveToFile( hWnd )); ! 924: else ! 925: { ! 926: ProcessCDError(CommDlgExtendedError(), hWnd ); ! 927: return FALSE; ! 928: } ! 929: ! 930: return (FALSE); ! 931: } ! 932: ! 933: ! 934: /**************************************************************************** ! 935: * ! 936: * FUNCTION: ChooseColorHookProc(HWND, UINT, UINT, LONG) ! 937: * ! 938: * PURPOSE: Processes messages for ChooseColor common dialog box ! 939: * ! 940: * COMMENTS: ! 941: * ! 942: * This hook procedure simply prompts the user whether or not they ! 943: * want to change the color. if they choose YES, the color of the ! 944: * text will be changed and the dialog will be dismissed. If they ! 945: * choose NO, the color will not be changed and the user will be ! 946: * returned to the dialog ! 947: * ! 948: * RETURN VALUES: ! 949: * TRUE - User chose 'Yes' from the "Are you sure message box". ! 950: * FALSE - User chose 'No'; return to the dialog box. ! 951: * ! 952: ****************************************************************************/ ! 953: ! 954: BOOL APIENTRY ChooseColorHookProc( ! 955: HWND hDlg, /* window handle of the dialog box */ ! 956: UINT message, /* type of message */ ! 957: UINT wParam, /* message-specific information */ ! 958: LONG lParam) ! 959: { ! 960: ! 961: switch (message) ! 962: { ! 963: case WM_COMMAND: ! 964: if (LOWORD(wParam) == IDOK) ! 965: { ! 966: if (MessageBox( hDlg, "Are you sure you want to change the color?", ! 967: "Information", MB_YESNO ) == IDYES ) ! 968: break; ! 969: return (TRUE); ! 970: ! 971: } ! 972: break; ! 973: } ! 974: return (FALSE); ! 975: ! 976: // avoid compiler warnings at W3 ! 977: lParam; ! 978: ! 979: } ! 980: ! 981: ! 982: /**************************************************************************** ! 983: * ! 984: * FUNCTION: ChooseNewColor(HWND) ! 985: * ! 986: * PURPOSE: Invokes common dialog function to chose a new color. ! 987: * ! 988: * COMMENTS: ! 989: * This function initializes the CHOOSECOLOR structure for any ! 990: * mode the user chooses: standard, using a hook or using a ! 991: * customized template. It then calls the ChooseColor() ! 992: * common dialog function. ! 993: * ! 994: * Hook procedure in Win 32 for ChoseColor() common dialog is ! 995: * not implemented yet. ! 996: * ! 997: * RETURN VALUES: ! 998: * TRUE - A new color was chosen. ! 999: * FALSE - No new color was chosen. ! 1000: * ! 1001: ****************************************************************************/ ! 1002: BOOL ChooseNewColor( HWND hWnd ) ! 1003: { ! 1004: ! 1005: DWORD dwColor; ! 1006: DWORD dwCustClrs[16]; ! 1007: BOOL fSetColor = FALSE; ! 1008: int i; ! 1009: ! 1010: ! 1011: for (i=0; i < 15; i++) ! 1012: dwCustClrs[i] = RGB( 255, 255, 255); ! 1013: ! 1014: dwColor = RGB( 0, 0, 0 ); ! 1015: ! 1016: chsclr.lStructSize = sizeof(CHOOSECOLOR); ! 1017: chsclr.hwndOwner = hWnd; ! 1018: chsclr.hInstance = (HANDLE)hInst; ! 1019: chsclr.rgbResult = dwColor; ! 1020: chsclr.lpCustColors = (LPDWORD)dwCustClrs; ! 1021: chsclr.lCustData = 0L; ! 1022: ! 1023: switch( wMode ) ! 1024: { ! 1025: case IDM_HOOK: ! 1026: case IDM_CUSTOM: ! 1027: ! 1028: #ifdef NOTIMPLEMENTED ! 1029: // chsclr.Flags = CC_PREVENTFULLOPEN | CC_ENABLEHOOK; ! 1030: // chsclr.lpfnHook = (FARPROC)MakeProcInstance(ChooseColorHookProc, NULL); ! 1031: // chsclr.lpTemplateName = (LPSTR)NULL; ! 1032: // break; ! 1033: #endif ! 1034: ! 1035: case IDM_STANDARD: ! 1036: chsclr.Flags = CC_PREVENTFULLOPEN; ! 1037: chsclr.lpfnHook = (LPCCHOOKPROC)(FARPROC)NULL; ! 1038: chsclr.lpTemplateName = (LPSTR)NULL; ! 1039: break; ! 1040: ! 1041: ! 1042: } ! 1043: ! 1044: if ( fSetColor = ChooseColor( &chsclr )) ! 1045: { ! 1046: crColor = chsclr.rgbResult; ! 1047: return (TRUE); ! 1048: } ! 1049: else ! 1050: { ! 1051: ProcessCDError(CommDlgExtendedError(), hWnd ); ! 1052: return FALSE; ! 1053: } ! 1054: } ! 1055: ! 1056: ! 1057: /**************************************************************************** ! 1058: * ! 1059: * FUNCTION: ChooseFontHookProc(HWND, UINT, UINT, LONG) ! 1060: * ! 1061: * PURPOSE: Processes messages for ChooseFont common dialog box ! 1062: * ! 1063: * COMMENTS: ! 1064: * ! 1065: * This hook procedure simply prompts the user whether or not they ! 1066: * want to change the font. if they choose YES, the color of the ! 1067: * font will be changed and the dialog will be dismissed. If they ! 1068: * choose NO, the font will not be changed and the user will be ! 1069: * returned to the dialog ! 1070: * ! 1071: * If the current mode is set to use a customized template, the ! 1072: * color drop down combo box is hidden. ! 1073: * ! 1074: * RETURN VALUES: ! 1075: * TRUE - Change the font. ! 1076: * FALSE - Return to the dialog box. ! 1077: * ! 1078: ****************************************************************************/ ! 1079: ! 1080: BOOL APIENTRY ChooseFontHookProc( ! 1081: HWND hDlg, /* window handle of the dialog box */ ! 1082: UINT message, /* type of message */ ! 1083: UINT wParam, /* message-specific information */ ! 1084: LONG lParam) ! 1085: { ! 1086: ! 1087: switch (message) ! 1088: { ! 1089: case WM_INITDIALOG: ! 1090: if (chf.Flags & CF_ENABLETEMPLATE) ! 1091: { ! 1092: ShowWindow(GetDlgItem(hDlg, stc4), SW_HIDE); ! 1093: ShowWindow(GetDlgItem(hDlg, cmb4), SW_HIDE); ! 1094: } ! 1095: break; ! 1096: ! 1097: case WM_COMMAND: ! 1098: if (LOWORD(wParam) == IDOK) ! 1099: { ! 1100: if (MessageBox( hDlg, "Are you sure you want to change the font?", ! 1101: "Information", MB_YESNO ) == IDYES ) ! 1102: break; ! 1103: return (TRUE); ! 1104: ! 1105: } ! 1106: break; ! 1107: } ! 1108: return (FALSE); ! 1109: ! 1110: // avoid compiler warnings at W3 ! 1111: lParam; ! 1112: ! 1113: } ! 1114: ! 1115: ! 1116: /**************************************************************************** ! 1117: * ! 1118: * FUNCTION: ChooseNewFont(HWND) ! 1119: * ! 1120: * PURPOSE: Invokes common dialog function to chose a new font. ! 1121: * ! 1122: * COMMENTS: ! 1123: * ! 1124: * This function initializes the CHOOSEFONT structure for any mode ! 1125: * the user chooses: standard, using a hook or using a customized ! 1126: * template. It then calls the ChooseFont() common dialog function. ! 1127: * ! 1128: * RETURN VALUES: ! 1129: * TRUE - A new font was chosen. ! 1130: * FALSE - No new font was chosen. ! 1131: * ! 1132: ****************************************************************************/ ! 1133: BOOL ChooseNewFont( HWND hWnd ) ! 1134: { ! 1135: ! 1136: HDC hDC; ! 1137: ! 1138: hDC = GetDC( hWnd ); ! 1139: chf.hDC = CreateCompatibleDC( hDC ); ! 1140: ReleaseDC( hWnd, hDC ); ! 1141: chf.lStructSize = sizeof(CHOOSEFONT); ! 1142: chf.hwndOwner = hWnd; ! 1143: chf.lpLogFont = &lf; ! 1144: chf.Flags = CF_SCREENFONTS | CF_EFFECTS; ! 1145: chf.rgbColors = RGB(0, 255, 255); ! 1146: chf.lCustData = 0; ! 1147: chf.hInstance = (HANDLE)hInst; ! 1148: chf.lpszStyle = (LPSTR)NULL; ! 1149: chf.nFontType = SCREEN_FONTTYPE; ! 1150: chf.nSizeMin = 0; ! 1151: chf.nSizeMax = 0; ! 1152: ! 1153: switch( wMode ) ! 1154: { ! 1155: case IDM_STANDARD: ! 1156: chf.Flags = CF_SCREENFONTS | CF_EFFECTS; ! 1157: chf.lpfnHook = (LPCFHOOKPROC)(FARPROC)NULL; ! 1158: chf.lpTemplateName = (LPSTR)NULL; ! 1159: break; ! 1160: ! 1161: case IDM_HOOK: ! 1162: chf.Flags = CF_SCREENFONTS | CF_EFFECTS | CF_ENABLEHOOK; ! 1163: chf.lpfnHook = (LPCFHOOKPROC)MakeProcInstance(ChooseFontHookProc, NULL); ! 1164: chf.lpTemplateName = (LPSTR)NULL; ! 1165: break; ! 1166: ! 1167: case IDM_CUSTOM: ! 1168: chf.Flags = CF_SCREENFONTS | CF_EFFECTS | CF_ENABLEHOOK | ! 1169: CF_ENABLETEMPLATE; ! 1170: chf.lpfnHook = (LPCFHOOKPROC)MakeProcInstance(ChooseFontHookProc, NULL); ! 1171: chf.lpTemplateName = (LPSTR)MAKEINTRESOURCE(FORMATDLGORD31); ! 1172: break; ! 1173: } ! 1174: ! 1175: ! 1176: if( ChooseFont( &chf ) == FALSE ) ! 1177: { ! 1178: DeleteDC( hDC ); ! 1179: ProcessCDError(CommDlgExtendedError(), hWnd ); ! 1180: return FALSE; ! 1181: } ! 1182: ! 1183: ! 1184: DeleteDC( hDC ); ! 1185: ! 1186: return (TRUE); ! 1187: } ! 1188: ! 1189: /**************************************************************************** ! 1190: * ! 1191: * FUNCTION: PrintSetupHookProc(HWND, UINT, UINT, LONG) ! 1192: * ! 1193: * PURPOSE: Processes messages for PrintDlg setup common dialog box ! 1194: * ! 1195: * COMMENTS: ! 1196: * ! 1197: * This function processes the hook and customized template for the ! 1198: * print setup common dialog box. If the customized template has ! 1199: * been provided, the 'Options' pushbutton is hidden. If the hook only mode ! 1200: * is chosen, a message box is displayed informing the user that the ! 1201: * hook has been installed. ! 1202: * ! 1203: * RETURN VALUES: ! 1204: * TRUE - Continue. ! 1205: * FALSE - Return to the dialog box. ! 1206: * ! 1207: ****************************************************************************/ ! 1208: ! 1209: BOOL APIENTRY PrintSetupHookProc( ! 1210: HWND hDlg, /* window handle of the dialog box */ ! 1211: UINT message, /* type of message */ ! 1212: UINT wParam, /* message-specific information */ ! 1213: LONG lParam) ! 1214: { ! 1215: ! 1216: switch (message) ! 1217: { ! 1218: case WM_INITDIALOG: ! 1219: if (pd.Flags & PD_ENABLESETUPTEMPLATE ) ! 1220: { ! 1221: ShowWindow( GetDlgItem(hDlg, psh1), SW_HIDE ); ! 1222: return(TRUE); ! 1223: } ! 1224: MessageBox( hDlg, ! 1225: "Hook installed.", ! 1226: "Information", MB_OK ); ! 1227: return (TRUE); ! 1228: } ! 1229: return (FALSE); ! 1230: ! 1231: // avoid compiler warnings at W3 ! 1232: lParam; ! 1233: wParam; ! 1234: } ! 1235: ! 1236: ! 1237: ! 1238: /**************************************************************************** ! 1239: * ! 1240: * FUNCTION: PrintDlgHookProc(HWND, UINT, UINT, LONG) ! 1241: * ! 1242: * PURPOSE: Processes messages for PrintDlg common dialog box ! 1243: * ! 1244: * COMMENTS: ! 1245: * ! 1246: * This hook procedure simply prompts the user whether or not they ! 1247: * want to print. if they choose YES, the text buf will be printed ! 1248: * and the dialog will be dismissed. If they choose NO, the text buf ! 1249: * will not be printeded and the user will be returned to the dialog. ! 1250: * ! 1251: * If the current mode is 'custom', the 'Print to file' and 'Collate ! 1252: * Copies' options are hidden. ! 1253: * ! 1254: * RETURN VALUES: ! 1255: * TRUE - Continue. ! 1256: * FALSE - Return to the dialog box. ! 1257: * ! 1258: ****************************************************************************/ ! 1259: ! 1260: BOOL APIENTRY PrintDlgHookProc( ! 1261: HWND hDlg, /* window handle of the dialog box */ ! 1262: UINT message, /* type of message */ ! 1263: UINT wParam, /* message-specific information */ ! 1264: LONG lParam) ! 1265: { ! 1266: ! 1267: switch (message) ! 1268: { ! 1269: case WM_INITDIALOG: ! 1270: if (pd.Flags & PD_ENABLEPRINTTEMPLATE ) ! 1271: { ! 1272: ShowWindow( GetDlgItem(hDlg, chx1), SW_HIDE ); ! 1273: ShowWindow( GetDlgItem(hDlg, chx2), SW_HIDE ); ! 1274: return(TRUE); ! 1275: } ! 1276: MessageBox( hDlg, ! 1277: "Hook installed.", ! 1278: "Information", MB_OK ); ! 1279: return (TRUE); ! 1280: ! 1281: case WM_COMMAND: ! 1282: if (LOWORD(wParam) == IDOK) ! 1283: { ! 1284: if (MessageBox( hDlg, "Are you sure you want to print?", ! 1285: "Information", MB_YESNO ) == IDYES ) ! 1286: break; ! 1287: return (TRUE); ! 1288: ! 1289: } ! 1290: break; ! 1291: } ! 1292: return (FALSE); ! 1293: ! 1294: // avoid compiler warnings at W3 ! 1295: lParam; ! 1296: ! 1297: } ! 1298: ! 1299: ! 1300: /**************************************************************************** ! 1301: * ! 1302: * FUNCTION: PrintFile(HWND) ! 1303: * ! 1304: * PURPOSE: Invokes common dialog function to print. ! 1305: * ! 1306: * COMMENTS: ! 1307: * ! 1308: * This function initializes the PRINTDLG structure for all modes ! 1309: * possible: standard, using a hook or using a customized template. ! 1310: * When hook mode is chosen, a hook is installed for both the ! 1311: * Print dialog and the Print Setup dialog. When custom mode is ! 1312: * chosen, the templates are enabled for both the print dialog and ! 1313: * the Print Setup dialog boxes. ! 1314: * ! 1315: * If the PrintDlg() common dialog returns TRUE, the current ! 1316: * text buffer is printed out. ! 1317: * ! 1318: * ! 1319: * RETURN VALUES: ! 1320: * void. ! 1321: * ! 1322: ****************************************************************************/ ! 1323: void PrintFile( HWND hWnd ) ! 1324: { ! 1325: ! 1326: ! 1327: // initialize PRINTDLG structure ! 1328: pd.lStructSize = sizeof(PRINTDLG); ! 1329: pd.hwndOwner = hWnd; ! 1330: pd.hDevMode = (HANDLE)NULL; ! 1331: pd.hDevNames = (HANDLE)NULL; ! 1332: pd.nFromPage = 0; ! 1333: pd.nToPage = 0; ! 1334: pd.nMinPage = 0; ! 1335: pd.nMaxPage = 0; ! 1336: pd.nCopies = 0; ! 1337: pd.hInstance = (HANDLE)hInst; ! 1338: ! 1339: ! 1340: switch( wMode ) ! 1341: { ! 1342: case IDM_STANDARD: ! 1343: pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION | PD_PRINTSETUP; ! 1344: pd.lpfnSetupHook = (LPSETUPHOOKPROC)(FARPROC)NULL; ! 1345: pd.lpSetupTemplateName = (LPSTR)NULL; ! 1346: pd.lpfnPrintHook = (LPPRINTHOOKPROC)(FARPROC)NULL; ! 1347: pd.lpPrintTemplateName = (LPSTR)NULL; ! 1348: break; ! 1349: ! 1350: case IDM_HOOK: ! 1351: pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION | ! 1352: PD_ENABLEPRINTHOOK | PD_ENABLESETUPHOOK | PD_PRINTSETUP; ! 1353: pd.lpfnSetupHook = (LPSETUPHOOKPROC)MakeProcInstance(PrintSetupHookProc, NULL); ! 1354: pd.lpSetupTemplateName = (LPSTR)NULL; ! 1355: pd.lpfnPrintHook = (LPPRINTHOOKPROC)MakeProcInstance(PrintDlgHookProc, NULL); ! 1356: pd.lpPrintTemplateName = (LPSTR)NULL; ! 1357: break; ! 1358: ! 1359: case IDM_CUSTOM: ! 1360: pd.Flags = PD_RETURNDC | PD_NOPAGENUMS | PD_NOSELECTION | ! 1361: PD_ENABLEPRINTHOOK | PD_ENABLEPRINTTEMPLATE | ! 1362: PD_ENABLESETUPHOOK | PD_ENABLESETUPTEMPLATE | PD_PRINTSETUP; ! 1363: pd.lpfnSetupHook = (LPSETUPHOOKPROC)MakeProcInstance(PrintSetupHookProc, NULL); ! 1364: pd.lpSetupTemplateName = (LPSTR)MAKEINTRESOURCE(PRNSETUPDLGORD); ! 1365: pd.lpfnPrintHook = (LPPRINTHOOKPROC)MakeProcInstance(PrintDlgHookProc, NULL); ! 1366: pd.lpPrintTemplateName = (LPSTR)MAKEINTRESOURCE(PRINTDLGORD); ! 1367: break; ! 1368: ! 1369: } ! 1370: ! 1371: //print a test page if successful ! 1372: if (PrintDlg(&pd) == TRUE) ! 1373: { ! 1374: Escape(pd.hDC, STARTDOC, 8, "Test-Doc", NULL); ! 1375: ! 1376: //Print text ! 1377: TextOut(pd.hDC, 5, 5, FileBuf, strlen(FileBuf)); ! 1378: ! 1379: Escape(pd.hDC, NEWFRAME, 0, NULL, NULL); ! 1380: Escape(pd.hDC, ENDDOC, 0, NULL, NULL ); ! 1381: DeleteDC(pd.hDC); ! 1382: if (pd.hDevMode) ! 1383: GlobalFree(pd.hDevMode); ! 1384: if (pd.hDevNames) ! 1385: GlobalFree(pd.hDevNames); ! 1386: } ! 1387: else ! 1388: ProcessCDError(CommDlgExtendedError(), hWnd ); ! 1389: } ! 1390: ! 1391: ! 1392: /**************************************************************************** ! 1393: * ! 1394: * FUNCTION: ReplaceTextHookProc(HWND, UINT, UINT, LONG) ! 1395: * ! 1396: * PURPOSE: Processes messages for ReplaceText common dialog box ! 1397: * ! 1398: * COMMENTS: ! 1399: * ! 1400: * Puts up a message stating that the hook is active if hook ! 1401: * only active. Otherwise, if template enabled, hides the ! 1402: * Replace All pushbutton, plus the 'Match case' and ! 1403: * 'Match whole word' check box options. ! 1404: * ! 1405: * RETURN VALUES: ! 1406: * TRUE - Continue. ! 1407: * FALSE - Return to the dialog box. ! 1408: * ! 1409: ****************************************************************************/ ! 1410: ! 1411: BOOL APIENTRY ReplaceTextHookProc( ! 1412: HWND hDlg, /* window handle of the dialog box */ ! 1413: UINT message, /* type of message */ ! 1414: UINT wParam, /* message-specific information */ ! 1415: LONG lParam) ! 1416: { ! 1417: ! 1418: switch (message) ! 1419: { ! 1420: case WM_INITDIALOG: ! 1421: if (frText.Flags & FR_ENABLETEMPLATE ) ! 1422: { ! 1423: ShowWindow( GetDlgItem(hDlg, psh2), SW_HIDE ); ! 1424: ShowWindow( GetDlgItem(hDlg, chx1), SW_HIDE ); ! 1425: ShowWindow( GetDlgItem(hDlg, chx2), SW_HIDE ); ! 1426: return(TRUE); ! 1427: } ! 1428: MessageBox( hDlg, ! 1429: "Hook installed.", ! 1430: "Information", MB_OK ); ! 1431: return (TRUE); ! 1432: ! 1433: ! 1434: default: ! 1435: break; ! 1436: } ! 1437: return (FALSE); ! 1438: ! 1439: // avoid compiler warnings at W3 ! 1440: lParam; ! 1441: wParam; ! 1442: } ! 1443: ! 1444: /**************************************************************************** ! 1445: * ! 1446: * FUNCTION: FindTextHookProc(HWND, UINT, UINT, LONG) ! 1447: * ! 1448: * PURPOSE: Processes messages for FindText common dialog box ! 1449: * ! 1450: * COMMENTS: ! 1451: * ! 1452: * Puts up a message stating that the hook is active if hook ! 1453: * only enabled. If custom template, hides the 'Match case' ! 1454: * and 'Match whole word' options, hides the group box 'Direction' ! 1455: * with the radio buttons 'Up' and 'Down'. ! 1456: * ! 1457: * RETURN VALUES: ! 1458: * TRUE - Continue. ! 1459: * FALSE - Return to the dialog box. ! 1460: * ! 1461: ****************************************************************************/ ! 1462: ! 1463: BOOL APIENTRY FindTextHookProc( ! 1464: HWND hDlg, /* window handle of the dialog box */ ! 1465: UINT message, /* type of message */ ! 1466: UINT wParam, /* message-specific information */ ! 1467: LONG lParam) ! 1468: { ! 1469: ! 1470: switch (message) ! 1471: { ! 1472: case WM_INITDIALOG: ! 1473: if (frText.Flags & FR_ENABLETEMPLATE ) ! 1474: { ! 1475: ShowWindow(GetDlgItem(hDlg, chx1), SW_HIDE); ! 1476: ShowWindow(GetDlgItem(hDlg, grp1), SW_HIDE); ! 1477: ShowWindow(GetDlgItem(hDlg, chx2), SW_HIDE); ! 1478: ShowWindow(GetDlgItem(hDlg, rad1), SW_HIDE); ! 1479: ShowWindow(GetDlgItem(hDlg, rad2), SW_HIDE); ! 1480: return(TRUE); ! 1481: } ! 1482: MessageBox( hDlg, ! 1483: "Hook installed.", ! 1484: "Information", MB_OK ); ! 1485: return (TRUE); ! 1486: ! 1487: ! 1488: default: ! 1489: break; ! 1490: } ! 1491: return (FALSE); ! 1492: ! 1493: // avoid compiler warnings at W3 ! 1494: lParam; ! 1495: wParam; ! 1496: } ! 1497: ! 1498: ! 1499: /**************************************************************************** ! 1500: * ! 1501: * FUNCTION: CallFindText( HWND ) ! 1502: * ! 1503: * PURPOSE: Initializes and calls the FindText() ! 1504: * common dialog. ! 1505: * ! 1506: * COMMENTS: ! 1507: * ! 1508: * This function initialzes the FINDREPLACE structure for any mode: ! 1509: * standard, using a hook or using a customized template. It then ! 1510: * calls the FindText() common dialog function. ! 1511: * ! 1512: * RETURN VALUES: ! 1513: * void. ! 1514: * ! 1515: ****************************************************************************/ ! 1516: void CallFindText( HWND hWnd ) ! 1517: { ! 1518: ! 1519: frText.lStructSize = sizeof( frText ); ! 1520: frText.hwndOwner = hWnd; ! 1521: frText.hInstance = (HANDLE)hInst; ! 1522: frText.lpstrFindWhat = szFindString; ! 1523: frText.lpstrReplaceWith = (LPSTR)NULL; ! 1524: frText.wFindWhatLen = sizeof(szFindString); ! 1525: frText.wReplaceWithLen = 0; ! 1526: frText.lCustData = 0; ! 1527: ! 1528: switch( wMode ) ! 1529: { ! 1530: case IDM_STANDARD: ! 1531: frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD; ! 1532: frText.lpfnHook = (LPFRHOOKPROC)(FARPROC)NULL; ! 1533: frText.lpTemplateName = (LPSTR)NULL; ! 1534: break; ! 1535: ! 1536: case IDM_HOOK: ! 1537: frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD | ! 1538: FR_ENABLEHOOK; ! 1539: frText.lpfnHook = (LPFRHOOKPROC)MakeProcInstance(FindTextHookProc, NULL); ! 1540: frText.lpTemplateName = (LPSTR)NULL; ! 1541: break; ! 1542: ! 1543: case IDM_CUSTOM: ! 1544: frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD | ! 1545: FR_ENABLEHOOK | FR_ENABLETEMPLATE; ! 1546: frText.lpfnHook = (LPFRHOOKPROC)MakeProcInstance(FindTextHookProc, NULL); ! 1547: frText.lpTemplateName = (LPSTR)MAKEINTRESOURCE(FINDDLGORD); ! 1548: break; ! 1549: } ! 1550: ! 1551: if ((hDlgFR = FindText(&frText)) == NULL) ! 1552: ProcessCDError(CommDlgExtendedError(), hWnd ); ! 1553: ! 1554: } ! 1555: ! 1556: ! 1557: /**************************************************************************** ! 1558: * ! 1559: * FUNCTION: CallReplaceText( HWND ) ! 1560: * ! 1561: * PURPOSE: Initializes and calls the ReplaceText() ! 1562: * common dialog. ! 1563: * ! 1564: * COMMENTS: ! 1565: * ! 1566: * This function initialzes the FINDREPLACE structure for any mode: ! 1567: * standard, using a hook or using a customized template. It then ! 1568: * calls the ReplaceText() common dialog function. ! 1569: * ! 1570: * RETURN VALUES: ! 1571: * void. ! 1572: * ! 1573: ****************************************************************************/ ! 1574: void CallReplaceText( HWND hWnd ) ! 1575: { ! 1576: frText.lStructSize = sizeof( frText ); ! 1577: frText.hwndOwner = hWnd; ! 1578: frText.hInstance = (HANDLE)hInst; ! 1579: frText.lpstrFindWhat = szFindString; ! 1580: frText.lpstrReplaceWith = szReplaceString; ! 1581: frText.wFindWhatLen = sizeof(szFindString); ! 1582: frText.wReplaceWithLen = sizeof( szReplaceString ); ! 1583: frText.lCustData = 0; ! 1584: ! 1585: switch( wMode ) ! 1586: { ! 1587: case IDM_STANDARD: ! 1588: frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD; ! 1589: frText.lpfnHook = (LPFRHOOKPROC)(FARPROC)NULL; ! 1590: frText.lpTemplateName = (LPSTR)NULL; ! 1591: break; ! 1592: ! 1593: case IDM_HOOK: ! 1594: frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD | ! 1595: FR_ENABLEHOOK; ! 1596: frText.lpfnHook = (LPFRHOOKPROC)MakeProcInstance(ReplaceTextHookProc, NULL); ! 1597: frText.lpTemplateName = (LPSTR)NULL; ! 1598: break; ! 1599: ! 1600: case IDM_CUSTOM: ! 1601: frText.Flags = FR_NOMATCHCASE | FR_NOUPDOWN | FR_NOWHOLEWORD | ! 1602: FR_ENABLEHOOK | FR_ENABLETEMPLATE; ! 1603: frText.lpfnHook = (LPFRHOOKPROC)MakeProcInstance(ReplaceTextHookProc, NULL); ! 1604: frText.lpTemplateName = (LPSTR)MAKEINTRESOURCE(REPLACEDLGORD); ! 1605: break; ! 1606: } ! 1607: ! 1608: if ((hDlgFR = ReplaceText( &frText )) == NULL) ! 1609: ProcessCDError(CommDlgExtendedError(), hWnd ); ! 1610: ! 1611: } ! 1612: ! 1613: /**************************************************************************** ! 1614: * ! 1615: * FUNCTION: SearchFile(LPFINDREPLACE) ! 1616: * ! 1617: * PURPOSE: Does the find/replace specified by the Find/ReplaceText ! 1618: * common dialog. ! 1619: * ! 1620: * COMMENTS: ! 1621: * ! 1622: * This function does the lease necessary to implement find and ! 1623: * replace by calling existing c-runtime functions. It is in ! 1624: * no way intended to demonstrate either correct or efficient ! 1625: * methods for doing textual search or replacement. ! 1626: * ! 1627: * RETURN VALUES: ! 1628: * void. ! 1629: * ! 1630: ****************************************************************************/ ! 1631: void SearchFile( LPFINDREPLACE lpFR ) ! 1632: { ! 1633: ! 1634: CHAR Buf[FILE_LEN]; ! 1635: CHAR *pStr; ! 1636: int count; ! 1637: ! 1638: ! 1639: strnset(Buf, '\0', FILE_LEN -1); ! 1640: if ((pStr = strstr( FileBuf, lpFR->lpstrFindWhat )) == NULL) ! 1641: { ! 1642: sprintf( Buf, "%s not found.", lpFR->lpstrFindWhat ); ! 1643: MessageBox( lpFR->hwndOwner, Buf, "No luck", MB_OK | MB_TASKMODAL); ! 1644: } ! 1645: ! 1646: else ! 1647: { ! 1648: if ( lpFR->wReplaceWithLen == 0) ! 1649: { ! 1650: sprintf( Buf, "%s found!", lpFR->lpstrFindWhat ); ! 1651: MessageBox( lpFR->hwndOwner, Buf, "Success!", MB_OK | MB_TASKMODAL ); ! 1652: } ! 1653: else ! 1654: { ! 1655: // replace string specified in the replace with found string ! 1656: // copy up to found string into new buffer ! 1657: for (count=0; ; count++) ! 1658: if (*pStr == *(FileBuf+count)) ! 1659: break; ! 1660: strncpy( Buf, FileBuf, count ); ! 1661: // concatenate new string ! 1662: strcat( Buf, lpFR->lpstrReplaceWith ); ! 1663: // copy rest of string (less the found string) ! 1664: count += strlen(lpFR->lpstrFindWhat); ! 1665: strcat( Buf, &FileBuf[count]); ! 1666: strcpy( FileBuf, Buf ); ! 1667: dwFileSize = strlen(FileBuf); ! 1668: MessageBox( lpFR->hwndOwner, Buf, "Success!", MB_OK | MB_TASKMODAL ); ! 1669: } ! 1670: } ! 1671: EndDialog( hDlgFR, FALSE ); ! 1672: } ! 1673: ! 1674: ! 1675: /**************************************************************************** ! 1676: * ! 1677: * FUNCTION: ProcessCDError(DWORD) ! 1678: * ! 1679: * PURPOSE: Processes errors from the common dialog functions. ! 1680: * ! 1681: * COMMENTS: ! 1682: * ! 1683: * This function is called whenever a common dialog function ! 1684: * fails. The CommonDialogExtendedError() value is passed to ! 1685: * the function which maps the error value to a string table. ! 1686: * The string is loaded and displayed for the user. ! 1687: * ! 1688: * RETURN VALUES: ! 1689: * void. ! 1690: * ! 1691: ****************************************************************************/ ! 1692: void ProcessCDError(DWORD dwErrorCode, HWND hWnd) ! 1693: { ! 1694: WORD wStringID; ! 1695: CHAR buf[256]; ! 1696: ! 1697: switch(dwErrorCode) ! 1698: { ! 1699: case CDERR_DIALOGFAILURE: wStringID=IDS_DIALOGFAILURE; break; ! 1700: case CDERR_STRUCTSIZE: wStringID=IDS_STRUCTSIZE; break; ! 1701: case CDERR_INITIALIZATION: wStringID=IDS_INITIALIZATION; break; ! 1702: case CDERR_NOTEMPLATE: wStringID=IDS_NOTEMPLATE; break; ! 1703: case CDERR_NOHINSTANCE: wStringID=IDS_NOHINSTANCE; break; ! 1704: case CDERR_LOADSTRFAILURE: wStringID=IDS_LOADSTRFAILURE; break; ! 1705: case CDERR_FINDRESFAILURE: wStringID=IDS_FINDRESFAILURE; break; ! 1706: case CDERR_LOADRESFAILURE: wStringID=IDS_LOADRESFAILURE; break; ! 1707: case CDERR_LOCKRESFAILURE: wStringID=IDS_LOCKRESFAILURE; break; ! 1708: case CDERR_MEMALLOCFAILURE: wStringID=IDS_MEMALLOCFAILURE; break; ! 1709: case CDERR_MEMLOCKFAILURE: wStringID=IDS_MEMLOCKFAILURE; break; ! 1710: case CDERR_NOHOOK: wStringID=IDS_NOHOOK; break; ! 1711: case PDERR_SETUPFAILURE: wStringID=IDS_SETUPFAILURE; break; ! 1712: case PDERR_PARSEFAILURE: wStringID=IDS_PARSEFAILURE; break; ! 1713: case PDERR_RETDEFFAILURE: wStringID=IDS_RETDEFFAILURE; break; ! 1714: case PDERR_LOADDRVFAILURE: wStringID=IDS_LOADDRVFAILURE; break; ! 1715: case PDERR_GETDEVMODEFAIL: wStringID=IDS_GETDEVMODEFAIL; break; ! 1716: case PDERR_INITFAILURE: wStringID=IDS_INITFAILURE; break; ! 1717: case PDERR_NODEVICES: wStringID=IDS_NODEVICES; break; ! 1718: case PDERR_NODEFAULTPRN: wStringID=IDS_NODEFAULTPRN; break; ! 1719: case PDERR_DNDMMISMATCH: wStringID=IDS_DNDMMISMATCH; break; ! 1720: case PDERR_CREATEICFAILURE: wStringID=IDS_CREATEICFAILURE; break; ! 1721: case PDERR_PRINTERNOTFOUND: wStringID=IDS_PRINTERNOTFOUND; break; ! 1722: case CFERR_NOFONTS: wStringID=IDS_NOFONTS; break; ! 1723: case FNERR_SUBCLASSFAILURE: wStringID=IDS_SUBCLASSFAILURE; break; ! 1724: case FNERR_INVALIDFILENAME: wStringID=IDS_INVALIDFILENAME; break; ! 1725: case FNERR_BUFFERTOOSMALL: wStringID=IDS_BUFFERTOOSMALL; break; ! 1726: ! 1727: case 0: //User may have hit CANCEL or we got a *very* random error ! 1728: return; ! 1729: ! 1730: default: ! 1731: wStringID=IDS_UNKNOWNERROR; ! 1732: } ! 1733: ! 1734: LoadString(NULL, wStringID, buf, sizeof(buf)); ! 1735: MessageBox(hWnd, buf, NULL, MB_OK); ! 1736: return; ! 1737: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.