这是我一个程序里的代码段,运行之后发现内存占用会不断增加。谁有办法帮我解决一下啊?!这是不是
getpixel 函数的BUG啊?!Option Explicit'API声明
Private Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Long, 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 Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long'API类型定义
Private Type POINTAPI
    x As Long
    y As Long
End Type
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type'取某点颜色
Public Function getColor(ByVal newX As Long, ByVal newY As Long) As Long
    Dim windowRect As RECT
    Dim x As Long
    Dim y As Long
    Dim windowDc
    Dim apiP As POINTAPI
    Dim color As Long
    GetWindowRect 0, windowRect
    x = windowRect.Left + newX
    y = windowRect.Top + newY
    apiP.x = x
    apiP.y = y
    windowDc = GetDC(0)
    ScreenToClient 0, apiP
    color = GetPixel(windowDc, apiP.x, apiP.y)
    getColor = color
End FunctionPrivate Sub Form_Load()End SubPrivate Sub Timer1_Timer()
    Dim color As Long
    color = getColor(367, 293)
    Cls
    Print color
End Sub

解决方案 »

  1.   

    哈哈,不是getpixel的bug,而是你的程序有bug,而且是严重的bug
    看看我以前是这样被老大指点的 回复人: laviewpbt(pbt) 
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Type POINTAPI
            x As Long
            y As Long
    End Type
    Private Sub Timer1_Timer()
    Dim a As Long
    Dim pp As POINTAPI
    Dim dd As Long
    a = GetDC(0)
    GetCursorPos pp
    dd = GetPixel(a, pp.x, pp.y)
    Text1.Text = Abs(dd And &HFF)
    Text2.Text = Abs(dd And 65280) / 256
    Text3.Text = Abs(dd And &HFF0000) / 65536
    End Sub回复人: rainstormmaster(暴风雨 v2.0) 
    //程序中的影像控件好像和这两个函数有冲突,运行几次后就乱了。
    你调用getdc之后没用ReleaseDC函数释放设备场景,windows没罢工就不错了原来的帖子见http://community.csdn.net/Expert/topic/3323/3323609.xml?temp=.9377558
      

  2.   

    所以在你的getcolor函数的最后一定要加上releasedc(windowDc)。以释放资源
    一般getdc与releasedc对应使用
    createdc与deletedc对应使用
      

  3.   

    是这样的.ReleaseDC
    不过,我发现我的程序(以前写的.有类似语句)在win98中会内存泄漏,
    我好像调用了什么DrawRect之类的函数.
    winXP就没事.看来,winXP内存管理功能要好的多