Reputation: 12384
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!
Upvotes: 1
Views: 1165
Reputation: 76762
Just to expand on the other answer, try this code (untested, I'm not on Windows right now)...
# first use FindWindow or FindWindowEx to determine window handle
frame = wx.Frame(None)
frame.AssociateHandle(handle)
dc = wx.ClientDC(frame)
width, height = dc.GetSize()
bitmap = wx.EmptyBitmap(width, height)
mdc = wx.MemoryDC(bitmap)
mdc.Blit(0, 0, width, height, dc, 0, 0)
del mdc
bitmap.SaveFile('output.png', wx.BITMAP_TYPE_PNG)
Upvotes: 1
Reputation: 365657
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.
Upvotes: 2