Memory problem when working with Desktop DC!

Posted on 2005-04-29
Last Modified: 2008-03-10

Please check the following program.
It's painting form's background to color of the pixel at current mouse coordinates.
Option Explicit

Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long

Private Type POINTAPI
    x As Long
    y As Long
End Type

Private Sub Form_Load()

End Sub

Private Sub Timer1_Timer()
    Dim Color As Long
    Dim Dot As POINTAPI
    GetCursorPos Dot
    Color = GetPixel(GetWindowDC(GetDesktopWindow), Dot.x, Dot.y)
    'Color = GetPixel(GetWindowDC(0), Dot.x, Dot.y)
    Me.BackColor = Color
End Sub

The problem is that, when running, program is continuously consuming memory. (Task Manager -> Memory Usage)
I've discovered that every time I call  GetWindowDC(GetDesktopWindow), program consumes more memory.

I've considered capturing DesktopDC only once at Form_Load, but if I then press Ctrl+Alt+Del, the desktop DC changes so the old DC doesn't work any more.
So, even this way, I still have to capture desktop DC inside a timer to make sure it's not changed.

Why is this happening and how can I fix it?

I'm using Visual Basic 6.0 on Win2000 proffessional.

Question by:dbrckovi
    LVL 85

    Accepted Solution

    Try releasing your DC when you are done with it:

    Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

    Private Sub Timer1_Timer()
        Dim Color As Long
        Dim Dot As POINTAPI
        Dim deskWnd As Long
        Dim deskDC As Long
        deskWnd = GetDesktopWindow()
        deskDC = GetWindowDC(deskWnd)
        GetCursorPos Dot
        Color = GetPixel(deskDC, Dot.x, Dot.y)
        ReleaseDC deskWnd, deskDC
        Me.BackColor = Color
    End Sub
    LVL 11

    Author Comment


    It's working now.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now