The code is based on custom GDI+ class. Download the class module first and save it in gdiplus.prg file.
Make sure the target file name is valid. The target file can be created in several graphics format supported by the GDI+: BMP, GIF, JPG, TIF.
Call this code from a method or event of a FoxPro form when that form is active, i.e. posesses the keyboard focus.
See also:
- GDI: Storing screen shot of a form to bitmap file
- Printing the image of a FoxPro form
- Storing clipboard contents to a BMP file
- Using the LoadImage() to display a BMP file on the main VFP window
- How to print a bitmap file
SET PROCEDURE TO gdiplus ADDITIVE
#DEFINE SRCCOPY 0x00CC0020
DO decl
* an instance of gdiplusinit should be created before
* and released after using any of gdi+ objects
PRIVATE gdiplus
gdiplus = CREATEOBJECT("gdiplusinit")
LOCAL hwindow, hdc, bmp, nWidth, nHeight
hwindow = GetFocus()
hdc = GetWindowDC(hwindow)
STORE 0 TO nWidth, nHeight
= GetWinRect(hwindow, @nWidth, @nHeight)
bmp = CREATEOBJECT("gdibitmap", m.nWidth, m.nHeight)
WITH bmp
.graphics.GetDC
= BitBlt(.graphics.hdc, 0,0, .imgwidth, .imgheight,;
m.hdc, 0,0, SRCCOPY)
.graphics.ReleaseDC
.SaveToFile("d:\temp\vfp.tif")
ENDWITH
= ReleaseDC(m.hwindow, m.hdc)
* end of main
PROCEDURE GetWinRect(hwindow, nWidth, nHeight)
#DEFINE maxDword 0xffffffff
LOCAL lpRect, nLeft, nTop, nRight, nBottom
lpRect = REPLI (Chr(0), 16)
= GetWindowRect (hwindow, @lpRect)
nRight = buf2dword(SUBSTR(lpRect, 9,4))
nBottom = buf2dword(SUBSTR(lpRect, 13,4))
nLeft = buf2dword(SUBSTR(lpRect, 1,4))
IF nLeft > nRight
nLeft = nLeft - maxDword
ENDIF
nTop = buf2dword(SUBSTR(lpRect, 5,4))
IF nTop > nBottom
nTop = nTop - maxDword
ENDIF
nWidth = nRight - nLeft
nHeight = nBottom - nTop
RETURN
PROCEDURE decl
DECLARE INTEGER GetFocus IN user32
DECLARE INTEGER GetWindowDC IN user32 INTEGER hwindow
DECLARE INTEGER ReleaseDC IN user32 INTEGER hwindow, INTEGER hdc
DECLARE INTEGER GetWindowRect IN user32 INTEGER hwnd, STRING @lpRect
DECLARE INTEGER BitBlt IN gdi32;
INTEGER hDestDC, INTEGER x, INTEGER y,;
INTEGER nWidth, INTEGER nHeight, INTEGER hSrcDC,;
INTEGER xSrc, INTEGER ySrc, INTEGER dwRop
BitBlt
GetFocus
GetWindowDC
GetWindowRect
ReleaseDC