'---------------------------------------------------------------------------- ' 'Author:lihonggen0 'Date:2002-6-19 '功能:抓屏 '---------------------------------------------------------------------------- Private Type POINTAPI x As Long y As Long End Type 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 Long Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As LongPrivate Sub Command1_Click() Dim hdc As Long Dim sw As Integer Dim sh As Integer Dim CurPos As POINTAPI Dim Cur As Long Me.Hide DoEvents Picture1.AutoRedraw = True hdc = GetDC(0) GetCursorPos CurPos Cur = GetCursor Picture1.Width = Screen.Width Picture1.Height = Screen.Height sw = Screen.Width / Screen.TwipsPerPixelX sh = Screen.Height / Screen.TwipsPerPixelY BitBlt Picture1.hdc, 0, 0, sw, sh, hdc, 0, 0, vbSrcCopy Me.Show DrawIcon Picture1.hdc, CurPos.x - 10, CurPos.y - 10, Cur ReleaseDC 0, hdc Picture1.AutoRedraw = FalseEnd Sub
得到单击的句柄,那么你就可以用GetDC(hwnd),然后再bitblt就可以了。
获当前活动窗口,模块: Option ExplicitType RECT_Type left As Long top As Long right As Long bottom As LongEnd Type'The following declare statements are case sensitive.Declare Function GetActiveWindow Lib "User32" () As Long Declare Function GetDesktopWindow Lib "User32" () As Long Declare Sub GetWindowRect Lib "User32" (ByVal Hwnd As Long, _ lpRect As RECT_Type) Declare Function GetDC Lib "User32" (ByVal Hwnd As Long) As Long 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 SelectObject Lib "Gdi32" (ByVal hdc As Long, _ ByVal hObject As Long) As Long 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 Declare Function OpenClipboard Lib "User32" (ByVal Hwnd As Long) As Long Declare Function EmptyClipboard Lib "User32" () As Long Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, _ ByVal hMem As Long) As Long Declare Function CloseClipboard Lib "User32" () As Long Declare Function ReleaseDC Lib "User32" (ByVal Hwnd As Long, _ ByVal hdc As Long) As Long Declare Function DeleteDC Lib "Gdi32" (ByVal hdc As Long) As LongGlobal Const SRCCOPY = &HCC0020 Global Const CF_BITMAP = 2
Function ScreenDump() Dim AccessHwnd As Long, DeskHwnd As Long Dim hdc As Long Dim hdcMem As Long Dim rect As RECT_Type Dim junk As Long Dim fwidth As Long, fheight As Long Dim hBitmap As Long ' DoCmd.Hourglass True '--------------------------------------------------- ' Get window handle to Windows and Microsoft Access '--------------------------------------------------- DeskHwnd = GetDesktopWindow() AccessHwnd = GetActiveWindow() '--------------------------------------------------- ' Get screen coordinates of Microsoft Access '--------------------------------------------------- Call GetWindowRect(AccessHwnd, rect) fwidth = rect.right - rect.left fheight = rect.bottom - rect.top '--------------------------------------------------- ' Get the device context of Desktop and allocate memory '--------------------------------------------------- hdc = GetDC(DeskHwnd) hdcMem = CreateCompatibleDC(hdc) hBitmap = CreateCompatibleBitmap(hdc, fwidth, fheight) If hBitmap <> 0 Then junk = SelectObject(hdcMem, hBitmap) '--------------------------------------------- ' Copy the Desktop bitmap to memory location ' based on Microsoft Access coordinates. '--------------------------------------------- junk = BitBlt(hdcMem, 0, 0, fwidth, fheight, hdc, rect.left, _ rect.top, SRCCOPY) '--------------------------------------------- ' Set up the Clipboard and copy bitmap '--------------------------------------------- junk = OpenClipboard(DeskHwnd) junk = EmptyClipboard() junk = SetClipboardData(CF_BITMAP, hBitmap) junk = CloseClipboard() End If '--------------------------------------------- ' Clean up handles '--------------------------------------------- junk = DeleteDC(hdcMem) junk = ReleaseDC(DeskHwnd, hdc) ' DoCmd.Hourglass FalseEnd Function窗体调用ScreenDump,将抓的图放入Clipboard() 然后在画图中粘贴一下
然后用getbitblt api 就行
'
'Author:lihonggen0
'Date:2002-6-19
'功能:抓屏
'----------------------------------------------------------------------------
Private Type POINTAPI
x As Long
y As Long
End Type
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 Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function DrawIcon Lib "user32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal hIcon As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As LongPrivate Sub Command1_Click()
Dim hdc As Long
Dim sw As Integer
Dim sh As Integer
Dim CurPos As POINTAPI
Dim Cur As Long
Me.Hide
DoEvents
Picture1.AutoRedraw = True
hdc = GetDC(0)
GetCursorPos CurPos
Cur = GetCursor
Picture1.Width = Screen.Width
Picture1.Height = Screen.Height
sw = Screen.Width / Screen.TwipsPerPixelX
sh = Screen.Height / Screen.TwipsPerPixelY
BitBlt Picture1.hdc, 0, 0, sw, sh, hdc, 0, 0, vbSrcCopy
Me.Show
DrawIcon Picture1.hdc, CurPos.x - 10, CurPos.y - 10, Cur
ReleaseDC 0, hdc
Picture1.AutoRedraw = FalseEnd Sub
Option ExplicitType RECT_Type left As Long
top As Long
right As Long
bottom As LongEnd Type'The following declare statements are case sensitive.Declare Function GetActiveWindow Lib "User32" () As Long
Declare Function GetDesktopWindow Lib "User32" () As Long
Declare Sub GetWindowRect Lib "User32" (ByVal Hwnd As Long, _
lpRect As RECT_Type)
Declare Function GetDC Lib "User32" (ByVal Hwnd As Long) As Long
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 SelectObject Lib "Gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
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
Declare Function OpenClipboard Lib "User32" (ByVal Hwnd As Long) As Long
Declare Function EmptyClipboard Lib "User32" () As Long
Declare Function SetClipboardData Lib "User32" (ByVal wFormat As Long, _
ByVal hMem As Long) As Long
Declare Function CloseClipboard Lib "User32" () As Long
Declare Function ReleaseDC Lib "User32" (ByVal Hwnd As Long, _
ByVal hdc As Long) As Long
Declare Function DeleteDC Lib "Gdi32" (ByVal hdc As Long) As LongGlobal Const SRCCOPY = &HCC0020
Global Const CF_BITMAP = 2
Function ScreenDump()
Dim AccessHwnd As Long, DeskHwnd As Long
Dim hdc As Long
Dim hdcMem As Long
Dim rect As RECT_Type
Dim junk As Long
Dim fwidth As Long, fheight As Long
Dim hBitmap As Long ' DoCmd.Hourglass True '---------------------------------------------------
' Get window handle to Windows and Microsoft Access
'---------------------------------------------------
DeskHwnd = GetDesktopWindow()
AccessHwnd = GetActiveWindow() '---------------------------------------------------
' Get screen coordinates of Microsoft Access
'---------------------------------------------------
Call GetWindowRect(AccessHwnd, rect)
fwidth = rect.right - rect.left
fheight = rect.bottom - rect.top '---------------------------------------------------
' Get the device context of Desktop and allocate memory
'---------------------------------------------------
hdc = GetDC(DeskHwnd)
hdcMem = CreateCompatibleDC(hdc)
hBitmap = CreateCompatibleBitmap(hdc, fwidth, fheight) If hBitmap <> 0 Then
junk = SelectObject(hdcMem, hBitmap) '---------------------------------------------
' Copy the Desktop bitmap to memory location
' based on Microsoft Access coordinates.
'---------------------------------------------
junk = BitBlt(hdcMem, 0, 0, fwidth, fheight, hdc, rect.left, _
rect.top, SRCCOPY) '---------------------------------------------
' Set up the Clipboard and copy bitmap
'---------------------------------------------
junk = OpenClipboard(DeskHwnd)
junk = EmptyClipboard()
junk = SetClipboardData(CF_BITMAP, hBitmap)
junk = CloseClipboard()
End If '---------------------------------------------
' Clean up handles
'---------------------------------------------
junk = DeleteDC(hdcMem)
junk = ReleaseDC(DeskHwnd, hdc) ' DoCmd.Hourglass FalseEnd Function窗体调用ScreenDump,将抓的图放入Clipboard()
然后在画图中粘贴一下