Search code examples
pythonpython-2.7wxpythonwxwidgets

wxPython - Get existing windowDC/handle using wx python module?


I've been looking into using the wx python package on Windows 7. Specifically, I would like to be able to get the image data of a existing window (i.e. not a window opened by the python program). It appears I can do this by getting the window's device context, creating a compatible device context, copying the bitmap, and then using it how I like from there. The problem is that I can't seem to find the way to get the device context (or the handle) of an existing window. I only see ways to get them from windows the python program using wx created. How might I be able to go about doing this? Thank you much!


Solution

  • wx may not have a way to do this.

    The Windows APIs you need are pretty simple, and you can use them through win32api (or ctypes if you prefer, but that's a lot more work).

    First, I don't know how you're planning to identify the window you want. If you have its class and name, you can just FindWindow (or, if it may not be a top-level window, FindWindowEx). If you want to search by something else, you will probably need to call EnumWindow (plus EnumChildWindows recursively, if you're not sure it's a top-level window).

    At this point, you can just call wx.Windows.AssociateHandle to attach a wx.Window object to the HWND.

    If you can't do that for whatever reason, GetDC gives you a display context for an HWND. You can then create a memory DC, or get the DC for the native window under underlying your wx window, and BitBlt from one to the other.