threadedgui.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. # Demo of using just windows, without documents and views.
  2. # Also demo of a GUI thread, pretty much direct from the MFC C++ sample MTMDI.
  3. import win32ui
  4. import win32con
  5. import win32api
  6. import timer
  7. from pywin.mfc import window, docview, thread
  8. from pywin.mfc.thread import WinThread
  9. WM_USER_PREPARE_TO_CLOSE = win32con.WM_USER + 32
  10. # font is a dictionary in which the following elements matter:
  11. # (the best matching font to supplied parameters is returned)
  12. # name string name of the font as known by Windows
  13. # size point size of font in logical units
  14. # weight weight of font (win32con.FW_NORMAL, win32con.FW_BOLD)
  15. # italic boolean; true if set to anything but None
  16. # underline boolean; true if set to anything but None
  17. # This window is a child window of a frame. It is not the frame window itself.
  18. class FontWindow(window.Wnd):
  19. def __init__(self, text = 'Python Rules!'):
  20. window.Wnd.__init__(self)
  21. self.text = text
  22. self.index = 0
  23. self.incr = 1
  24. self.width = self.height = 0
  25. self.ChangeAttributes()
  26. # set up message handlers
  27. def Create(self, title, style, rect, parent):
  28. classStyle = win32con.CS_HREDRAW | win32con.CS_VREDRAW
  29. className = win32ui.RegisterWndClass(classStyle, 0, win32con.COLOR_WINDOW+1, 0)
  30. self._obj_ = win32ui.CreateWnd()
  31. self._obj_.AttachObject(self)
  32. self._obj_.CreateWindow(className, title, style, rect, parent, win32ui.AFX_IDW_PANE_FIRST)
  33. self.HookMessage (self.OnSize, win32con.WM_SIZE)
  34. self.HookMessage (self.OnPrepareToClose, WM_USER_PREPARE_TO_CLOSE)
  35. self.HookMessage (self.OnDestroy, win32con.WM_DESTROY)
  36. self.timerid = timer.set_timer (100, self.OnTimer)
  37. self.InvalidateRect()
  38. def OnDestroy (self, msg):
  39. timer.kill_timer(self.timerid)
  40. def OnTimer(self, id, timeVal):
  41. self.index = self.index + self.incr
  42. if self.index > len(self.text):
  43. self.incr = -1
  44. self.index = len(self.text)
  45. elif self.index < 0:
  46. self.incr = 1
  47. self.index = 0
  48. self.InvalidateRect()
  49. def OnPaint (self):
  50. # print "Paint message from thread", win32api.GetCurrentThreadId()
  51. dc, paintStruct = self.BeginPaint()
  52. self.OnPrepareDC(dc, None)
  53. if (self.width == 0 and self.height == 0):
  54. left, top, right, bottom = self.GetClientRect()
  55. self.width = right - left
  56. self.height = bottom - top
  57. x, y = self.width // 2, self.height // 2
  58. dc.TextOut (x, y, self.text[:self.index])
  59. self.EndPaint(paintStruct)
  60. def ChangeAttributes(self):
  61. font_spec = {'name':'Arial', 'height':42}
  62. self.font = win32ui.CreateFont (font_spec)
  63. def OnPrepareToClose(self, params):
  64. self.DestroyWindow()
  65. def OnSize (self, params):
  66. lParam = params[3]
  67. self.width = win32api.LOWORD(lParam)
  68. self.height = win32api.HIWORD(lParam)
  69. def OnPrepareDC (self, dc, printinfo):
  70. # Set up the DC for forthcoming OnDraw call
  71. dc.SetTextColor (win32api.RGB(0,0,255))
  72. dc.SetBkColor (win32api.GetSysColor (win32con.COLOR_WINDOW))
  73. dc.SelectObject (self.font)
  74. dc.SetTextAlign (win32con.TA_CENTER | win32con.TA_BASELINE)
  75. class FontFrame(window.MDIChildWnd):
  76. def __init__(self):
  77. pass # Dont call base class doc/view version...
  78. def Create(self, title, rect = None, parent = None):
  79. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW
  80. self._obj_ = win32ui.CreateMDIChild()
  81. self._obj_.AttachObject(self)
  82. self._obj_.CreateWindow(None, title, style, rect, parent)
  83. rect = self.GetClientRect()
  84. rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
  85. self.child = FontWindow("Not threaded")
  86. self.child.Create("FontDemo", win32con.WS_CHILD | win32con.WS_VISIBLE, rect, self)
  87. class TestThread(WinThread):
  88. def __init__(self, parentWindow):
  89. self.parentWindow = parentWindow
  90. self.child = None
  91. WinThread.__init__(self)
  92. def InitInstance(self):
  93. rect = self.parentWindow.GetClientRect()
  94. rect = (0,0,rect[2]-rect[0], rect[3]-rect[1])
  95. self.child = FontWindow()
  96. self.child.Create("FontDemo", win32con.WS_CHILD | win32con.WS_VISIBLE, rect, self.parentWindow)
  97. self.SetMainFrame(self.child)
  98. return WinThread.InitInstance(self)
  99. def ExitInstance(self):
  100. return 0
  101. class ThreadedFontFrame(window.MDIChildWnd):
  102. def __init__(self):
  103. pass # Dont call base class doc/view version...
  104. self.thread = None
  105. def Create(self, title, rect = None, parent = None):
  106. style = win32con.WS_CHILD | win32con.WS_VISIBLE | win32con.WS_OVERLAPPEDWINDOW
  107. self._obj_ = win32ui.CreateMDIChild()
  108. self._obj_.CreateWindow(None, title, style, rect, parent)
  109. self._obj_.HookMessage(self.OnDestroy, win32con.WM_DESTROY)
  110. self._obj_.HookMessage (self.OnSize, win32con.WM_SIZE)
  111. self.thread = TestThread(self)
  112. self.thread.CreateThread()
  113. def OnSize(self, msg):
  114. pass
  115. def OnDestroy(self, msg):
  116. win32ui.OutputDebugString("OnDestroy\n")
  117. if self.thread and self.thread.child:
  118. child = self.thread.child
  119. child.SendMessage(WM_USER_PREPARE_TO_CLOSE, 0, 0)
  120. win32ui.OutputDebugString("Destroyed\n")
  121. def Demo():
  122. f = FontFrame()
  123. f.Create("Font Demo")
  124. def ThreadedDemo():
  125. rect = win32ui.GetMainFrame().GetMDIClient().GetClientRect()
  126. rect = rect[0], int(rect[3]*3/4), int(rect[2]/4), rect[3]
  127. incr = rect[2]
  128. for i in range(4):
  129. if i==0:
  130. f = FontFrame()
  131. title = "Not threaded"
  132. else:
  133. f = ThreadedFontFrame()
  134. title = "Threaded GUI Demo"
  135. f.Create(title, rect)
  136. rect = rect[0] + incr, rect[1], rect[2]+incr, rect[3]
  137. # Givem a chance to start
  138. win32api.Sleep(100)
  139. win32ui.PumpWaitingMessages()
  140. if __name__=='__main__':
  141. import demoutils
  142. if demoutils.NeedGoodGUI():
  143. ThreadedDemo()
  144. # Demo()