home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / mswindo / programm / misc / 5493 < prev    next >
Encoding:
Text File  |  1993-01-29  |  6.7 KB  |  199 lines

  1. Newsgroups: comp.os.ms-windows.programmer.misc
  2. Path: sparky!uunet!europa.eng.gtefsd.com!howland.reston.ans.net!spool.mu.edu!yale.edu!newsserver.jvnc.net!gmd.de!Germany.EU.net!mcsun!fuug!kiae!relay1!river!csoft!news-server
  3. From:  smcl@speed.kiev.ua (Scott McLoughlin)
  4. Subject: Re: window dump in windows [LONG] [CODE]
  5. Reply-To: smcl@speed.kiev.ua
  6. Organization: Private Scott McLoughlin
  7. Date: Thu, 28 Jan 93 03:23:05 +0200
  8. Message-ID: <AAvLpPhOC5@speed.kiev.ua>
  9. Lines: 186
  10. References: <2272@bdrc.bdrc.bd.com>
  11. Sender: news-server@river.cs.kiev.ua
  12.  
  13. Howdy,
  14.  
  15. > Can someone tell me what I'm doing wrong here? I'm trying to print the
  16. > image/bitmap of my main window.
  17.  
  18. Yea, I've done this. Probably not a good idea. (see below)
  19.  
  20. > Here is my (undoubtably stupid) method...
  21.  
  22. Nah, that's what I (and probably _every_ other windows programmer)
  23. tried at first.
  24.  
  25. >    /* mainDC is obtained with GetDC()    */
  26. >    /* printDC is obtained via PrintDlg() */
  27. >    /* error checking deleted for clarity */
  28. >    Escape(printDC, STARTDOC, 6, "My App", NULL);
  29. >    BitBlt(printDC, 0, 0, 1000, 1000, mainDC, 0, 0, SRCCOPY);
  30. >    Escape(printDC, NEWFRAME, 0, NULL, NULL);
  31. >    Escape(printDC, ENDDOC, 0, NULL, NULL);
  32.  
  33. > When my code executes, all I get is a page ejected from the printer (which
  34. > is a Canon BJ10E). What am I leaving out in my quick and dirty method?
  35.  
  36. Welcome to windows.
  37.  
  38. The printer DC and the display (window) DC are not compatible.
  39. If I remember correctly, you have to retrieve the bitmap from the window,
  40. select this bitmap into a memory DC then bitblt to the printer DC.
  41.  
  42. I wrote just such a function for a co. I worked for about a year ago or
  43. so.  I happen to have the function lying around.  The code is below.
  44. I _think_ that this is the final version that works.  If not, sorry.
  45.  
  46. ::NOTE::  Once I wrote the routine, the functionality was inadequate.
  47. Once again, if memory serves, while DIBs come out nice and gray scaled
  48. on a HP, the aforementioned method produced lame black and white
  49. output (yellows and greens didn't show), and the fonts scaled (or rather,
  50. do not scale) poorly.
  51.  
  52. I moved on to other tasks.  Another bloke wrote a very nice routine
  53. to have all the graphics output to a metafile (one with the funny
  54. scalable header) which could be played either to the screen or to
  55. the printer.  If that sounds tougher, you're right.  No query
  56. GDI functions played to the metafile, no MM_TEXT mode, careful to use
  57. LOGFONTS, etc. ** In the end though, the output was impressive. **
  58.  
  59. Read Petzold on GDI very carefully.  Then learn all about reading/writing/
  60. /scaling/displaying DIBs and their palettes (most of this is folklore
  61. here on the net, CI$ and BIX/WIX).  Oh yeah, read _Advanced Windows
  62. Programming_ by Martin Heller.  Good discussions of bitmapped graphics
  63. display using GDI.
  64.  
  65. All this makes one realize that
  66. even though GDI is nicer than, say, Borland's BGI (_not_ a crit, I've
  67. done and seen some nifty stuff done with BGI), it is still pretty low level.
  68. For clean, maintainable application design, you probably want to add
  69. another "virtual" output layer in there that suits your application.
  70.  
  71. > Thanks,
  72. Your welcome.
  73. > john
  74. Scott McLoughlin
  75.  
  76. ----------  CODE LISTING FOLLOWS
  77.  
  78. /**************************************************************************
  79. **
  80. **      FILE:
  81. **
  82. **              BITPRN.CPP
  83. **
  84. **      PURPOSE:
  85. **
  86. **              Print a bitmap to a printer.
  87. **
  88. **************************************************************************/
  89.  
  90. #include <windows.h>
  91. #include <iostream.h>
  92. #include "bitprn.h"
  93.  
  94.  
  95.  
  96. BOOL prnWin(HWND hWnd, HDC hdcPrn, BOOL doMax) {
  97.  
  98.         POINT   ptPrintSize;
  99.         RECT    rectWinSize;
  100.         HDC     hdcWin, hdcMem;
  101.         HBITMAP hbmOld, hbmTest;
  102.  
  103.         int     iDevCaps = GetDeviceCaps(hdcPrn, RASTERCAPS);
  104.         char    szJobName[] = "TestBitBlt";
  105.  
  106.         /* Maximize the window if requested.
  107.         */
  108.         if (doMax) {
  109.                 SendMessage(hWnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0L);
  110.         }
  111.  
  112.         /* Get Printer Page dimensions and
  113.         ** Window Dimensions
  114.         */
  115.         Escape(hdcPrn, GETPHYSPAGESIZE, NULL, NULL, (char far*) &ptPrintSize);
  116.         GetClientRect(hWnd, &rectWinSize);
  117.  
  118. // NOTE: If I remember correctly, these tests return inaccurate
  119. //      negative results for _many_ drivers, including HP
  120. //      drivers.  But that was Win 3.0. Anyway...
  121.  
  122.         /* Test for BitBlt/StretchBlt capabilities
  123.         ** If printer cannot print bitmaps, then
  124.         ** return FALSE.
  125.         */
  126.         if ((iDevCaps & RC_BITBLT) != RC_BITBLT) {
  127.                 MessageBox(hWnd, "Printer is not able to\nprint bitmaps.",
  128.                         "Error!", MB_OK | MB_ICONEXCLAMATION);
  129.                 return (FALSE);
  130.         } else if ((iDevCaps & RC_STRETCHBLT) != RC_STRETCHBLT) {
  131.                 MessageBox(hWnd, "Printer is not able to\nstretch bitmaps.",
  132.                         "Warning!", MB_OK | MB_ICONEXCLAMATION);
  133.                 // No need to return
  134.         }
  135.  
  136.  
  137.         /* Create memory DC and bitmap.
  138.         ** Clean up during error handling!
  139.         */
  140.         if ((hdcWin = GetDC(hWnd)) == 0)
  141.                 return (FALSE);
  142.         if ((hdcMem = CreateCompatibleDC(hdcWin)) == 0) {
  143.                 ReleaseDC(hWnd, hdcWin); // Clean up
  144.                 return (FALSE);
  145.         }
  146.         if ((hbmTest = CreateCompatibleBitmap(
  147.           hdcWin, rectWinSize.right, rectWinSize.bottom)) == 0) {
  148.                 ReleaseDC(hWnd, hdcWin); // Clean up
  149.                 DeleteDC(hdcMem);
  150.                 return (FALSE);
  151.         }
  152.  
  153.         /* Copy screen image into bitmap
  154.         */
  155.         hbmOld = SelectObject(hdcMem, hbmTest);
  156.         BitBlt(hdcMem, 0, 0, rectWinSize.right, rectWinSize.bottom,
  157.                 hdcWin, 0, 0, SRCCOPY);
  158.  
  159.  
  160.         /* Start Document.  If failure, clean up.
  161.         */
  162.         if (Escape(hdcPrn, STARTDOC, sizeof(szJobName), szJobName,NULL) < 1) {
  163.                 MessageBox(hWnd, "Problem starting document.", "Error!",
  164.                         MB_OK | MB_ICONEXCLAMATION);
  165.  
  166.                 SelectObject(hdcMem, hbmOld); // Clean up
  167.                 DeleteDC(hdcMem);
  168.                 DeleteObject(hbmTest);
  169.                 ReleaseDC(hWnd, hdcWin);
  170.                 return (FALSE);
  171.         }
  172.  
  173.         /* Copy bitmap to printer
  174.         */
  175.         if (BitBlt(hdcPrn,
  176.                 0, 0, rectWinSize.right, rectWinSize.bottom,
  177.                 hdcMem, 0, 0, SRCCOPY) == 0) {
  178.                 MessageBox(hWnd, "Unsuccessful BitBlt!", "Error!",
  179.                         MB_OK | MB_ICONEXCLAMATION);
  180.         }
  181.  
  182.         /* New page and end document
  183.         */
  184.         if (Escape(hdcPrn, NEWFRAME, 0, NULL, NULL) > 0)
  185.                 Escape(hdcPrn, ENDDOC, 0, NULL, NULL);
  186.  
  187.         /* Clean up
  188.         */
  189.         SelectObject(hdcMem, hbmOld);
  190.         DeleteObject(hbmTest);
  191.         DeleteDC(hdcMem);
  192.         ReleaseDC(hWnd, hdcWin);
  193.         return (TRUE);
  194. }
  195.  
  196.  
  197.  
  198.  
  199.