Newsgroup: it.comp.lang.visual-basic
Titolo: Api win 32 & VB

Salve a tutti,
ho un piccolo problema con le Api win32 che mi
affligge perchè non riesco a capire la soluzione.
Devo necessariamente usare le api per caricare da file
una bitmap e copiarle in un Picture control.
Il problema è che il codice realizzato funzione nel
visualizzare la bitmap ma se eseguito n-volte vedo
(mediante il monitor delle risorse) che le risorse GDI
diminuiscono senzibilmente sebbene rilascio sia lo
handler della bitmap (con DeleteObject) sia il device
contex (con DeleteDC).
Le risorse sono completamente rilasciate solo al
termine del programma.
Il codice è stato provato sia con VB5 sp3 che con VB6
sp6.
Grazie.
Private Sub Test()
Dim sFileName as String
Dim hImage as Long
Dim iImageDC as Long
Dim lResult as Long
Dim BitmapData As BITMAP    ' data on the incoming
bitmap
Dim iWidth as Integer
Dim iHeight as Integer
Dim i as Integer
sFileName = App.Path + "\Test.bmp"
i = 50
for i=1 to 50
	' call the LoadImage API to attempt to load in the
test bitmap.
	hImage = LoadImage(0, sFileName, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE)
	' if LoadImage fails, the return result is a zero
	If (hImage = 0) Then
		MsgBox "Error : Unable To Load Bitmap Image : " &
sFileName, _
       	        vbOKOnly, "Bitmap Load Error"
	       Exit Sub
	End If
	iImageDC = CreateCompatibleDC(0)
	' make sure there is new DC's to use in this bitmap
	If (iImageDC = 0) Then
		MsgBox "Error : Unable To CreatCompatibleDC"
	       DeletObject hImage
       	Exit Sub
	End If
	' save the actual bitmap within this bitmap object
	lResult = SelectObject(iImageDC, hImage)
	' make sure the object is selected
	If (lResult = 0) Then
		MsgBox "Error : Unable To SelectObject"
		DeleteDC iImageDC
       	DeletObject hImage
	       Exit Sub
	End If
	' verify the bitmap data
	lResult = GetObject(hImage, Len(BitmapData),
BitmapData)
	If (lResult = 0) Then
		MsgBox "Error : Unable To GetObject info"
		DeleteDC iImageDC
       	DeletObject hImage
	       Exit Sub
	End If
	' persist the height/width of the bitmap image
	iWidth = BitmapData.bmWidth     ' determine the
bitmaps width
	iHeight = BitmapData.bmHeight    ' determine the
bitmaps height
	' blit the image
	lResult = BitBlt(Picture1.hdc, 0, 0, iWidth, iHeight,
iImageDC, 0, 0, SRCCOPY)
	'Delete hImage
	lResult = DeleteObject(hImage)
	If (lResult = 0) Then
		MsgBox "Error : Unable To DeleteObject"
	       Exit Sub
	End If
	'Delete DC
	lResult =  DeleteDC(iImageDC) 
	If (lResult = 0) Then
		MsgBox "Error : Unable To DeleteDC"
	       Exit Sub
	End If
Next i
End Sub
 
' constants used in this example are declared here
Public Const SRCAND = &H8800C6     ' used to determine
how a blit will  turn out
Public Const SRCCOPY = &HCC0020    ' used to determine
how a blit will turn out
Public Const SRCERASE = &H440328   ' used to determine
how a blit will turn out
Public Const SRCINVERT = &H660046  ' used to determine
how a blit will turn out
Public Const SRCPAINT = &HEE0086   ' used to determine
how a blit will turn out
Public Const IMAGE_BITMAP = &O0    ' used with
LoadImage to load a bitmap
Public Const LR_LOADFROMFILE = 16  ' used with
LoadImage
' structures used in this example
    Type BITMAP
       bmType As Long
       bmWidth As Long
       bmHeight As Long
       bmWidthBytes As Long
       bmPlanes As Integer
       bmBitsPixel As Integer
       bmBits As Long
    End Type
'API's used in this example
Declare Function GetObject Lib "gdi32" Alias
"GetObjectA"  (ByVal hObject As Long, _
		ByVal nCount As Long, lpObject As Any) As Long
Declare Function DeleteObject Lib "gdi32" (ByVal
hObject As Long) As Long
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal
hdc As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As
Long) As Long
Declare Function SelectObject Lib "gdi32" ByVal hdc As
Long, ByVal hObject As Long) As Long
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
Declare Function SetBkColor Lib "gdi32" (ByVal hdc As
Long, ByVal crColor As Long) As Long
Declare Function SetTextColor Lib "gdi32" (ByVal hdc
As Long, ByVal crColor As Long) As Long
Declare Function LoadImage Lib "user32" Alias
"LoadImageA"  (ByVal hInst As Long, _ 
		ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As
Long, ByVal n2 As Long, _
		ByVal un2 As Long) As Long
Declare Function CreateCompatibleBitmap Lib "gdi32" 
(ByVal hdc As Long, _
		ByVal nWidth As Long, ByVal nHeight As Long) As Long
______________________________________________________________________
Do You Yahoo!?
Il tuo indirizzo gratis e per sempre @
yahoo.it
 su 
http://mail.yahoo.it
-- 
Posted from 
web611.mail.yahoo.com
 [216.115.104.95] 
via Mailgate.ORG Server - 
http://www.Mailgate.ORG

 una bitmap e copiarle in un Picture control.
Ma usare LoadPicture non andava bene? Se la risposta è no allora cancella la
risposta 8-))
Ciao VB