Results 1 to 6 of 6

Thread: Does DeleteDC() even work ?

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    Does DeleteDC() even work ?

    Correct me if I'm wrong, but surely the code below should be able to run indefinitely, as it is deleting the DC right after it creates it ??

    If not, then how does one actually recover the lost resources ?

    VB Code:
    1. Option Explicit
    2.  
    3. 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
    4. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    5. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDc As Long) As Long
    6. Private Declare Function DeleteDC Lib "gdi32" (ByVal hDc As Long) As Long
    7. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    8. Private Declare Function SelectObject Lib "gdi32" (ByVal hDc As Long, ByVal hObject As Long) As Long
    9. Private Declare Sub GlobalMemoryStatus Lib "kernel32" (lpBuffer As MEMORYSTATUS)
    10.  
    11. Private Type MEMORYSTATUS
    12.     dwLength As Long
    13.     dwMemoryLoad As Long
    14.     dwTotalPhys As Long
    15.     dwAvailPhys As Long
    16.     dwTotalPageFile As Long
    17.     dwAvailPageFile As Long
    18.     dwTotalVirtual As Long
    19.     dwAvailVirtual As Long
    20. End Type
    21.  
    22.  
    23. Private Function GenerateDC(FileName As String) As Long
    24.     Dim DC As Long, picTemp As IPictureDisp
    25.     DC = CreateCompatibleDC(0)
    26.     If DC < 1 Then
    27.         Exit Function
    28.     End If
    29.     Set picTemp = LoadPicture(FileName)
    30.     SelectObject DC, picTemp
    31.     DeleteObject picTemp
    32.     Set picTemp = Nothing
    33.     GenerateDC = DC
    34. End Function
    35.  
    36. Private Sub Form_Load()
    37.     Dim i As Long
    38.     Do
    39.         DoEvents
    40.         Debug.Print "Memory Free : " & (x.dwAvailPhys / 1024) & "Kb  :  " & IIf(DeleteDC(GenerateDC("c:\boilogo.gif")) <> 0, "DeleteDC() Worked", "DeleteDC() Failed")
    41.         DoEvents
    42.     Loop
    43. End Sub
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  2. #2
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Yes it works fine. I also had long time problems with
    that crappy thing but finally I found a solution to load
    bitmaps and create memory DCs and after all releas
    all resources. So heres what to do:

    Code:
    Dim DC As Long
    Dim Temp As IPictureDisp
    Dim Original As Long
    
    'Load the bitmap
    DC = CreateCompatibleDC(MainWindow.hDC)
    Set Temp = LoadPicture(FileName)
    Original = SelectObject(DC, Temp)
    
    '---
    'Here you can use your bitmap
    'BitBlt etc.
    'The whole game you can say :)
    '---
    
    'Release again
    SelectObject DC, Original
    DeleteObject Temp
    DeleteDC DC
    Now this should work fine

  3. #3

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359
    Fox, if I was a puff I'd kiss you.
    Thanks for that.

    I'll try it at home later this evening.

    Cheers,
    Jamie.
    Microsoft MVP : Visual Developer - Visual Basic [2004-2005]

  4. #4
    Addicted Member
    Join Date
    May 2000
    Location
    Mexico City
    Posts
    242

    Re: Does DeleteDC() even work ?

    Originally posted by plenderj
    VB Code:
    1. If DC < 1 Then
    Correct me if I'm wrong, but, Don't DCs can be negative too? I think you should change that "< 1" to "= 0".

  5. #5
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    My pleasure plenderj


    Oh and gxpark:

    Yes technically =0 would have the same effect in any case BUT I DAMN DO NOT CARE ABOUT THAT *hehe*

    Have fun

  6. #6
    Addicted Member Janus's Avatar
    Join Date
    Aug 2001
    Location
    California
    Posts
    221
    Yes, if you read the API docs you have to release all objects allocated to the DC.
    "1 4m 4 1337 #4xz0r!'
    Janus

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width