|
发表于 2024-12-27 13:24
来自手机
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
lsggjie 发表于 2024-12-27 08:46
有哪位大佬高手给解决一下吗?
Private Type PicBmp
Size As Long
type As Long
hbmp As LongPtr
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 Declare PtrSafe Function GetDesktopWindow _
Lib "user32" () As LongPtr
Private Declare PtrSafe Function GetDC Lib "user32" ( _
ByVal hWnd As LongPtr) As LongPtr
Private Declare PtrSafe Function ReleaseDC Lib "user32" ( _
ByVal hWnd As LongPtr, ByVal hDC As LongPtr) As LongPtr
Private Declare PtrSafe Function BitBlt Lib "gdi32" ( _
ByVal hDestDC As LongPtr, ByVal x As Long, ByVal y As Long, _
ByVal nWidth As Long, ByVal nHeight As Long, _
ByVal hSrcDC As LongPtr, ByVal xSrc As Long, ByVal ySrc As Long, _
ByVal dwRop As Long) As Long
Private Declare PtrSafe Function CreateCompatibleDC Lib "gdi32" ( _
ByVal hDC As LongPtr) As LongPtr
Private Declare PtrSafe Function CreateCompatibleBitmap _
Lib "gdi32" (ByVal hDC As LongPtr, ByVal nWidth As Long, _
ByVal nHeight As Long) As Long
Private Declare PtrSafe Function SelectObject Lib "gdi32" ( _
ByVal hDC As LongPtr, ByVal hObject As LongPtr) As LongPtr
Private Declare PtrSafe Function DeleteDC Lib "gdi32" ( _
ByVal hDC As LongPtr) As LongPtr
Private Declare PtrSafe Function DeleteObject Lib "gdi32" ( _
ByVal hObject As LongPtr) As LongPtr
'office32位:"olepro32.dll"
Private Declare PtrSafe Function OleCreatePictureIndirect _
Lib "oleaut32.dll" (PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, Ipic As IPicture) As Long
'定义vbSrcCopy常量
Private Const vbSrcCopy As Long = &HCC0020
Sub CaptureScreenArea()
' On Error Resume Next
Dim leftPos As Long, topPos As Long
Dim width As Long, height As Long
leftPos = 100 ' 截取区域的左边界
topPos = 100 ' 截取区域的上边界
width = 400 ' 截取区域的宽度
height = 300 ' 截取区域的高度
Dim hWnd As LongPtr
hWnd = GetDesktopWindow()
Dim hDC As LongPtr
hDC = GetDC(hWnd)
Dim memDC As LongPtr
memDC = CreateCompatibleDC(hDC)
Dim hBitmap As LongPtr
hBitmap = CreateCompatibleBitmap(hDC, width, height)
Dim oldBitmap As LongPtr
oldBitmap = SelectObject(memDC, hBitmap)
BitBlt memDC, 0, 0, width, height, hDC, leftPos, topPos, vbSrcCopy
Dim Sliid As GUID, hpic As PicBmp
With Sliid
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With hpic
.Size = Len(hpic)
.type = 1
.hbmp = hBitmap
.hPal = 0
End With
Dim Ipic As IPictureDisp, pfile As String
OleCreatePictureIndirect hpic, Sliid, 1, Ipic
pfile = ThisWorkbook.Path & "\screenshot.bmp"
SavePicture Ipic, pfile
DeleteObject hBitmap
DeleteDC memDC
ReleaseDC hWnd, hDC
End Sub |
|