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