views:

363

answers:

2

I am getting a DC for a window handle of an object in another program using win32gui.GetDC which returns an int/long. I need to blit this DC into a memory DC in python. The only thing I can't figure out how to do is get a wxDC derived object from the int/long that win32gui returns. None of the wxDC objects allow me to pass an actual DC handle to them from what I can tell. This of course keeps me from doing my blit. Is there any way to do this?

A: 

From what I can tell, DCs in python are abstracted due to platform variation. So a device context in python doesn't directly map to a device context in Windows even though many of the methods are direct Windows method calls. To make this happen it appears you would need to make your own DelegateDC class or something similar that is intended just for Windows so that you could set the DC handle directly.

There might also be some way to attach a wxWindow to the window handle after which you could get a wxWindowDC from the wxWindow...can't figure this out though.

flxkid
I needed to do something similar recently and came to the same conclusion, but I only spent a few hours on it so I'm still hoping there's a way.
FogleBird
+1  A: 

I downloaded the wxWidgets source and dug around, and I think this will work.

You need the handle (HWND) for the external window, not the DC.

window = wx.Frame(None, -1, '')
window.AssociateHandle(hwnd)
dc = wx.WindowDC(window)
FogleBird