Jump to content
Sign in to follow this  
JohnOne

_WinAPI_GetDC related

Recommended Posts

JohnOne

I'm not so sure about what exactly hdc is.

My question and code example might help to ask my question.

#include <WinAPI.au3>

$_GetPixel = _GetPixel(10, 10)

ConsoleWrite($_GetPixel &@LF)

Func _GetPixel($iX, $iY)
    $hDC = _WinAPI_GetDC(0)

    $aGetPixel = DllCall("Gdi32.dll", "dword", "GetPixel", "handle", $hDC, "int", $iX, "int", $iY)
    If @error Then
        Exit MsgBox(0, "DllCall Error", @error)
    EndIf

    $rtn = StringRight(Hex($aGetPixel[0]), 6)

    Return $rtn
EndFunc   ;==>_GetPixel

Code returns pixel color at screen coords.

Should I make the call to _WinAPI_GetDC each time my function is called or is it safe to have it called once globally, given that screen content can obviously change?

I'm not after a debate on the pro's and con's of global variables, and could just as well be static. Just whether the device context remains the same throughout the life of the script

I have tested this but I want to be sure.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
KaFu

Afaik the DC stays the same as long as the hWnd stays the same, so as long as the desktop hwnd does not change, _WinAPI_GetDC(0) should stay the same and can be obtained one time globally. To free resources _WinAPI_ReleaseDC(0, $hDC) should also be called after usage, and btw., just saw in the MSDN doc comments that this will only get you the DC of the default monitor screen in a multi-monitor set-up, see said docs for a trick to get DC for whole screen in a multi-monitor set-up.

  • Like 1

Share this post


Link to post
Share on other sites
JohnOne

Thanks for the clarification KaFu.

Do you know what the relationship between desktop dc and desktop hwnd is?

For example If I try to perform a control* function on hwnd, it operates only on the desktop window.

If I perform GetPixel using the DC, I get the colour of anything visible on the whole screen regardless of what the window might be.


AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
KaFu

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×