Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
1  
AssociateHandle can indeed be used to work with windows created outside of the Python application. –  FogleBird Nov 15 at 1:05
 
@FogleBird: Thanks! Let me edit the answer, because that makes things simpler. –  abarnert Nov 15 at 1:06
 
Ok. I was thinking this might be the case. I was trying to do what you're suggesting, but was running into problems as I am looking for in this question. However, I wasn't getting the solution there and I've been having trouble hunting that down on my own. That was when I found wx and thought it might be a solution. –  golmschenk Nov 15 at 1:07
 
Oh! Maybe the update of using AssociateHandle will work for me. Thanks! –  golmschenk Nov 15 at 1:09
 
Check my code in my answer for that. –  FogleBird Nov 15 at 1:10
add comment

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)
share|improve this answer
 
Also, check out my code here which draws snowflakes on the desktop - you can also see how to use ctypes to use the FindWindow function. github.com/fogleman/wxSnow/blob/master/wxsnow.py –  FogleBird Nov 15 at 1:17
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.