GetHandle example?

I’ve been getting a “”“wxPyDeprecationWarning: Call to deprecated item. Use GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)”"" warning for a drawing canvas on Windows. GetHandle appears not to be a drop-in replacement for GetHDC, so I was I was wondering if there was an example somewhere on the proper way to use this now.

Cheers, Eric

If I had to guess, I'd say you *probably* just need to convert it to an int, try:
self.draw(int(window_dc.GetHandle()), x, y, width, height)

Scott

···

On Thu, 27 Apr 2017, braidedlogix@gmail.com wrote:

I've been getting a """wxPyDeprecationWarning: Call to deprecated item. Use
GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)"""
warning for a drawing canvas on Windows. GetHandle appears not to be a
drop-in replacement for GetHDC, so I was I was wondering if there was an
example somewhere on the proper way to use this now.

Sadly - no that doesn’t do it. It complains about the number being too large and does not update the canvas.

Scott Talbert wrote:

I've been getting a """wxPyDeprecationWarning: Call to deprecated
item. Use
GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)"""
warning for a drawing canvas on Windows. GetHandle appears not to be a
drop-in replacement for GetHDC, so I was I was wondering if there was an
example somewhere on the proper way to use this now.

If I had to guess, I'd say you *probably* just need to convert it to
an int, try:
self.draw(int(window_dc.GetHandle()), x, y, width, height)

Scott

Sadly - no that doesn't do it. It complains about the number being too
large and does not update the canvas.

Here's probably what is happening. GetHandle is returning a void* which
is returned to Python as a siplib.voidptr object. Since it is
representing a memory address then it will treat it as an unsigned value
when converting to an integer in its __int__() method. However if that
unsigned value is greater than the maxint value then it will return a
Python Long instead of an Int. When you try to use that Long for
whatever it is that needs the HDC it is complaining because it wont
convert the Long back into a handle.

If you're not using a 64-bit build of Python and wxPython I would try
using that first. Otherwise there is probably a way to convert the
value back into a C long with ctypes or something.

···

On Thu, 27 Apr 2017, braidedlogix@gmail.com wrote:

--
Robin Dunn
Software Craftsman

Robin Dunn wrote:

Scott Talbert wrote:
I've been getting a """wxPyDeprecationWarning: Call to deprecated
item. Use
GetHandle instead. self.draw(window_dc.GetHDC(), x, y, width, height)"""
warning for a drawing canvas on Windows. GetHandle appears not to be a
drop-in replacement for GetHDC, so I was I was wondering if there was an
example somewhere on the proper way to use this now.

If I had to guess, I'd say you *probably* just need to convert it to
an int, try:
self.draw(int(window_dc.GetHandle()), x, y, width, height)
Scott

Sadly - no that doesn't do it. It complains about the number being too
large and does not update the canvas.

Here's probably what is happening. GetHandle is returning a void* which
is returned to Python as a siplib.voidptr object. Since it is
representing a memory address then it will treat it as an unsigned value
when converting to an integer in its __int__() method. However if that
unsigned value is greater than the maxint value then it will return a
Python Long instead of an Int. When you try to use that Long for
whatever it is that needs the HDC it is complaining because it wont
convert the Long back into a handle.
If you're not using a 64-bit build of Python and wxPython I would try
using that first. Otherwise there is probably a way to convert the
value back into a C long with ctypes or something.

I’ve started some changes for a future release (not the next one as that
is already in the pipeline) that will make this a little easier. We’ve got mapped types for wxIntPtr and wxUIntPtr, which are integer types guaranteed to be large enough to hold a pointer value, which is what is needed for GetHandle. I’ll switch the wrappers over to use that type instead and it should behave more like before but will be safe for cross
platform and also different architectures.

···

braidedlogix@gmail.com

Robin Dunn

Software Craftsman

http://wxPython.org

The following works to get rid of deprecation warning and accomplish the same thing as GetHDC():

window_dc = wx.PaintDC(window)
#old
#window_dc.GetHDC()
#new
import ctypes
ctypes.c_ulong(window_dc.GetHandle()).value

Thanks for pointing out the underlying type! I would never have thought to use ctypes here.

Probably the GetHandle should be adjusted to be drop-in replacement or the deprecation warning could be changed to point out the necessary conversion.

Cheers,Eric