Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
3.6 KiB

  1. /*++
  2. Copyright (c) Microsoft Corporation. All rights reserved.
  3. Module Name:
  4. Print.cpp
  5. Abstract:
  6. Implements printing functionality. Bad print
  7. functions are contained in badfunc.cpp.
  8. Notes:
  9. ANSI only - must run on Win9x.
  10. History:
  11. 01/30/01 rparsons Created
  12. 01/10/02 rparsons Revised
  13. --*/
  14. #include "demoapp.h"
  15. extern APPINFO g_ai;
  16. /*++
  17. Routine Description:
  18. Abort callback procedure for printing.
  19. Arguments:
  20. hDC - Print device context.
  21. Return Value:
  22. TRUE on success, FALSE otherwise.
  23. --*/
  24. BOOL
  25. CALLBACK
  26. AbortProc(
  27. IN HDC hDC,
  28. IN int nError
  29. )
  30. {
  31. MSG msg;
  32. while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  33. TranslateMessage(&msg);
  34. DispatchMessage(&msg);
  35. }
  36. return TRUE;
  37. }
  38. /*++
  39. Routine Description:
  40. Prints a little bit of text to a printer.
  41. Note that we call a couple bad functions
  42. from here.
  43. Arguments:
  44. hWnd - Parent window handle.
  45. lpTextOut - Text to print out.
  46. Return Value:
  47. TRUE on success, FALSE otherwise.
  48. --*/
  49. BOOL
  50. PrintDemoText(
  51. IN HWND hWnd,
  52. IN LPSTR lpTextOut
  53. )
  54. {
  55. HDC hDC = NULL;
  56. HANDLE hPrinter = NULL;
  57. DOCINFO di;
  58. PRINTDLG pdlg;
  59. char szError[MAX_PATH];
  60. BOOL bReturn = FALSE;
  61. BOOL bResult = FALSE;
  62. //
  63. // If we're allowed, call a bad function.
  64. // If the user doesn't have any network printers
  65. // installed, this function will fail (on Windows 2000/XP).
  66. //
  67. if (g_ai.fEnableBadFunc) {
  68. bReturn = BadEnumPrinters();
  69. if (!bReturn) {
  70. LoadString(g_ai.hInstance, IDS_NO_PRINTER, szError, sizeof(szError));
  71. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  72. return FALSE;
  73. }
  74. hPrinter = BadOpenPrinter();
  75. if (!hPrinter) {
  76. LoadString(g_ai.hInstance, IDS_NO_PRINTER, szError, sizeof(szError));
  77. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  78. return FALSE;
  79. } else {
  80. ClosePrinter(hPrinter);
  81. }
  82. }
  83. //
  84. // Initialize the PRINTDLG structure and obtain a device context for the
  85. // default printer.
  86. //
  87. memset(&pdlg, 0, sizeof(PRINTDLG));
  88. pdlg.lStructSize = sizeof(PRINTDLG);
  89. pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
  90. PrintDlg(&pdlg);
  91. hDC = pdlg.hDC;
  92. if (!hDC) {
  93. LoadString(g_ai.hInstance, IDS_NO_PRINT_DC, szError, sizeof(szError));
  94. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  95. return FALSE;
  96. }
  97. //
  98. // Set the AbortProc callback.
  99. //
  100. if (SetAbortProc(hDC, AbortProc) == SP_ERROR) {
  101. LoadString(g_ai.hInstance, IDS_ABORT_PROC, szError, sizeof(szError));
  102. MessageBox(hWnd, szError, 0, MB_ICONERROR);
  103. goto exit;
  104. }
  105. //
  106. // Initialize the DOCINFO structure and start the document.
  107. //
  108. di.cbSize = sizeof(DOCINFO);
  109. di.lpszDocName = "TestDoc";
  110. di.lpszOutput = NULL;
  111. di.lpszDatatype = NULL;
  112. di.fwType = 0;
  113. StartDoc(hDC, &di);
  114. //
  115. // Print one page.
  116. //
  117. StartPage(hDC);
  118. TextOut(hDC, 0, 0, lpTextOut, lstrlen(lpTextOut));
  119. EndPage(hDC);
  120. //
  121. // Tell the spooler that we're done.
  122. //
  123. EndDoc(hDC);
  124. bResult = TRUE;
  125. exit:
  126. DeleteDC(hDC);
  127. return bResult;
  128. }