天呀,呵呵,楼上两位大哥说得方法不错!! 我有一个笨方法,就是用vb代码写的: Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) ' 一个抓屏的例子 Const VK_SNAPSHOT As Byte = &H2C ' 把应用窗口图象放到剪贴板:Private Sub Command1_Click() Call keybd_event(VK_SNAPSHOT, 0, 0, 0) ' 把整个屏幕抓到剪贴板: End SubPrivate Sub Command2_Click() Call keybd_event(VK_SNAPSHOT, 1, 0, 0) '可以用该方法抓 AVI 图象。 End Sub然后保存图片,比较复杂,我用了一个笨办法,没有什么艺术感 如果没有高手来接着发表,我再把剩下代码贴出来。
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long Private Declare Function EmptyClipboard Lib "user32" () As Long Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc 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 Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) As Long Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Private Declare Function CloseClipboard Lib "user32" () As Long 函数: Sub ScrnCap(Lt, Top, Rt, Bot) rWidth = Rt - Lt rHeight = Bot - Top SourceDC = CreateDC("DISPLAY", 0, 0, 0) DestDC = CreateCompatibleDC(SourceDC) Bhandle = CreateCompatibleBitmap(SourceDC, rWidth, rHeight) SelectObject DestDC, Bhandle BitBlt DestDC, 0, 0, rWidth, rHeight, SourceDC, Lt, Top, &HCC0020 Wnd = Screen.ActiveForm.hwnd OpenClipboard Wnd EmptyClipboard SetClipboardData 2, Bhandle CloseClipboard DeleteDC DestDC ReleaseDC Dhandle, SourceDC End Sub 以下的示例把屏幕图象捕捉后,放到Picture1 中。 Sub Command1_Click() Form1.Visible = False ScrnCap 0, 0, 640, 480 Form1.Visible = True picture1 = Clipboard.GetData() End Sub
建个模块代码如下:Private Type PALETTEENTRY peRed As Byte peGreen As Byte peBlue As Byte peFlags As Byte End TypePrivate Type LOGPALETTE palVersion As Integer palNumEntries As Integer palPalEntry(255) As PALETTEENTRY End TypePrivate Type GUID Data1 As Long Data2 As Integer Data3 As Integer Data4(7) As Byte End TypePrivate Const RASTERCAPS As Long = 38 Const RC_PALETTE As Long = &H100 Const SIZEPALETTE As Long = 104 Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long Declare Function BitBlt Lib "gdi32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long Declare Function GetForegroundWindow Lib "USER32" () As Long Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long Declare Function GetWindowDC Lib "USER32" (ByVal hwnd As Long) As Long Declare Function GetDC Lib "USER32" (ByVal hwnd As Long) As Long Declare Function GetWindowRect Lib "USER32" (ByVal hwnd As Long, lpRect As RECT) As Long Declare Function ReleaseDC Lib "USER32" (ByVal hwnd As Long, ByVal hdc As Long) As Long Declare Function GetDesktopWindow Lib "USER32" () As Long Type PicBmp Size As Long Type As Long hBmp As Long hPal As Long Reserved As Long End Type Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture Dim r As Long Dim Pic As PicBmp
Dim IPic As IPicture Dim IID_IDispatch As GUID With IID_IDispatch .Data1 = &H20400 .Data4(0) = &HC0 .Data4(7) = &H46 End With With Pic .Size = Len(Pic) .Type = vbPicTypeBitmap .hBmp = hBmp .hPal = hPal End With r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic) Set CreateBitmapPicture = IPic End Function Public Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture Dim hDCMemory As Long Dim hBmp As Long Dim hBmpPrev As Long Dim r As Long Dim hDCSrc As Long Dim hPal As Long Dim hPalPrev As Long Dim RasterCapsScrn As Long Dim HasPaletteScrn As Long Dim PaletteSizeScrn As Long Dim LogPal As LOGPALETTE If Client Then hDCSrc = GetDC(hWndSrc) Else hDCSrc = GetWindowDC(hWndSrc)
End If hDCMemory = CreateCompatibleDC(hDCSrc) hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc) hBmpPrev = SelectObject(hDCMemory, hBmp) RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) HasPaletteScrn = RasterCapsScrn And RC_PALETTE
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
LogPal.palVersion = &H300 LogPal.palNumEntries = 256 r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0)) hPal = CreatePalette(LogPal) hPalPrev = SelectPalette(hDCMemory, hPal, 0) r = RealizePalette(hDCMemory) End If r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy) hBmp = SelectObject(hDCMemory, hBmpPrev) If HasPaletteScrn And (PaletteSizeScrn = 256) Then hPal = SelectPalette(hDCMemory, hPalPrev, 0) End If r = DeleteDC(hDCMemory) r = ReleaseDC(hWndSrc, hDCSrc) Set CaptureWindow = CreateBitmapPicture(hBmp, hPal) End FunctionPublic Function CaptureForm(frmSrc As Form) As Picture Set CaptureForm = CaptureWindow(frmSrc.hwnd, False, 0, 0, frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels)) End Function Public Function CaptureClient(frmSrc As Form) As Picture Set CaptureClient = CaptureWindow(frmSrc.hwnd, True, 0, 0, frmSrc.ScaleX(frmSrc.ScaleWidth, frmSrc.ScaleMode, vbPixels), frmSrc.ScaleY(frmSrc.ScaleHeight, frmSrc.ScaleMode, vbPixels)) End FunctionPublic Function CaptureActiveWindow() As Picture Dim hWndActive As Long Dim r As Long Dim RectActive As RECT hWndActive = GetForegroundWindow() r = GetWindowRect(hWndActive, RectActive) Set CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, RectActive.Right - RectActive.Left, RectActive.Bottom - RectActive.Top) End Function 然后在窗体内写个函数: Private Function CaptureScreen() As Picture On Error Resume Next Dim hWndScreen As Long hWndScreen = GetDesktopWindow() Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Str$(GetSystemMetrics(SM_CXSCREEN)), Str$(GetSystemMetrics(SM_CYSCREEN))) End Function要截屏的时候调用 CaptureScreen 函数就好了啊 如:Image1.picture=CaptureScreen() 然后用savepicture方法保存不就好了吗?
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 Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As LongConst SRCCOPY = &HCC0020Private Sub Form_Activate() Me.Hide For i = 1 To 10000 '延时 DoEvents Next i BitBlt Picture1.hDC, 0, 0, Screen.Width / 15, Screen.Height / 15, GetDC(0), 0, 0, SRCCOPY SavePicture Picture1.Image, "c:\sss.bmp" End End SubPrivate Sub Form_Load() With Picture1 .AutoRedraw = True .Width = Screen.Width .Height = Screen.Height End With End Sub
TO:jisheng(古朴的狼) 你的方法最好,不过还有更短的程序: Option Explicit Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long) Const VK_SNAPSHOT As Byte = &H2C Private Sub Command1_Click() Call keybd_event(VK_SNAPSHOT, 0, 0, 0) DoEvents SavePicture Clipboard.GetData, "c:\temp.bmp" End Sub 不用Picture控件,程序会更小,把它做成OCX会很有用的。
我有一个笨方法,就是用vb代码写的:
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
' 一个抓屏的例子
Const VK_SNAPSHOT As Byte = &H2C
' 把应用窗口图象放到剪贴板:Private Sub Command1_Click()
Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
' 把整个屏幕抓到剪贴板:
End SubPrivate Sub Command2_Click()
Call keybd_event(VK_SNAPSHOT, 1, 0, 0)
'可以用该方法抓 AVI 图象。
End Sub然后保存图片,比较复杂,我用了一个笨办法,没有什么艺术感
如果没有高手来接着发表,我再把剩下代码贴出来。
Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function EmptyClipboard Lib "user32" () As Long
Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc 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 Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CloseClipboard Lib "user32" () As Long
函数:
Sub ScrnCap(Lt, Top, Rt, Bot)
rWidth = Rt - Lt
rHeight = Bot - Top
SourceDC = CreateDC("DISPLAY", 0, 0, 0)
DestDC = CreateCompatibleDC(SourceDC)
Bhandle = CreateCompatibleBitmap(SourceDC, rWidth, rHeight)
SelectObject DestDC, Bhandle
BitBlt DestDC, 0, 0, rWidth, rHeight, SourceDC, Lt, Top, &HCC0020
Wnd = Screen.ActiveForm.hwnd
OpenClipboard Wnd
EmptyClipboard
SetClipboardData 2, Bhandle
CloseClipboard
DeleteDC DestDC
ReleaseDC Dhandle, SourceDC
End Sub
以下的示例把屏幕图象捕捉后,放到Picture1 中。
Sub Command1_Click()
Form1.Visible = False
ScrnCap 0, 0, 640, 480
Form1.Visible = True
picture1 = Clipboard.GetData()
End Sub
peRed As Byte
peGreen As Byte
peBlue As Byte
peFlags As Byte
End TypePrivate Type LOGPALETTE
palVersion As Integer
palNumEntries As Integer
palPalEntry(255) As PALETTEENTRY
End TypePrivate Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End TypePrivate Const RASTERCAPS As Long = 38
Const RC_PALETTE As Long = &H100
Const SIZEPALETTE As Long = 104 Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Declare Function BitBlt Lib "gdi32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function GetForegroundWindow Lib "USER32" () As Long
Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function GetWindowDC Lib "USER32" (ByVal hwnd As Long) As Long
Declare Function GetDC Lib "USER32" (ByVal hwnd As Long) As Long
Declare Function GetWindowRect Lib "USER32" (ByVal hwnd As Long, lpRect As RECT) As Long
Declare Function ReleaseDC Lib "USER32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Declare Function GetDesktopWindow Lib "USER32" () As Long Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Public Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
Dim r As Long Dim Pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With Pic
.Size = Len(Pic)
.Type = vbPicTypeBitmap
.hBmp = hBmp
.hPal = hPal
End With
r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
Set CreateBitmapPicture = IPic
End Function Public Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim LogPal As LOGPALETTE
If Client Then
hDCSrc = GetDC(hWndSrc)
Else
hDCSrc = GetWindowDC(hWndSrc)
End If hDCMemory = CreateCompatibleDC(hDCSrc) hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)
HasPaletteScrn = RasterCapsScrn And RC_PALETTE
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
LogPal.palVersion = &H300
LogPal.palNumEntries = 256
r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
hPal = CreatePalette(LogPal)
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
r = RealizePalette(hDCMemory)
End If
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
hBmp = SelectObject(hDCMemory, hBmpPrev)
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End FunctionPublic Function CaptureForm(frmSrc As Form) As Picture
Set CaptureForm = CaptureWindow(frmSrc.hwnd, False, 0, 0, frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels))
End Function
Public Function CaptureClient(frmSrc As Form) As Picture
Set CaptureClient = CaptureWindow(frmSrc.hwnd, True, 0, 0, frmSrc.ScaleX(frmSrc.ScaleWidth, frmSrc.ScaleMode, vbPixels), frmSrc.ScaleY(frmSrc.ScaleHeight, frmSrc.ScaleMode, vbPixels))
End FunctionPublic Function CaptureActiveWindow() As Picture
Dim hWndActive As Long
Dim r As Long
Dim RectActive As RECT
hWndActive = GetForegroundWindow()
r = GetWindowRect(hWndActive, RectActive)
Set CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, RectActive.Right - RectActive.Left, RectActive.Bottom - RectActive.Top)
End Function
然后在窗体内写个函数:
Private Function CaptureScreen() As Picture
On Error Resume Next
Dim hWndScreen As Long
hWndScreen = GetDesktopWindow()
Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Str$(GetSystemMetrics(SM_CXSCREEN)), Str$(GetSystemMetrics(SM_CYSCREEN)))
End Function要截屏的时候调用 CaptureScreen 函数就好了啊
如:Image1.picture=CaptureScreen()
然后用savepicture方法保存不就好了吗?
先放一个picture控件,把截取在剪切板的图片放进去
Picture1.Picture = Clipboard.GetData然后用把picture的图片保存起来(bmp格式)
SavePicture Picture1, "c:\temp.bmp"
呵呵,方法很烦,让各位见笑了,谁有更好的方法,就把分数给他好了,我也当作学习。
(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 Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As LongConst SRCCOPY = &HCC0020Private Sub Form_Activate()
Me.Hide
For i = 1 To 10000 '延时
DoEvents
Next i
BitBlt Picture1.hDC, 0, 0, Screen.Width / 15, Screen.Height / 15, GetDC(0), 0, 0, SRCCOPY
SavePicture Picture1.Image, "c:\sss.bmp"
End
End SubPrivate Sub Form_Load()
With Picture1
.AutoRedraw = True
.Width = Screen.Width
.Height = Screen.Height
End With
End Sub
你的方法最好,不过还有更短的程序:
Option Explicit
Private Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal bScan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Const VK_SNAPSHOT As Byte = &H2C
Private Sub Command1_Click()
Call keybd_event(VK_SNAPSHOT, 0, 0, 0)
DoEvents
SavePicture Clipboard.GetData, "c:\temp.bmp"
End Sub
不用Picture控件,程序会更小,把它做成OCX会很有用的。
哇,厉害!msdn里面骗我说savepicture的第一个参数是picture或者image的名字!!
谢谢你,我又学了一招
实在太过分了!这么短的程序都可以做得出来!嗯,有前途啊!我喜欢!
有人使用剪贴板扩充软件,可以在内存中保存多个剪贴板的内容,你一个 PrintScreen 就是 800x600 一幅 Bitmap,要是多来几次……呵呵……哎呀!内存条冒烟了……
hDCScreen = GetDC(GetDesktopWindow)
With picScreen
.Width = Screen.Width / Screen.TwipsPerPixelX
.Height = Screen.Height / Screen.TwipsPerPixelY
End With
With Screen
BitBlt picScreen.hDC, 0, 0, .Width / .TwipsPerPixelX, .Height / .TwipsPerPixelY, _
hDCScreen, 0, 0, SRCCOPY
End With
DeleteDC hDCScreen