office交流網--QQ交流群號

Access培訓群:792054000         Excel免費交流群群:686050929          Outlook交流群:221378704    

Word交流群:218156588             PPT交流群:324131555

VBA無鬚PictureBox的全API截屏代碼

2020-04-30 08:00:00
sunruiyeyipeng
轉貼
3393
無鬚PictureBox的全API截屏代碼

Option Explicit
'//////////////////////////////////////////////////////////////////////////////
'@@summary 支持全屏截屏
'@@require 無
'@@reference 代碼部分來源網絡,我進行瞭組閤構建,以及刪減調試
'@@license MIT
'@@author sunruiyeyipeng@163.com
'@@create 2016年4月28日14:10:43
'@@modify
'//////////////////////////////////////////////////////////////////////////////




'//////////////////////////////////////////////////////////////////////////////
'//
'// 私有聲明
'//
'//////////////////////////////////////////////////////////////////////////////


'------------------------------------------------------------------------------
' 私有數據類型
'------------------------------------------------------------------------------
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Type EncoderParameter
GUID As GUID
NumberOfValues As Long
Type As Long
Value As Long
End Type
Private Type EncoderParameters
Count As Long
Parameter As EncoderParameter
End Type


Private Type PicBmp
Size As Long
Type As Long
hbmp As Long
hPal As Long
Reserved As Long
End Type
'------------------------------------------------------------------------------
' 私有API
'------------------------------------------------------------------------------
Private Declare Function GdiplusStartup Lib "GDIPlus" (token As Long, inputbuf As GdiplusStartupInput, ByVal outputbuf As Long) As Long
Private Declare Function GdiplusShutdown Lib "GDIPlus" (ByVal token As Long) As Long
Private Declare Function GdipCreateBitmapFromHBITMAP Lib "GDIPlus" (ByVal hbm As Long, ByVal hPal As Long, Bitmap As Long) As Long
Private Declare Function GdipDisposeImage Lib "GDIPlus" (ByVal Image As Long) As Long
Private Declare Function GdipSaveImageToFile Lib "GDIPlus" (ByVal Image As Long, ByVal filename As Long, clsidEncoder As GUID, encoderParams As Any) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal str As Long, id As GUID) As Long
Private Declare Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As Long, Bitmap As Long) As Long


Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, 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 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 SelectObject Lib "gdi32" _
(ByVal hdc As Long, ByVal hObject 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 Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
'//////////////////////////////////////////////////////////////////////////////
'//
'// 類
'//
'//////////////////////////////////////////////////////////////////////////////




'------------------------------------------------------------------------------
' 初始化
'------------------------------------------------------------------------------
Private Sub Class_Initialize()


End Sub


'------------------------------------------------------------------------------
' 銷譭
'------------------------------------------------------------------------------
Private Sub Class_Terminate()


End Sub


'//////////////////////////////////////////////////////////////////////////////
'//
'// 私有方法
'//
'//////////////////////////////////////////////////////////////////////////////
Private Function PictureBoxSaveJPG(ByVal pict As StdPicture, ByVal filename As String, Optional ByVal quality As Byte = 80) As Boolean
Dim tSI As GdiplusStartupInput
Dim lRes As Long
Dim lGDIP As Long
Dim lBitmap As Long

'初始化 GDI+
tSI.GdiplusVersion = 1
lRes = GdiplusStartup(lGDIP, tSI, 0)

If lRes = 0 Then
'從句柄創建 GDI+ 圖像
lRes = GdipCreateBitmapFromHBITMAP(pict.Handle, 0, lBitmap)

If lRes = 0 Then
Dim tJpgEncoder As GUID
Dim tParams As EncoderParameters

'初始化解碼器的GUID標識
CLSIDFromString StrPtr("{557CF401-1A04-11D3-9A73-0000F81EF32E}"), tJpgEncoder

'設置解碼器蔘數
tParams.Count = 1
With tParams.Parameter ' Quality
'得到Quality蔘數的GUID標識
CLSIDFromString StrPtr("{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}"), .GUID
.NumberOfValues = 1
.Type = 4
.Value = VarPtr(quality)
End With

'保存圖像
lRes = GdipSaveImageToFile(lBitmap, StrPtr(filename), tJpgEncoder, tParams)

'銷譭GDI+圖像
GdipDisposeImage lBitmap
End If

'銷譭 GDI+
GdiplusShutdown lGDIP
End If

If lRes Then
PictureBoxSaveJPG = False
Else
PictureBoxSaveJPG = True
End If
End Function


'//////////////////////////////////////////////////////////////////////////////
'//
'// 公有方法
'//
'//////////////////////////////////////////////////////////////////////////////
Public Sub ClipScreenToFile(ByVal FilePath As String)
Dim pPicture As New StdPicture
Set pPicture = ClipToImage
PictureBoxSaveJPG pPicture, FilePath
End Sub


Private Function ClipToImage() As StdPicture
Dim screenDc As Long, width As Long, height As Long
Dim lPicDc As Long, lPicBmp As Long
Dim G As GUID, p As PicBmp
width = Screen.width / Screen.TwipsPerPixelX
height = Screen.height / Screen.TwipsPerPixelY

screenDc = GetDC(0)
lPicDc = CreateCompatibleDC(screenDc)
lPicBmp = CreateCompatibleBitmap(screenDc, width, height)
SelectObject lPicDc, lPicBmp
BitBlt lPicDc, 0, 0, width, height, screenDc, 0, 0, vbSrcCopy
With G
.Data1 = &H20400
.Data4(0) = &HC0&
.Data4(7) = &H46&
End With
With p
.Size = Len(p)
.Type = vbPicTypeBitmap
.hbmp = lPicBmp
.hPal = &H0&
End With
OleCreatePictureIndirect p, G, True, ClipToImage
ReleaseDC 0, screenDc
DeleteObject lPicBmp
End Function




使用方式
1、複製以上代碼
2、VB6中創建一箇類,刪除默認的代碼
3、以剛纔複製的代碼粘貼到新建的類中,將類命名爲CScreenCliper
4、代碼用法
Dim op As New CScreenCliper
op.ClipScreenToFile App.Path & "\demo.jpg"
Set op = Nothing
分享