|
-
Sep 20th, 2000, 08:05 PM
#1
Thread Starter
Junior Member
grab colors?
i'm currently using the following code to get the color of the pixel the mouse is over...
Code:
On Error Resume Next
Dim cursorpos As POINTAPI
Dim rDC As Long, rPixel As Long
Call GetCursorPos(cursorpos)
rDC& = GetDC(0&)
rPixel& = GetPixel(rDC&, cursorpos.X, cursorpos.Y)
Call ReleaseDC(0&, rDC&)
color& = rPixel&
It works fine except for there's a minor problem. When i use it in a timer or a loop for a longish period of time (like anywhere between 4-10 minutes), the program starts lagging my computer. It seems to be eating up memory somehow. Anyone know how i might change this code so it could run for hours on end without any trouble with lag?
thanks.
-
Sep 20th, 2000, 08:19 PM
#2
Fanatic Member
The loop is obviously where the mem is being eaten. If you want a constant reading of the pixel color how about placing it in the MouseMove event.
Gl,
D!m
-
Sep 20th, 2000, 08:25 PM
#3
Hyperactive Member
Here's a thought
I've not really had to get into the depths of Windows Device Contexts so I am no authority.
However, it occurs to me that you might be slowing down because perhaps Windows releases DC's quite slowly. If the DC is not released internally within windows by the time you are calling for the next DC, then eventually you will "run out" and so the API call will return 0.
I note that you do not check for this in your code. Adding some debugging would be helpful to verify if what I am suspecting is in fact true.
If it is, then one fix would be to exit if you could not Get a DC.
I think that in your situation, you would be better to use CreateDC because then you don't need to release it until your program is finished. Calling GetDC followed by ReleaseDC within milliseconds, and repeating this cycle many times seems wrong in my gut...
But like I said I have not experimented in this area so you'll have to try a few things out.
Lastly, I see the On Error Resume Next statement. This is not a good idea because it is masking the real problem. I suspect it is there because you were sometimes getting an rDC& of 0 which is not valid (guessing here) for the call to GetPixel.
I hope by suggestions help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|