codekingpro/portable-devtools
114k
1# Demo of ToolBars2 3# Shows the toolbar control.4# Demos how to make custom tooltips, etc.5 6import commctrl7import win32api8import win32con9import win32ui10from pywin.mfc import afxres, docview, window11 12 13class GenericFrame(window.MDIChildWnd):14 def OnCreateClient(self, cp, context):15 # handlers for toolbar buttons16 self.HookCommand(self.OnPrevious, 401)17 self.HookCommand(self.OnNext, 402)18 # Its not necessary for us to hook both of these - the19 # common controls should fall-back all by themselves.20 # Indeed, given we hook TTN_NEEDTEXTW, commctrl.TTN_NEEDTEXTA21 # will not be called.22 self.HookNotify(self.GetTTText, commctrl.TTN_NEEDTEXT)23 self.HookNotify(self.GetTTText, commctrl.TTN_NEEDTEXTW)24 25 # parent = win32ui.GetMainFrame()26 parent = self27 style = (28 win32con.WS_CHILD29 | win32con.WS_VISIBLE30 | afxres.CBRS_SIZE_DYNAMIC31 | afxres.CBRS_TOP32 | afxres.CBRS_TOOLTIPS33 | afxres.CBRS_FLYBY34 )35 36 buttons = (win32ui.ID_APP_ABOUT, win32ui.ID_VIEW_INTERACTIVE)37 bitmap = win32ui.IDB_BROWSER_HIER38 tbid = 0xE84039 self.toolbar = tb = win32ui.CreateToolBar(parent, style, tbid)40 tb.LoadBitmap(bitmap)41 tb.SetButtons(buttons)42 43 tb.EnableDocking(afxres.CBRS_ALIGN_ANY)44 tb.SetWindowText("Test")45 parent.EnableDocking(afxres.CBRS_ALIGN_ANY)46 parent.DockControlBar(tb)47 parent.LoadBarState("ToolbarTest")48 window.MDIChildWnd.OnCreateClient(self, cp, context)49 return 150 51 def OnDestroy(self, msg):52 self.SaveBarState("ToolbarTest")53 54 def GetTTText(self, std, extra):55 (hwndFrom, idFrom, code) = std56 text, hinst, flags = extra57 if flags & commctrl.TTF_IDISHWND:58 return # Not handled59 if idFrom == win32ui.ID_APP_ABOUT:60 # our 'extra' return value needs to be the following61 # entries from a NMTTDISPINFO[W] struct:62 # (szText, hinst, uFlags). None means 'don't change63 # the value'64 return 0, ("It works!", None, None)65 return None # not handled.66 67 def GetMessageString(self, id):68 if id == win32ui.ID_APP_ABOUT:69 return "Dialog Test\nTest"70 else:71 return self._obj_.GetMessageString(id)72 73 def OnSize(self, params):74 print("OnSize called with ", params)75 76 def OnNext(self, id, cmd):77 print("OnNext called")78 79 def OnPrevious(self, id, cmd):80 print("OnPrevious called")81 82 83msg = """\84This toolbar was dynamically created.\r85\r86The first item's tooltips is provided by Python code.\r87\r88(Dont close the window with the toolbar in a floating state - it may not re-appear!)\r89"""90 91 92def test():93 template = docview.DocTemplate(94 win32ui.IDR_PYTHONTYPE, None, GenericFrame, docview.EditView95 )96 doc = template.OpenDocumentFile(None)97 doc.SetTitle("Toolbar Test")98 view = doc.GetFirstView()99 view.SetWindowText(msg)100 101 102if __name__ == "__main__":103 import demoutils104 105 if demoutils.NeedGoodGUI():106 test()107 