'使用Bitblt等api作图函数时,如果源图片不完全显示,会导致不能实现功能
'借助内存的设备场景可解决此类问题
Private Sub Command1_Click()
'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 Const SRCCOPY = &HCC0020
'
'Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
'Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
'
'Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Picture1.ScaleMode = 3
Picture2.ScaleMode = 3
Randomize
'用Bitblt函数,把图片框2的部分内容贴到图片框1
'用选择框进行选择
If chkDC.Value = vbUnchecked Then '不使用内存设备场景
'一般的写法,如果图片框2的图片不完全显示,会影响功能
BitBlt Picture1.hdc, Rnd * Picture1.Width, Rnd * Picture1.Height, Rnd * Picture2.Width, Rnd * Picture2.Height, Picture2.hdc, Rnd * Picture2.Width, Rnd * Picture2.Height, SRCCOPY
Else '使用内存设备场景
' Picture2.Visible = False '不可见或者图片不完全显示都不影响
Dim SourceDC As Long
'建立一个与图片框picture2兼容的内存设备场景,返回其句柄
SourceDC = CreateCompatibleDC(Picture2.hdc)
'把图片框picture2的图片选入这个内存设备场景
SelectObject SourceDC, Picture2.Picture.Handle
'用这个内存设备场景代替图片框picture2
BitBlt Picture1.hdc, Rnd * Picture1.Width, Rnd * Picture1.Height, Rnd * Picture2.Width, Rnd * Picture2.Height, SourceDC, Rnd * Picture2.Width, Rnd * Picture2.Height, SRCCOPY
'删除这个内存设备场景
DeleteDC SourceDC
End If
End Sub