本范例是以一個stdPicture物件來存圖形,之後於PictureBox中以特殊效果來顯示。因為我們想顯示的只有一個圖,所以不想多用另一個PictureBox來存原始圖,而後再畫到另一個PictureBox上,那只有用StdPicture物件來取代PictureBox(存來源圖),但是BitBlt這個繪圖函式需來源與目的的hDc,而StdPicture物件沒有hDc,它只有一個Handle值,以本例來說,這Handle值便是圖形的hBitmap值。所以我們只好使用MemoryDC的方式來做,產生一個MemoryDc後將BitMap圖放於其上,之後便可以使用BitBlt來繪圖了。'需求一個PictureBox(Namedpicture2),一個Command按鍵)
OptionExplicit
PrivateDeclareFunctionBitBltLib"gdi32"(ByValhDestDCAsLong,ByValxAsLong,ByValyAsLong,ByValnWidthAsLong,ByValnHeightAsLong,ByValhSrcDCAsLong,ByValxSrcAsLong,ByValySrcAsLong,ByValdwRopAsLong)AsLong
PrivateDeclareFunctionCreateCompatibleDCLib"gdi32"(ByValhdcAsLong)AsLong
PrivateDeclareFunctionSelectObjectLib"gdi32"(ByValhdcAsLong,ByValhObjectAsLong)AsLong
PrivateDeclareFunctionDeleteDCLib"gdi32"(ByValhdcAsLong)AsLong
ConstSRCCOPY=&HCC0020
PrivatePicture1AsNewStdPicture
PrivateSubCommand1_Click()
DimiAsLong
DimjAsLong
Dimheight5AsLong,width5AsLong
DimhMemDcAsLong
'stdPicture物件的度量單位是Himetric所以要轉換成Pixel
height5=ScaleY(Picture1.Height,vbHimetric,vbPixels)
Ifheight5>Picture2.ScaleHeightThen
height5=Picture2.ScaleHeight
EndIf
width5=ScaleX(Picture1.Width,vbHimetric,vbPixels)
Ifwidth5>Picture2.ScaleWidthThen
width5=Picture2.ScaleWidth
EndIf
'CreateMemoryDC
hMemDc=CreateCompatibleDC(Picture2.hdc)
'將Picture1的BitMap圖指定給hMemDc
CallSelectObject(hMemDc,Picture1.Handle)
Fori=height5To1Step-1
CallBitBlt(Picture2.hdc,0,i,width5,1,hMemDc,0,i,SRCCOPY)
Forj=i-1To1Step-1
CallBitBlt(Picture2.hdc,0,j,width5,1,hMemDc,0,i,SRCCOPY)
Nextj
Next
CallDeleteDC(hMemDc)
EndSub
PrivateSubForm_Load()
DimiAsLong
Picture2.ScaleMode=3'設定成Pixel的度量單位
'設定待Display的圖
SetPicture1=LoadPicture("c:windows素還真.bmp")
'^^^^^^^^^^^^^^^^^^^^^^
'Loadthepicturewewanttoshow
EndSub->