home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / windows / slap2.zip / SLAP2.C < prev    next >
C/C++ Source or Header  |  1986-03-20  |  4KB  |  171 lines

  1. /*  Slap2
  2.         The main routine creates a printer DC, gets a DC for the screen
  3.         [using GetDC(0)] and blts from the screen to the printer.
  4. */
  5.  
  6. #include "windows.h"
  7. #include "winexp.h"
  8. #include "slap2.h"
  9.  
  10. Slap()
  11. {
  12.     char *pch;
  13.     char *pchFile;
  14.     char *pchPort;
  15.     HBITMAP hbm;
  16.     HDC hdcmem;
  17.  
  18.     HDC hPrintDC;
  19.     HDC hdc;
  20.     int xres, yres;
  21.  
  22.     bMenuUp = FALSE;
  23.     EnumWindows(lpfnEnumWnd, (long)NULL);
  24.     if (bMenuUp) {
  25.             /* put in string table */
  26.         Death((LPSTR)"Can't copy with menu visible.", TRUE);
  27.         return;
  28.     }
  29.  
  30.             /* get printer info to make a call to CreateDC */
  31.     GetProfileString((LPSTR)"windows", (LPSTR)"Device",
  32.                      (LPSTR)"", (LPSTR)fileandport, 40);
  33.     for (pch = fileandport; *pch && *pch != ','; ++pch)
  34.         ;
  35.     if (*pch)
  36.         *pch++=0;
  37.     while(*pch && *pch <= ' ')
  38.         pch++;
  39.     pchFile = pch;
  40.     while(*pch && *pch != ',' && *pch > ' ')
  41.         pch++;
  42.     if (*pch)
  43.         *pch++ = 0;
  44.     while (*pch && (*pch <= ' ' || *pch == ','))
  45.         pch++;
  46.     pchPort = pch;
  47.     while (*pch && *pch > ' ')
  48.         pch++;
  49.     *pch = 0;
  50.  
  51.     if (!(hPrintDC = CreateDC((LPSTR)pchFile, (LPSTR)fileandport,
  52.                               (LPSTR)pchPort, (LPSTR)NULL)))
  53.         {
  54. CantPrint:
  55.         if (hPrintDC)
  56.             DeleteDC(hPrintDC);
  57.         if (!bmessage) {
  58.             bmessage = TRUE;
  59.             Death((LPSTR)"Unable to copy screen to printer.", FALSE);
  60.             bmessage = FALSE;
  61.           }
  62.         return(NULL);
  63.         }
  64.  
  65.         hdc  = GetDC((HANDLE)NULL);
  66.         xres = GetDeviceCaps( hdc, HORZRES);
  67.         yres = GetDeviceCaps( hdc, VERTRES);
  68.  
  69.         hdcmem = CreateCompatibleDC(hdc);
  70.         hbm = CreateCompatibleBitmap(hdcmem, xres, yres);
  71.         if (!hbm)
  72.         goto CantPrint;
  73.  
  74.         Escape(hPrintDC, SETABORTPROC, 0, (LPSTR)lpfnAbortProc, (LPSTR)0);
  75.         if (Escape(hPrintDC, STARTDOC, lstrlen((LPSTR)"SLAP - Screen Print"),
  76.                     (LPSTR)"SLAP - Screen Print", (LPSTR)0) < 0)
  77.             goto CantPrint;
  78.  
  79.         SelectObject(hdcmem, hbm);
  80.  
  81.         BitBlt(hdcmem, 0, 0, xres, yres,
  82.                    hdc, 0, 0, SRCCOPY);
  83.         BitBlt(hPrintDC, 0, 0, xres, yres,
  84.                    hdcmem, 0, 0, SRCCOPY);
  85.  
  86.         Escape(hPrintDC, NEWFRAME, 0, (LPSTR)NULL, (LPSTR)0);
  87.         Escape(hPrintDC, ENDDOC, 0, (LPSTR)0, (LPSTR)0);
  88.         DeleteDC(hPrintDC);
  89.         ReleaseDC(0, hdc);
  90.     }
  91.  
  92.  
  93. /** EnumWnd
  94.     Callback routine from EnumWindows to determine if a menu is currently
  95.     pulled down.
  96. */
  97. BOOL FAR PASCAL EnumWnd( hwnd, lparam)
  98. HWND hwnd;
  99. long lparam;
  100. {
  101.     char buf [10];
  102.  
  103.     GetClassName(hwnd, (LPSTR)buf, 10);
  104.     if (!lstrcmp((LPSTR)buf, (LPSTR)"#32768")) {
  105.         bMenuUp = TRUE;
  106.         return FALSE;
  107.     }
  108.     return TRUE;
  109. }
  110.  
  111.  
  112. /** CheckPrintKey
  113.         This routine gets called every time a key is pressed on the
  114.         keyboard.  This is because the WH_KEYBOARD Windows Hook is set
  115.         to this routine.
  116. */
  117. BOOL far PASCAL CheckPrintKey(wParam, lParam)
  118. WORD wParam;
  119. LONG lParam;
  120. {
  121.     if (wParam == VK_F7 && lParam < 0) {       /* up transition of key */
  122.         if (Slap())
  123.             return(TRUE);
  124.     }
  125.     if (lpfnOldHook != (FARPROC)NULL)
  126.         return((*lpfnOldHook)(wParam, lParam));
  127.     else
  128.         return(FALSE);
  129. }
  130.  
  131.  
  132. /**  MyLoad
  133.         This is the entry point to the whole thing.
  134. */
  135. BOOL near pascal MyLoad(hInstance)
  136. HANDLE hInstance;
  137. {
  138.     hSlapInstance = hInstance;
  139.     lpfnCheckKey = MakeProcInstance(CheckPrintKey, hInstance);
  140.     lpfnOldHook = SetWindowsHook(WH_KEYBOARD, lpfnCheckKey);
  141.     lpfnEnumWnd = MakeProcInstance(EnumWnd, hInstance);
  142.     lpfnAbortProc = MakeProcInstance(fnAbortProc, hInstance);
  143.     Death((LPSTR)"F7 set to copy screen to printer.", FALSE);
  144.     return(TRUE);
  145. }
  146.  
  147.  
  148. /** Death
  149.         Generic Message system.
  150. */
  151. Death(message, bModal)
  152. LPSTR message;
  153. BOOL  bModal;
  154. {
  155.     MessageBox( NULL,
  156.         (LPSTR)message,
  157.         (LPSTR)"",
  158.         MB_SYSTEMMODAL | MB_OK | (bModal ? MB_ICONHAND : MB_ICONEXCLAMATION));
  159. }
  160.  
  161.  
  162. /** fnAbortProc
  163.        stub needed so spooler can operate correctly
  164. */
  165. int FAR PASCAL fnAbortProc(hPrintDC, iReserved)
  166. HDC hPrintDC;
  167. int iReserved;
  168.     {
  169.     return(TRUE);
  170.     }
  171.