Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to copy the screen of a form and print it
Message
 
To
08/02/2005 13:27:03
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00984796
Message ID:
00984808
Views:
30
Here is some code that I picked up on the Universalthread some time back. I placed the initial code on a button and the Procedure PrintForm in a procedure file and it works perfectly, i.e. printing from within your foxpro application:


#DEFINE LOGPIXELSX 88
#DEFINE LOGPIXELSY 90
#DEFINE PHYSICALOFFSETX 112
#DEFINE PHYSICALOFFSETY 113
#DEFINE SRCCOPY 13369376
#DEFINE DIB_RGB_COLORS 0


DO PrintForm

PRIVATE pnWidth, pnHeight, lnBitsPerPixel, lnBytesPerScan
STORE 0 TO pnWidth, pnHeight, lnBitsPerPixel, lnBytesPerScan

LOCAL hwnd, hFormDC, hPrnDC, hMemDC, hMemBmp, hSavedBitmap,;
xOffsPrn, yOffsPrn, xScale, yScale, lcDocInfo, lcBInfo, lpBitsArray

* retrieving printer device context and page offsets
hPrnDC = getDefaultPrnDC() && no error check
xOffsPrn = GetDeviceCaps(hPrnDC, PHYSICALOFFSETX)
yOffsPrn = GetDeviceCaps(hPrnDC, PHYSICALOFFSETY)

* retrieving screen window handle, device context, width, and height
hwnd = GetFocus() && a window with keyboard focus
hFormDC = GetWindowDC(hwnd)
= getWinRect (hwnd, @pnWidth, @pnHeight)

* scaling factor values from screen to printer
xScale = GetDeviceCaps(hPrnDC, LOGPIXELSX)/GetDeviceCaps(hFormDC, LOGPIXELSX)
yScale = GetDeviceCaps(hPrnDC, LOGPIXELSY)/GetDeviceCaps(hFormDC, LOGPIXELSY)

* creating screen compatible DC and BITMAP to pass image
* through them from screen to printer; no direct screen to printer copying
hMemDC = CreateCompatibleDC (hFormDC)
hMemBmp = CreateCompatibleBitmap (hFormDC, pnWidth, pnHeight)
hSavedBitmap = SelectObject(hMemDC, hMemBmp)

* copying bitmap data from screen to virtual device context
* and unselecting the bmp from the memory device context
= BitBlt (hMemDC, 0,0, pnWidth,pnHeight, hFormDC, 0,0, SRCCOPY)
= SelectObject(hMemDC, hSavedBitmap)

* retrieving bits from the compatible bitmap into a buffer
* as a device-independent bitmap (DIB) with a BitsPerPixel value
* as one of the printer device context
lcBInfo = InitBitmapInfo(hPrnDC)
lpBitsArray = InitBitsArray()
= GetDIBits (hMemDC, hMemBmp, 0, pnHeight,;
lpBitsArray, @lcBInfo, DIB_RGB_COLORS)

lcDocInfo = Chr(20) + Repli(Chr(0), 19) && DOCINFO struct - 20 bytes
IF StartDoc(hPrnDC, @lcDocInfo) > 0
= StartPage(hPrnDC)

= StretchDIBits (hPrnDC, xOffsPrn, yOffsPrn,;
xOffsPrn + Int(xScale * pnWidth),;
yOffsPrn + Int(yScale * pnHeight),;
0,0, pnWidth, pnHeight,;
lpBitsArray, @lcBInfo, DIB_RGB_COLORS, SRCCOPY)

= EndPage(hPrnDC)
= EndDoc(hPrnDC)
ENDIF

* releasing system resources on exit
= GlobalFree(lpBitsArray)
= DeleteObject(hMemBmp)
= DeleteDC(hMemDC)
= DeleteDC(hPrnDC)
= ReleaseDC(hwnd, hFormDC)
RETURN




PROCEDURE printform && so many of them declared here
DECLARE INTEGER GetFocus IN user32
DECLARE INTEGER EndDoc IN gdi32 INTEGER hdc
DECLARE INTEGER GetWindowDC IN user32 INTEGER hwnd
DECLARE INTEGER DeleteObject IN gdi32 INTEGER hObject
DECLARE INTEGER CreateCompatibleDC IN gdi32 INTEGER hdc
DECLARE INTEGER ReleaseDC IN user32 INTEGER hwnd, INTEGER hdc
DECLARE INTEGER GetWindowRect IN user32 INTEGER hwnd, STRING @lpRect
DECLARE INTEGER GlobalAlloc IN kernel32 INTEGER wFlags, INTEGER dwBytes
DECLARE INTEGER GetDeviceCaps IN gdi32 INTEGER hdc, INTEGER nIndex
DECLARE INTEGER SelectObject IN gdi32 INTEGER hdc, INTEGER hObject
DECLARE INTEGER StartDoc IN gdi32 INTEGER hdc, STRING @ lpdi
DECLARE INTEGER GlobalFree IN kernel32 INTEGER hMem
DECLARE INTEGER PrintDlg IN comdlg32 STRING @ lppd
DECLARE INTEGER DeleteDC IN gdi32 INTEGER hdc
DECLARE INTEGER StartPage IN gdi32 INTEGER hdc
DECLARE INTEGER EndPage IN gdi32 INTEGER hdc

DECLARE RtlZeroMemory IN kernel32 As ZeroMemory;
INTEGER dest, INTEGER numBytes

DECLARE INTEGER CreateCompatibleBitmap IN gdi32;
INTEGER hdc, INTEGER nWidth, INTEGER nHeight

DECLARE INTEGER BitBlt IN gdi32;
INTEGER hDestDC, INTEGER x, INTEGER y,;
INTEGER nWidth, INTEGER nHeight, INTEGER hSrcDC,;
INTEGER xSrc, INTEGER ySrc, INTEGER dwRop

DECLARE INTEGER StretchDIBits IN gdi32;
INTEGER hdc, INTEGER XDest, INTEGER YDest,;
INTEGER nDestWidth, INTEGER nDestHeight, INTEGER XSrc,;
INTEGER YSrc, INTEGER nSrcWidth, INTEGER nSrcHeight,;
INTEGER lpBits, STRING @lpBitsInfo,;
INTEGER iUsage, INTEGER dwRop

DECLARE INTEGER GetDIBits IN gdi32;
INTEGER hdc, INTEGER hbmp, INTEGER uStartScan,;
INTEGER cScanLines, INTEGER lpvBits, STRING @lpbi,;
INTEGER uUsage
RETURN && decl
Kevin Delaney
Financial Systems Manager
CPL Solutions Ltd

83 Merrion Square, Dublin 2, Ireland.
e: kevin.delaney@cpl.ie w: www.cpl.ie
p: +353 1 482-5368
Previous
Reply
Map
View

Click here to load this message in the networking platform