可以啊。代码如下:
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Sub My_Screen_1() '整个屏幕
Call keybd_event(vbKeySnapshot, 0, 0, 0)
DoEvents
End Sub
Sub My_Screen_2() '活动窗口
Call keybd_event(vbKeySnapshot, 1, 1, 1)
DoEvents
End Sub
功能:运行 My_Screen_1 就是截取整个屏幕,运行 My_Screen_2 就是仅截取当前活动窗口。
原理: 调用API函数模拟键盘上的PrtSc键(印屏幕)
上面的代码我是跑过OK后才发的(我是放在模块里的)。估计你混入中文符号了。算了,我还是去掉注释吧:
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Sub My_Screen_1()
Call keybd_event(vbKeySnapshot, 0, 0, 0)
DoEvents
End Sub
Sub My_Screen_2()
Call keybd_event(vbKeySnapshot, 1, 1, 1)
DoEvents
End Sub
可以了,想再进一步,每10分钟截取一次,并放入d:下,能做到吗,谢了
会再加分给你,麻烦了!
从剪贴板获取图像比较难。我写了一段,不过好像保存的时候有点问题:
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Private Declare Function GetClipboardData Lib "user32" (ByVal wFormat As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As Guid, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Const CF_BITMAP = 2
Private Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Type Guid
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Sub My_Screen_1()
Call keybd_event(vbKeySnapshot, 0, 0, 0)
DoEvents
End Sub
Private Sub My_Screen_2()
Call keybd_event(vbKeySnapshot, 1, 1, 1)
DoEvents
End Sub
Sub Scheduled_Snapshot()
Dim Pic As PicBmp, IPic As IPicture, IID_IDispatch As Guid
Call My_Screen_1
oTime = Now()
OpenClipboard 0
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With Pic
.Size = Len(Pic)
.Type = 1
.hBmp = GetClipboardData(CF_BITMAP)
End With
OleCreatePictureIndirect Pic, IID_IDispatch, 1, IPic
stdole.SavePicture IPic, "d:\snapshot_" & Year(oTime) & Month(oTime) & Day(oTime) _
& "_" & Hour(oTime) & Minute(oTime) & Second(oTime) & ".bmp"
CloseClipboard
Sleep 600000
End Sub
就运行这个 Scheduled_Snapshot 。
或者,有时间你自己看看这个帖子吧:
http://www.officefans.net/cdb/viewthread.php?tid=103805
Private Declare Sub keybd_event Lib "user32" _
(ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Sub My_Screen_1() '整个屏幕
Call keybd_event(vbKeySnapshot, 0, 0, 0)
DoEvents
End Sub
Sub My_Screen_2() '活动窗口
Call keybd_event(vbKeySnapshot, 1, 1, 1)
DoEvents
End Sub
这段代码不能放在模块里面的.只能放在某一张表的代码里面才行.
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Sub Command1_Click()
Me.AutoRedraw = True
BitBlt Me.hDC, 0, 0, Screen.Width, Screen.Height, GetDC(0), 0, 0, vbSrcCopy
SavePicture Me.Image, "D:\截图.bmp" '保存路径,是bmp图片
Me.Clear
End Sub