如何将vfp中的表单以图片的格式插入word文档?
如题
[解决办法]
*WIN API-保存当前一个窗口到一个bmp文件
*----------------------------------------------------
*你可以在command 窗口执行 bmpform.prg,不过最好在要保存的form调用这个子程序.
*bmpform.prg
********
DO decl
PRIVATE hMemDC, hMemBmp, lnWidth, lnHeight, lnBitsPerPixel,;
lnBytesPerScan, lcBFileHdr, lcBIHdr, lpBitsArray, lnBitsSize,;
lcRgbQuad, lnRgbQuadSize, lcBInfo, lnFileSize
STORE " " TO lcBIHdr, lcBInfo, lcRgbQuad
STORE 0 TO hMemDC, hMemBmp, lnWidth, lnHeight, lnFileSize,;
lnBitsPerPixel, lnBytesPerScan, lnRgbQuadSize, lpBitsArray, lnBitsSize
= MakeSnapshot()
= InitBitmapInfo()
= InitBitsArray()
#DEFINE DIB_RGB_COLORS 0
= GetDIBits (hMemDC, hMemBmp, 0, lnHeight, lpBitsArray,;
@lcBInfo, DIB_RGB_COLORS)
LOCAL lcFilename
**保存的文件名
lcFilename = "c:\myfile.bmp "
IF bmp2file (lcFilename)
ACTI SCREEN
? "file: ", lcFilename
? "Size: ", LTRIM(TRANS(lnFileSize, "999,999,999,999 "))
? "Width: ", LTRIM(STR(lnWidth)) + " pixels "
? "Height: ", LTRIM(STR(lnHeight)) + " pixels "
? "Bits per pixel: ", LTRIM(STR(lnBitsPerPixel))
ENDIF
= GlobalFree (lpBitsArray)
= DeleteObject (hMemBmp)
= DeleteDC (hMemDC)
RETURN && main
PROCEDURE InitBitmapInfo()
#DEFINE BI_RGB 0
#DEFINE RGBQUAD_SIZE 4 && RGBQUAD
#DEFINE BHDR_SIZE 40 && BITMAPINFOHEADER
* forcing 24-bit format
lnBitsPerPixel = 24
lnBytesPerScan = lnWidth * 3
* line width should be DWORD-aligned (4 bytes)
* important for 16- and 24-bit color palettes
IF Mod(lnBytesPerScan, 4) <> 0
lnBytesPerScan = lnBytesPerScan + 4 - Mod(lnBytesPerScan, 4)
ENDIF
* initializing BitmapInfoHeader structure
lcBIHdr = num2dword(BHDR_SIZE) + num2dword(lnWidth) +;
num2dword(lnHeight) + num2word(1) + num2word(lnBitsPerPixel) +;
num2dword(BI_RGB) + num2dword(0) + num2dword(0) + num2dword(0) +;
num2dword(0) + num2dword(0)
* creating a buffer for the color table
IF lnBitsPerPixel <= 8
lnRgbQuadSize = (2^lnBitsPerPixel) * RGBQUAD_SIZE
lcRgbQuad = Repli(Chr(0), lnRgbQuadSize)
ELSE
lnRgbQuadSize = 0
lcRgbQuad = " "
ENDIF
* merging two pieces together
lcBInfo = lcBIHdr + lcRgbQuad
RETURN
PROCEDURE InitBitsArray()
#DEFINE GMEM_FIXED 0
lnBitsSize = lnHeight * lnBytesPerScan
lpBitsArray = GlobalAlloc (GMEM_FIXED, lnBitsSize)
= ZeroMemory (lpBitsArray, lnBitsSize)
FUNCTION bmp2file (lcTargetFile)
* store all gathered pieces to a disk file
#DEFINE GENERIC_WRITE 1073741824 && 0x40000000
#DEFINE FILE_SHARE_WRITE 2
#DEFINE CREATE_ALWAYS 2
#DEFINE FILE_ATTRIBUTE_NORMAL 128
#DEFINE INVALID_HANDLE_value -1
#DEFINE BFHDR_SIZE 14 && BITMAPFILEHEADER
LOCAL hFile, lnOffBits
* resulting BMP file size
lnFileSize = BFHDR_SIZE + BHDR_SIZE + lnRgbQuadSize + lnBitsSize
* offset to the bitmap bits
lnOffBits = BFHDR_SIZE + BHDR_SIZE + lnRgbQuadSize
* BMP file header
lcBFileHdr = "BM " + num2dword(lnFileSize) +;
num2dword(0) + num2dword(lnOffBits)
* target file handle
hFile = CreateFile (lcTargetFile,;
GENERIC_WRITE,;
FILE_SHARE_WRITE, 0,;
CREATE_ALWAYS,;
FILE_ATTRIBUTE_NORMAL, 0)
IF hFile <> INVALID_HANDLE_value
* a straightforward process of storing block after block:
= String2File (hFile, @lcBFileHdr) && BitmapFileHeader
= String2File (hFile, @lcBInfo) && BitmapInfo
= Ptr2File (hFile, lpBitsArray, lnBitsSize) && bitmap data
= CloseHandle (hFile)
RETURN .T.
ELSE
= MessageB ( "Unable to create file: " + lcTargetFile)
RETURN .F.
ENDIF
PROCEDURE String2File (hFile, lcBuffer)
* appends string buffer to a file
DECLARE INTEGER WriteFile IN kernel32;
INTEGER hFile, STRING @lpBuffer, INTEGER nBt2Write,;
INTEGER @lpBtWritten, INTEGER lpOverlapped
= WriteFile (hFile, @lcBuffer, Len(lcBuffer), 0, 0)
RETURN