webbrowser.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # This demo uses the IE4 Web Browser control.
  2. # It catches an "OnNavigate" event, and updates the frame title.
  3. # (event stuff by Neil Hodgson)
  4. import win32ui, win32con, win32api, regutil
  5. from pywin.mfc import window, activex
  6. from win32com.client import gencache
  7. import sys
  8. WebBrowserModule = gencache.EnsureModule("{EAB22AC0-30C1-11CF-A7EB-0000C05BAE0B}", 0, 1, 1)
  9. if WebBrowserModule is None:
  10. raise ImportError("IE4 does not appear to be installed.")
  11. class MyWebBrowser(activex.Control, WebBrowserModule.WebBrowser):
  12. def OnBeforeNavigate2(self, pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel):
  13. self.GetParent().OnNavigate(URL)
  14. #print "BeforeNavigate2", pDisp, URL, Flags, TargetFrameName, PostData, Headers, Cancel
  15. class BrowserFrame(window.MDIChildWnd):
  16. def __init__(self, url = None):
  17. if url is None:
  18. self.url = regutil.GetRegisteredHelpFile("Main Python Documentation")
  19. if self.url is None:
  20. self.url = "http://www.python.org"
  21. else:
  22. self.url = url
  23. pass # Dont call base class doc/view version...
  24. def Create(self, title, rect = None, parent = None):
  25. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW
  26. self._obj_ = win32ui.CreateMDIChild()
  27. self._obj_.AttachObject(self)
  28. self._obj_.CreateWindow(None, title, style, rect, parent)
  29. rect = self.GetClientRect()
  30. rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
  31. self.ocx = MyWebBrowser()
  32. self.ocx.CreateControl("Web Browser", win32con.WS_VISIBLE | win32con.WS_CHILD, rect, self, 1000)
  33. self.ocx.Navigate(self.url)
  34. self.HookMessage (self.OnSize, win32con.WM_SIZE)
  35. def OnSize (self, params):
  36. rect = self.GetClientRect()
  37. rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
  38. self.ocx.SetWindowPos(0, rect, 0)
  39. def OnNavigate(self, url):
  40. title = "Web Browser - %s" % (url,)
  41. self.SetWindowText(title)
  42. def Demo(url=None):
  43. if url is None and len(sys.argv)>1:
  44. url = win32api.GetFullPathName(sys.argv[1])
  45. f = BrowserFrame(url)
  46. f.Create("Web Browser")
  47. if __name__=='__main__':
  48. Demo()