Attachment 'WndProcHookMixin.py'

Download

   1 import win32gui
   2 import win32con
   3 import win32api
   4 
   5 class WndProcHookMixin:
   6     """
   7     This class can be mixed in with any wxWindows window class in order to hook it's WndProc function. 
   8     You supply a set of message handler functions with the function addMsgHandler. When the window receives that
   9     message, the specified handler function is invoked. If the handler explicitly returns False then the standard 
  10     WindowProc will not be invoked with the message. You can really screw things up this way, so be careful. 
  11     This is not the correct way to deal with standard windows messages in wxPython (i.e. button click, paint, etc) 
  12     use the standard wxWindows method of binding events for that. This is really for capturing custom windows messages
  13     or windows messages that are outside of the wxWindows world.
  14     """
  15     def __init__(self):
  16         self.msgDict = {}
  17 
  18     def hookWndProc(self):
  19         self.oldWndProc = win32gui.SetWindowLong(self.GetHandle(),
  20                                                  win32con.GWL_WNDPROC,
  21                                                  self.localWndProc)
  22     def unhookWndProc(self):
  23         # Notice the use of wxin32api instead of win32gui here.  This is to avoid an error due to not passing a 
  24         #callable object.
  25         win32api.SetWindowLong(self.GetHandle(),
  26                                win32con.GWL_WNDPROC,
  27                                self.oldWndProc)
  28 
  29     def addMsgHandler(self,messageNumber,messageName,handler):
  30         self.msgDict[messageNumber] = (messageName,handler)
  31 
  32     def localWndProc(self, hWnd, msg, wParam, lParam):
  33         # call the handler if one exists
  34         if self.msgDict.has_key(msg):
  35             # if the handler returns false, we terminate the message here
  36             if self.msgDict[msg][1](wParam,lParam) == False:
  37                 return
  38 
  39         # Restore the old WndProc on Destroy.  
  40         if msg == win32con.WM_DESTROY: self.unhookWndProc()
  41 
  42         # Pass all messages (in this case, yours may be different) on
  43         # to the original WndProc
  44         return win32gui.CallWindowProc(self.oldWndProc,
  45                                        hWnd, msg, wParam, lParam)
  46 
  47 # a simple example
  48 if __name__ == "__main__":
  49     import wx
  50     
  51     class MyFrame(wx.Frame,WndProcHookMixin):
  52         def __init__(self,parent):
  53             WndProcHookMixin.__init__(self)
  54             frameSize = wx.Size(640,480)
  55             wx.Frame.__init__(self,parent,-1,"Change my size and watch stdout",size=frameSize)
  56             
  57             # this is for demo purposes only, use the wxPython method for getting events 
  58             # on window size changes and other standard windowing messages
  59             WM_SIZE_value = getattr(win32con, "WM_SIZE")
  60             self.addMsgHandler(WM_SIZE_value, "WM_SIZE", self.onHookedSize)
  61             self.hookWndProc()
  62         
  63         def onHookedSize(self,wParam,lParam):
  64             print "WM_SIZE [WPARAM:%i][LPARAM:%i]"%(wParam,lParam)
  65             return True
  66 
  67     app = wx.PySimpleApp()
  68     frame = MyFrame(None)
  69     frame.Show(True)
  70     app.MainLoop()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2009-10-04 09:16:03, 3.1 KB) [[attachment:WndProcHookMixin.py]]
  • [get | view] (2009-10-04 09:16:03, 4.4 KB) [[attachment:WndProcHookMixinCtypes.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.