请都市夜猫老师看一下截屏打印的问题

kmyz_yyl 2020-07-11 04:16:33
* 这段代码是网上找的,据说是都市夜猫老师的代码,请帮看一下代码没有执行的原因,我主要是想实现打印鼠标点击图片就直接打印,请老师指点!

DECLARE INTEGER FindWindow IN user32 STRING lpClassName,STRING lpWindowName
DECLARE INTEGER GetDC IN user32 INTEGER hwnd
Declare SHORT GetWindowRect IN user32 INTEGER hwnd, STRING @ lpRect
DECLARE Long GetWindowDC IN WIN32API Long hWnd
DECLARE Long ReleaseDC IN WIN32API Long hWnd, Long hDc
DECLARE Long CreateCompatibleDC IN WIN32API Long hDc
DECLARE Long DeleteDC IN WIN32API Long hDc
DECLARE Long CreateCompatibleBitmap IN WIN32API Long hDc, Long nWidth, Long nHeight
DECLARE Long BitBlt IN WIN32API Long hDestDC, Long nXDest, Long nYDest, Long nWidth, Long nHeight, Long hSrcDC, Long nXSrc, Long nYSrc, Long dwRop
DECLARE Long SelectObject IN WIN32API Long hDc, Long hObject
DECLARE Long DeleteObject IN WIN32API Long hObject
DECLARE Long GdiplusStartup IN gdiplus Long @ token, String @ inputbuf, Long @ outputbuf
DECLARE Long GdiplusShutdown IN gdiplus Long token
DECLARE Long GdipCreateBitmapFromHBITMAP IN gdiplus Long hbitmap, Long hpalette, Long @ hGpBitmap
DECLARE Long GdipDisposeImage IN gdiplus Long image
DECLARE Long GdipSaveImageToFile IN gdiplus Long nImage, String FileName, String clsIdEncoder, Long encoderParams

**以上的API 不是全部用上 只是复制进来,懒得删除了!
DECLARE INTEGER PrintWindow IN user32 INTEGER,INTEGER ,INTEGER

#define SRCCOPY 0x00CC0020 LOCAL cFileExtName, cEncoder, iInputBuf, iResult
LOCAL hDesktopDc, hVDc, hBitmap, hToken, hGdipBitmap
m.hWnd=FindWindow("Notepad",NULL) &&截取一个文本窗口试一试
tcFile=LOWER('d:\aaa.JPG') &&jpg 格式图像
m.cFileExtName =LOWER( JUSTEXT( m.tcFile ))
lpRect = REPLI (Chr(0), 16)
GetWindowRect (m.hWnd, @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')&&窗口高度

m.hDesktopDc = GetDC( m.hWnd )
m.hVdc = CreateCompatibleDC( m.hDesktopDc )
m.hBitmap = CreateCompatibleBitmap( m.hDesktopDc,lnWidth,lnHeight )
ABC=SelectObject( m.hVdc, m.hBitmap )
i=PrintWindow(m.hWnd,m.hVdc,0)
SelectObject( m.hVdc, ABC )
*BitBlt( m.hVdc, 0,0,lnWidth,lnHeight, m.hDesktopDc,0,0, SRCCOPY )

m.hToken = 0
m.iInputBuf = 0h01 + REPLICATE( CHR(0),15 )
m.iResult = -1
IF ( 0 == GdiplusStartup( @ m.hToken, @ m.iInputBuf, 0 ))
m.hGdipBitmap = 0
IF ( 0 == GdipCreateBitmapFromHBITMAP( ;
m.hBitmap, 0, @ m.hGdipBitmap ))
m.cEncoder = ICASE( ;
'jpg' == m.cFileExtName, 0h01, ;
'gif' == m.cFileExtName, 0h02, ;
'tif' == m.cFileExtName, 0h05, ;
'png' == m.cFileExtName, 0h06, 0h00 ) ;
+ 0hF47C55041AD3119A730000F81EF32E
m.iResult = GdipSaveImageToFile( ;
m.hGdipBitmap, ;
STRCONV( m.tcFile+CHR(0), 5 ), ;
m.cEncoder, 0 )
GdipDisposeImage( m.hGdipBitmap )
ENDIF
GdiplusShutdown( m.hToken )
ENDIF

DeleteObject( m.hBitmap )
DeleteDC( m.hVdc )
ReleaseDC( 0, m.hDesktopDc )
...全文
277 点赞 收藏 9
写回复
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
sych11 2020-09-29
Declare Integer GetDefaultPrinter In winspool.drv String @pszBuffer, Integer @pcchBuffer DECLARE LONG GdipCreateFromHDC IN GDIPLUS LONG hdc, LONG @graphics DECLARE INTEGER FindWindow IN user32 STRING lpClassName,STRING lpWindowName DECLARE INTEGER GetDC IN user32 INTEGER hwnd Declare SHORT GetWindowRect IN user32 INTEGER hwnd, STRING @ lpRect DECLARE Long GetWindowDC IN WIN32API Long hWnd DECLARE Long ReleaseDC IN WIN32API Long hWnd, Long hDc DECLARE Long CreateCompatibleDC IN WIN32API Long hDc DECLARE Long DeleteDC IN WIN32API Long hDc DECLARE Long CreateCompatibleBitmap IN WIN32API Long hDc, Long nWidth, Long nHeight DECLARE Long BitBlt IN WIN32API Long hDestDC, Long nXDest, Long nYDest, Long nWidth, Long nHeight, Long hSrcDC, Long nXSrc, Long nYSrc, Long dwRop DECLARE Long SelectObject IN WIN32API Long hDc, Long hObject DECLARE Long DeleteObject IN WIN32API Long hObject DECLARE Long GdiplusStartup IN gdiplus Long @ token, String @ inputbuf, Long @ outputbuf DECLARE Long GdiplusShutdown IN gdiplus Long token DECLARE Long GdipCreateBitmapFromHBITMAP IN gdiplus Long hbitmap, Long hpalette, Long @ hGpBitmap DECLARE Long GdipDisposeImage IN gdiplus Long image DECLARE Long GdipSaveImageToFile IN gdiplus Long nImage, String FileName, String clsIdEncoder, Long encoderParams DECLARE LONG CreateDC IN win32api STRING @cDriver, STRING @cDevice, STRING cOutput,STRING cInitData DECLARE INTEGER StartDoc IN win32api INTEGER hdc, STRING @ lpdi DECLARE INTEGER EndDoc IN win32api INTEGER hdc DECLARE INTEGER StartPage IN win32api INTEGER hdc DECLARE INTEGER EndPage IN win32api INTEGER hdc DECLARE LONG GdipDrawImage IN GDIPLUS LONG graphics, LONG nImage, SINGLE x, SINGLE y **以上的API 不是全部用上 只是复制进来,懒得删除了! DECLARE INTEGER PrintWindow IN user32 INTEGER,INTEGER ,INTEGER #define SRCCOPY 0x00CC0020 LOCAL cFileExtName, cEncoder, iInputBuf, iResult LOCAL hDesktopDc, hVDc, hBitmap, hToken, hGdipBitmap m.hWnd=FindWindow("Notepad",NULL) &&截取一个文本窗口试一试 m.hWnd=_vfp.hwnd tcFile=LOWER('d:\aaa.JPG') &&jpg 格式图像 m.cFileExtName =LOWER( JUSTEXT( m.tcFile )) lpRect = REPLI (Chr(0), 16) GetWindowRect (m.hWnd, @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')&&窗口高度 m.hDesktopDc = GetDC( m.hWnd ) m.hVdc = CreateCompatibleDC( m.hDesktopDc ) m.hBitmap = CreateCompatibleBitmap( m.hDesktopDc,lnWidth,lnHeight ) ABC=SelectObject( m.hVdc, m.hBitmap ) i=PrintWindow(m.hWnd,m.hVdc,0) SelectObject( m.hVdc, ABC ) *BitBlt( m.hVdc, 0,0,lnWidth,lnHeight, m.hDesktopDc,0,0, SRCCOPY ) m.hToken = 0 m.iInputBuf = 0h01 + REPLICATE( CHR(0),15 ) m.iResult = -1 IF ( 0 == GdiplusStartup( @ m.hToken, @ m.iInputBuf, 0 )) m.hGdipBitmap = 0 IF ( 0 == GdipCreateBitmapFromHBITMAP( m.hBitmap, 0, @ m.hGdipBitmap )) IF "5.0"$OS() lcDriver="WINSPOOL" ELSE lcDriver=NULL ENDIF nBufsize = 250 prtname = Replicate(Chr(0), nBufsize) GetDefaultPrinter(@prtname, @nBufsize) prtname = STREXTRACT(prtname, "", Chr(0)) hprnDC = CreateDC(lcDriver, prtname, NULL, null) lcDocInfo = Chr(20) + Repli(Chr(0), 19) StartDoc(hPrnDC, @lcDocInfo) StartPage(hprndc) graphics=0 GdipCreateFromHDC(hprndc, @graphics) GdipDrawImage(graphics,m.hGdipBitmap,0,0) EndPage(hPrnDC) EndDoc(hPrnDC) GdipDisposeImage( m.hGdipBitmap ) ENDIF GdiplusShutdown( m.hToken ) ENDIF DeleteObject( m.hBitmap ) DeleteDC( m.hVdc ) ReleaseDC( 0, m.hDesktopDc ) 这个可以直接打印
回复
kmyz_yyl 2020-09-28
谢谢! 我原本设想不经过打印窗口实现直接打印,由于技术有限没能实现那种方法,最后也采用了=ShellExecute(0, "print",tcfile,null, "",1)这个函数来实现。
回复
sych11 2020-09-24
DECLARE INTEGER FindWindow IN user32 STRING lpClassName,STRING lpWindowName DECLARE INTEGER GetDC IN user32 INTEGER hwnd Declare SHORT GetWindowRect IN user32 INTEGER hwnd, STRING @ lpRect DECLARE Long GetWindowDC IN WIN32API Long hWnd DECLARE Long ReleaseDC IN WIN32API Long hWnd, Long hDc DECLARE Long CreateCompatibleDC IN WIN32API Long hDc DECLARE Long DeleteDC IN WIN32API Long hDc DECLARE Long CreateCompatibleBitmap IN WIN32API Long hDc, Long nWidth, Long nHeight DECLARE Long BitBlt IN WIN32API Long hDestDC, Long nXDest, Long nYDest, Long nWidth, Long nHeight, Long hSrcDC, Long nXSrc, Long nYSrc, Long dwRop DECLARE Long SelectObject IN WIN32API Long hDc, Long hObject DECLARE Long DeleteObject IN WIN32API Long hObject DECLARE Long GdiplusStartup IN gdiplus Long @ token, String @ inputbuf, Long @ outputbuf DECLARE Long GdiplusShutdown IN gdiplus Long token DECLARE Long GdipCreateBitmapFromHBITMAP IN gdiplus Long hbitmap, Long hpalette, Long @ hGpBitmap DECLARE Long GdipDisposeImage IN gdiplus Long image DECLARE Long GdipSaveImageToFile IN gdiplus Long nImage, String FileName, String clsIdEncoder, Long encoderParams Declare INTEGER ShellExecute IN shell32.DLL INTEGER HWND,; STRING lpszOP, ; STRING lpszFile, ; STRING lpszParams, ; STRING lpszDir, ; INTEGER fsshowcmd **以上的API 不是全部用上 只是复制进来,懒得删除了! DECLARE INTEGER PrintWindow IN user32 INTEGER,INTEGER ,INTEGER #define SRCCOPY 0x00CC0020 LOCAL cFileExtName, cEncoder, iInputBuf, iResult LOCAL hDesktopDc, hVDc, hBitmap, hToken, hGdipBitmap m.hWnd=FindWindow("Notepad",NULL) &&截取一个文本窗口试一试 m.hWnd=_vfp.hwnd tcFile=LOWER('d:\aaa.JPG') &&jpg 格式图像 m.cFileExtName =LOWER( JUSTEXT( m.tcFile )) lpRect = REPLI (Chr(0), 16) GetWindowRect (m.hWnd, @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')&&窗口高度 m.hDesktopDc = GetDC( m.hWnd ) m.hVdc = CreateCompatibleDC( m.hDesktopDc ) m.hBitmap = CreateCompatibleBitmap( m.hDesktopDc,lnWidth,lnHeight ) ABC=SelectObject( m.hVdc, m.hBitmap ) i=PrintWindow(m.hWnd,m.hVdc,0) SelectObject( m.hVdc, ABC ) *BitBlt( m.hVdc, 0,0,lnWidth,lnHeight, m.hDesktopDc,0,0, SRCCOPY ) m.hToken = 0 m.iInputBuf = 0h01 + REPLICATE( CHR(0),15 ) m.iResult = -1 IF ( 0 == GdiplusStartup( @ m.hToken, @ m.iInputBuf, 0 )) m.hGdipBitmap = 0 IF ( 0 == GdipCreateBitmapFromHBITMAP( ; m.hBitmap, 0, @ m.hGdipBitmap )) m.cEncoder = ICASE( ; 'jpg' == m.cFileExtName, 0h01, ; 'gif' == m.cFileExtName, 0h02, ; 'tif' == m.cFileExtName, 0h05, ; 'png' == m.cFileExtName, 0h06, 0h00 ) ; + 0hF47C55041AD3119A730000F81EF32E m.iResult = GdipSaveImageToFile( ; m.hGdipBitmap, ; STRCONV( m.tcFile+CHR(0), 5 ), ; m.cEncoder, 0 ) GdipDisposeImage( m.hGdipBitmap ) ENDIF GdiplusShutdown( m.hToken ) IF m.iResult =0 =ShellExecute(0, "print",tcfile,null, "",1) endif ENDIF DeleteObject( m.hBitmap ) DeleteDC( m.hVdc ) ReleaseDC( 0, m.hDesktopDc ) 这个可以打印
回复
sych11 2020-09-24
没明白是打印截屏还是打印屏幕上的图片文件,反正最终都是打印文件,所以ShellExecute是最快捷的解决方式
回复
zoumian888 2020-09-18
如果要实现点击就打印,还要将以上打印按钮的代码用到TIMER事件中,让TIMER事件在后台检测鼠标的范围,如果在打开的"Notepad"范围,并且执行CLICK事件时执行截屏打印操作。
回复
zoumian888 2020-09-18
你的程序我调试了一下,发现以上代码没有问题,代码是用来生成截屏文件的,不是用来打印截屏的,运行以上代码能正常生成截屏文件'd:\aaa.JPG',如果你要达到点击就打印的功能,显然以上代码还远远不够,最起码要有一个打开"Notepad"文件,然后再设计一个截屏的按钮,将按钮代码写上上面的代码,同时在代码的后面应该再加上将'd:\aaa.JPG'图片文件打印出来的代码。
回复
kmyz_yyl 2020-07-20
那一句已经改为 m.hWnd=thisform.hWnd,主要原因是代码没有任何反应,也不报错
回复
antony521 2020-07-20
m.hWnd=FindWindow("Notepad",NULL) &&截取一个文本窗口试一试 这一句窗口名与你要截取的相对应
回复
kmyz_yyl 2020-07-18
用了一个ShellExecute笨办法先暂时过渡一下,期待各位大师的指点!
回复
相关推荐
发帖
VFP
创建于2007-09-28

2491

社区成员

VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
申请成为版主
帖子事件
创建了帖子
2020-07-11 04:16
社区公告
暂无公告