createwin.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # Window creation example
  3. #
  4. # This example creates a minimal "control" that just fills in its
  5. # window with red. To make your own control, subclass Control and
  6. # write your own OnPaint() method. See PyCWnd.HookMessage for what
  7. # the parameters to OnPaint are.
  8. #
  9. from pywin.mfc import dialog, window
  10. import win32ui
  11. import win32con
  12. import win32api
  13. class Control(window.Wnd):
  14. """Generic control class"""
  15. def __init__ (self):
  16. window.Wnd.__init__(self, win32ui.CreateWnd ())
  17. def OnPaint (self):
  18. dc, paintStruct = self.BeginPaint()
  19. self.DoPaint(dc)
  20. self.EndPaint(paintStruct)
  21. def DoPaint (self, dc): # Override this!
  22. pass
  23. class RedBox (Control):
  24. def DoPaint (self, dc):
  25. dc.FillSolidRect (self.GetClientRect(), win32api.RGB(255,0,0))
  26. class RedBoxWithPie (RedBox):
  27. def DoPaint (self, dc):
  28. RedBox.DoPaint(self, dc)
  29. r = self.GetClientRect()
  30. dc.Pie(r[0], r[1], r[2], r[3], 0,0,r[2], r[3]//2)
  31. def MakeDlgTemplate():
  32. style = (win32con.DS_MODALFRAME |
  33. win32con.WS_POPUP |
  34. win32con.WS_VISIBLE |
  35. win32con.WS_CAPTION |
  36. win32con.WS_SYSMENU |
  37. win32con.DS_SETFONT)
  38. cs = (win32con.WS_CHILD |
  39. win32con.WS_VISIBLE)
  40. w = 64
  41. h = 64
  42. dlg = [["Red box",
  43. (0, 0, w, h),
  44. style,
  45. None,
  46. (8, "MS Sans Serif")],
  47. ]
  48. s = win32con.WS_TABSTOP | cs
  49. dlg.append([128,
  50. "Cancel",
  51. win32con.IDCANCEL,
  52. (7, h - 18, 50, 14), s | win32con.BS_PUSHBUTTON])
  53. return dlg
  54. class TestDialog(dialog.Dialog):
  55. def OnInitDialog(self):
  56. rc = dialog.Dialog.OnInitDialog(self)
  57. self.redbox = RedBox ()
  58. self.redbox.CreateWindow (None, "RedBox",
  59. win32con.WS_CHILD |
  60. win32con.WS_VISIBLE,
  61. (5, 5, 90, 68),
  62. self, 1003)
  63. return rc
  64. class TestPieDialog(dialog.Dialog):
  65. def OnInitDialog(self):
  66. rc = dialog.Dialog.OnInitDialog(self)
  67. self.control = RedBoxWithPie()
  68. self.control.CreateWindow (None, "RedBox with Pie",
  69. win32con.WS_CHILD |
  70. win32con.WS_VISIBLE,
  71. (5, 5, 90, 68),
  72. self, 1003)
  73. def demo(modal=0):
  74. d = TestPieDialog (MakeDlgTemplate())
  75. if modal:
  76. d.DoModal()
  77. else:
  78. d.CreateWindow()
  79. if __name__=='__main__':
  80. demo(1)