VFP 也说下程序窗口截屏[收藏自行者孙]

也许大家都见过VFP下把程序窗口截屏到剪切板的代码,但如何把截屏保存到硬盘往往令很多刚学VFP的人感到头痛,记得www.news2news.com/vfp的作者写过一个,可能有些狐友已经见过了,但里面繁杂的结构引用是代码看上去并不是很好理解,现在有了GDI+就使这样的事情变的简单多了,代码也通俗易懂,这里给大家演示一下如何用GDI+保存程序界面截图到本地!

FUNCTION HWND2JPG(wHwnd as INTEGER,imagefile as string)
**wHwnd 窗口句柄
***imagefile 要保存到本地的文件名,我这里只定义了JPG格式,大家可以扩展
CF_BITMAP=2
SRCCOPY=13369376
Declare SHORT GetWindowRect IN user32 INTEGER hwnd, STRING @ lpRect
Declare INTEGER GetWindowDC IN user32 INTEGER hwnd
Declare INTEGER CreateCompatibleDC IN gdi32 INTEGER hdc
Declare INTEGER DeleteDC IN gdi32 INTEGER hdc
Declare INTEGER ReleaseDC IN user32 INTEGER hwnd, INTEGER hdc
Declare INTEGER CreateCompatibleBitmap IN gdi32;
INTEGER hdc,;
INTEGER nWidth,;
INTEGER nHeight
Declare INTEGER SelectObject IN gdi32 INTEGER hdc, INTEGER hObject
Declare INTEGER DeleteObject IN gdi32 INTEGER hObject
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 OpenClipboard IN user32 INTEGER hwnd
Declare INTEGER CloseClipboard IN user32
Declare INTEGER EmptyClipboard IN user32
Declare INTEGER SetClipboardData IN user32;
INTEGER wFormat,;
INTEGER hMem
DECLARE INTEGER GetClipboardData IN user32;
INTEGER uFormat
DECLARE INTEGER GdipCreateBitmapFromHBITMAP IN gdiplus;
INTEGER hbm,;
INTEGER hpal,;
INTEGER @ hbitmap
DECLARE INTEGER GdipSaveImageToFile IN gdiplus;
INTEGER img,;
STRING filename,;
STRING clsidEncoder,;
INTEGER encoderParams
DECLARE Long GdipDisposeImage IN Gdiplus.dll Long nativeImage
lpRect = REPLI (Chr(0), 16)
GetWindowRect (wHwnd, @lpRect)
lnWidth=ctob(SUBS(lpRect,9,4),’4rs’)-ctob(SUBS(lpRect,1,4),’4rs’)&&窗口宽度
lnHeight=ctob(SUBS(lpRect,13,4),’4rs’)-ctob(SUBS(lpRect,5,4),’4rs’)&&窗口高度
hdc = GetWindowDC (wHwnd)
hVdc = CreateCompatibleDC (hdc)
hBitmap = CreateCompatibleBitmap(hdc, lnWidth, lnHeight)
SelectObject (hVdc, hBitmap)
BitBlt (hVdc, 0,0, lnWidth,lnHeight, hdc, 0,0, SRCCOPY)
OpenClipboard (wHwnd)
EmptyClipboard()
SetClipboardData (CF_BITMAP, hBitmap)
hClipBmp=GetClipboardData(CF_BITMAP)
hbitmap=0
GdipCreateBitmapFromHBITMAP(hClipBmp,2,@hbitmap)
lcOutputFile=STRCONV(imagefile+CHR(0),5)
jpg=0h01F47C55041AD3119A730000F81EF32E&&JPG格式,其它格式可以到我前面的文章里找
GdipSaveImageToFile(hbitmap,lcOutputFile,jpg,0)
GdipDisposeImage(hbitmap)
CloseClipboard()
DeleteObject (hBitmap)
DeleteDC (hVdc)
ReleaseDC (wHwnd, hdc)
ENDFUNC

***************************************
行者孙 (QQ:310727570)
***************************************
VFP应用程式算法群:12787940
***************************************

posted @ 2008-10-03 14:16  月亮不合眼  阅读(1382)  评论(0编辑  收藏  举报