codekingpro/portable-devtools
114k
1# Demo of using just windows, without documents and views.2 3# Also demo of a GUI thread, pretty much direct from the MFC C++ sample MTMDI.4 5import timer6import win32api7import win32con8import win32ui9from pywin.mfc import docview, thread, window10from pywin.mfc.thread import WinThread11 12WM_USER_PREPARE_TO_CLOSE = win32con.WM_USER + 3213 14# font is a dictionary in which the following elements matter:15# (the best matching font to supplied parameters is returned)16# name string name of the font as known by Windows17# size point size of font in logical units18# weight weight of font (win32con.FW_NORMAL, win32con.FW_BOLD)19# italic boolean; true if set to anything but None20# underline boolean; true if set to anything but None21 22 23# This window is a child window of a frame. It is not the frame window itself.24class FontWindow(window.Wnd):25 def __init__(self, text="Python Rules!"):26 window.Wnd.__init__(self)27 self.text = text28 self.index = 029 self.incr = 130 self.width = self.height = 031 self.ChangeAttributes()32 # set up message handlers33 34 def Create(self, title, style, rect, parent):35 classStyle = win32con.CS_HREDRAW | win32con.CS_VREDRAW36 className = win32ui.RegisterWndClass(37 classStyle, 0, win32con.COLOR_WINDOW + 1, 038 )39 self._obj_ = win32ui.CreateWnd()40 self._obj_.AttachObject(self)41 self._obj_.CreateWindow(42 className, title, style, rect, parent, win32ui.AFX_IDW_PANE_FIRST43 )44 self.HookMessage(self.OnSize, win32con.WM_SIZE)45 self.HookMessage(self.OnPrepareToClose, WM_USER_PREPARE_TO_CLOSE)46 self.HookMessage(self.OnDestroy, win32con.WM_DESTROY)47 self.timerid = timer.set_timer(100, self.OnTimer)48 self.InvalidateRect()49 50 def OnDestroy(self, msg):51 timer.kill_timer(self.timerid)52 53 def OnTimer(self, id, timeVal):54 self.index = self.index + self.incr55 if self.index > len(self.text):56 self.incr = -157 self.index = len(self.text)58 elif self.index < 0:59 self.incr = 160 self.index = 061 self.InvalidateRect()62 63 def OnPaint(self):64 # print "Paint message from thread", win32api.GetCurrentThreadId()65 dc, paintStruct = self.BeginPaint()66 self.OnPrepareDC(dc, None)67 68 if self.width == 0 and self.height == 0:69 left, top, right, bottom = self.GetClientRect()70 self.width = right - left71 self.height = bottom - top72 x, y = self.width // 2, self.height // 273 dc.TextOut(x, y, self.text[: self.index])74 self.EndPaint(paintStruct)75 76 def ChangeAttributes(self):77 font_spec = {"name": "Arial", "height": 42}78 self.font = win32ui.CreateFont(font_spec)79 80 def OnPrepareToClose(self, params):81 self.DestroyWindow()82 83 def OnSize(self, params):84 lParam = params[3]85 self.width = win32api.LOWORD(lParam)86 self.height = win32api.HIWORD(lParam)87 88 def OnPrepareDC(self, dc, printinfo):89 # Set up the DC for forthcoming OnDraw call90 dc.SetTextColor(win32api.RGB(0, 0, 255))91 dc.SetBkColor(win32api.GetSysColor(win32con.COLOR_WINDOW))92 dc.SelectObject(self.font)93 dc.SetTextAlign(win32con.TA_CENTER | win32con.TA_BASELINE)94 95 96class FontFrame(window.MDIChildWnd):97 def __init__(self):98 pass # Dont call base class doc/view version...99 100 def Create(self, title, rect=None, parent=None):101 style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW102 self._obj_ = win32ui.CreateMDIChild()103 self._obj_.AttachObject(self)104 105 self._obj_.CreateWindow(None, title, style, rect, parent)106 rect = self.GetClientRect()107 rect = (0, 0, rect[2] - rect[0], rect[3] - rect[1])108 self.child = FontWindow("Not threaded")109 self.child.Create(110 "FontDemo", win32con.WS_CHILD | win32con.WS_VISIBLE, rect, self111 )112 113 114class TestThread(WinThread):115 def __init__(self, parentWindow):116 self.parentWindow = parentWindow117 self.child = None118 WinThread.__init__(self)119 120 def InitInstance(self):121 rect = self.parentWindow.GetClientRect()122 rect = (0, 0, rect[2] - rect[0], rect[3] - rect[1])123 124 self.child = FontWindow()125 self.child.Create(126 "FontDemo", win32con.WS_CHILD | win32con.WS_VISIBLE, rect, self.parentWindow127 )128 self.SetMainFrame(self.child)129 return WinThread.InitInstance(self)130 131 def ExitInstance(self):132 return 0133 134 135class ThreadedFontFrame(window.MDIChildWnd):136 def __init__(self):137 pass # Dont call base class doc/view version...138 self.thread = None139 140 def Create(self, title, rect=None, parent=None):141 style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW142 self._obj_ = win32ui.CreateMDIChild()143 self._obj_.CreateWindow(None, title, style, rect, parent)144 self._obj_.HookMessage(self.OnDestroy, win32con.WM_DESTROY)145 self._obj_.HookMessage(self.OnSize, win32con.WM_SIZE)146 147 self.thread = TestThread(self)148 self.thread.CreateThread()149 150 def OnSize(self, msg):151 pass152 153 def OnDestroy(self, msg):154 win32ui.OutputDebugString("OnDestroy\n")155 if self.thread and self.thread.child:156 child = self.thread.child157 child.SendMessage(WM_USER_PREPARE_TO_CLOSE, 0, 0)158 win32ui.OutputDebugString("Destroyed\n")159 160 161def Demo():162 f = FontFrame()163 f.Create("Font Demo")164 165 166def ThreadedDemo():167 rect = win32ui.GetMainFrame().GetMDIClient().GetClientRect()168 rect = rect[0], int(rect[3] * 3 / 4), int(rect[2] / 4), rect[3]169 incr = rect[2]170 for i in range(4):171 if i == 0:172 f = FontFrame()173 title = "Not threaded"174 else:175 f = ThreadedFontFrame()176 title = "Threaded GUI Demo"177 f.Create(title, rect)178 rect = rect[0] + incr, rect[1], rect[2] + incr, rect[3]179 # Givem a chance to start180 win32api.Sleep(100)181 win32ui.PumpWaitingMessages()182 183 184if __name__ == "__main__":185 import demoutils186 187 if demoutils.NeedGoodGUI():188 ThreadedDemo()189# Demo()190 