If hBitmap = 0 Then ''''Failure in loading bitmap
DeleteDC MemDC
Exit Function
End If
''''Throw the Bitmap into the Device Context
SelectObject MemDC, hBitmap
''''Return OK
GenerateDC = 1
End Function
If hBitmap = 0 Then ''''Failure in loading bitmap
DeleteDC MemDC
Exit Function
End If
''''Throw the Bitmap into the Device Context
SelectObject MemDC, hBitmap
''''Return OK
GenerateDC = 1
End Function
A device context created with the CreateCompatibleDC must be deleted by calling the DeleteDC API function and the Bitmap handle returned from LoadImage much be deleted with the DeleteObject API. So let''''s create a reusable function for this called DeleteGeneratedDC. This function takes two arguments, a DC to be deleted and the Bitmap Handle for the corresponding DC.
Private Function DeleteGeneratedDC(hBitmap As Long, MemDC As Long) As Long
DeleteGeneratedDC = DeleteDC(MemDC)
DeleteObject hBitmap
End Function
Run the sample project. Press the Load bitmap button. The bitmaps are now loaded and ready for use. We use the usual BltBit function to Blit them from the memory context and into the device context of the form. Press the Draw the sprite button, and observe how the sprite is blitted transparently onto the form.
One word of advice when using this scheme to create a memory device context: Be observant of the scope of the variables which you store the device contexts in. If the variable goes out of scope, and you have not deleted it with the DeleteDC function, you抣l start losing resources. So always delete the created device contexts to be on the safe side.
End of Part II of the three part Drawing and Animation Tutorial by Burt Abreu & S鴕en Skov. Download all Samples]
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!