toolbar.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Demo of ToolBars
  2. # Shows the toolbar control.
  3. # Demos how to make custom tooltips, etc.
  4. import win32ui
  5. import win32con
  6. import win32api
  7. from pywin.mfc import docview, window, afxres
  8. import commctrl
  9. class GenericFrame(window.MDIChildWnd):
  10. def OnCreateClient(self, cp, context):
  11. # handlers for toolbar buttons
  12. self.HookCommand (self.OnPrevious, 401)
  13. self.HookCommand (self.OnNext, 402)
  14. # Its not necessary for us to hook both of these - the
  15. # common controls should fall-back all by themselves.
  16. # Indeed, given we hook TTN_NEEDTEXTW, commctrl.TTN_NEEDTEXTA
  17. # will not be called.
  18. self.HookNotify(self.GetTTText, commctrl.TTN_NEEDTEXT)
  19. self.HookNotify(self.GetTTText, commctrl.TTN_NEEDTEXTW)
  20. # parent = win32ui.GetMainFrame()
  21. parent = self
  22. style = win32con.WS_CHILD | win32con.WS_VISIBLE | \
  23. afxres.CBRS_SIZE_DYNAMIC | afxres.CBRS_TOP | afxres.CBRS_TOOLTIPS | afxres.CBRS_FLYBY
  24. buttons = (win32ui.ID_APP_ABOUT,win32ui.ID_VIEW_INTERACTIVE)
  25. bitmap = win32ui.IDB_BROWSER_HIER
  26. tbid = 0xE840
  27. self.toolbar = tb = win32ui.CreateToolBar (parent, style, tbid)
  28. tb.LoadBitmap(bitmap)
  29. tb.SetButtons(buttons)
  30. tb.EnableDocking(afxres.CBRS_ALIGN_ANY)
  31. tb.SetWindowText("Test")
  32. parent.EnableDocking(afxres.CBRS_ALIGN_ANY)
  33. parent.DockControlBar(tb)
  34. parent.LoadBarState("ToolbarTest")
  35. window.MDIChildWnd.OnCreateClient(self, cp, context)
  36. return 1
  37. def OnDestroy(self, msg):
  38. self.SaveBarState("ToolbarTest")
  39. def GetTTText(self, std, extra):
  40. (hwndFrom, idFrom, code) = std
  41. text, hinst, flags = extra
  42. if flags & commctrl.TTF_IDISHWND:
  43. return # Not handled
  44. if (idFrom==win32ui.ID_APP_ABOUT):
  45. # our 'extra' return value needs to be the following
  46. # entries from a NMTTDISPINFO[W] struct:
  47. # (szText, hinst, uFlags). None means 'don't change
  48. # the value'
  49. return 0, ("It works!", None, None)
  50. return None # not handled.
  51. def GetMessageString(self, id):
  52. if id==win32ui.ID_APP_ABOUT:
  53. return "Dialog Test\nTest"
  54. else:
  55. return self._obj_.GetMessageString(id)
  56. def OnSize (self, params):
  57. print('OnSize called with ', params)
  58. def OnNext (self, id, cmd):
  59. print('OnNext called')
  60. def OnPrevious (self, id, cmd):
  61. print('OnPrevious called')
  62. msg = """\
  63. This toolbar was dynamically created.\r
  64. \r
  65. The first item's tooltips is provided by Python code.\r
  66. \r
  67. (Dont close the window with the toolbar in a floating state - it may not re-appear!)\r
  68. """
  69. def test():
  70. template = docview.DocTemplate( win32ui.IDR_PYTHONTYPE, None, GenericFrame, docview.EditView)
  71. doc = template.OpenDocumentFile(None)
  72. doc.SetTitle("Toolbar Test")
  73. view = doc.GetFirstView()
  74. view.SetWindowText(msg)
  75. if __name__=='__main__':
  76. import demoutils
  77. if demoutils.NeedGoodGUI():
  78. test()