sliderdemo.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # sliderdemo.py
  2. # Demo of the slider control courtesy of Mike Fletcher.
  3. import win32con, win32ui
  4. from pywin.mfc import dialog
  5. class MyDialog(dialog.Dialog):
  6. '''
  7. Example using simple controls
  8. '''
  9. _dialogstyle = (win32con.WS_MINIMIZEBOX | win32con.WS_DLGFRAME |
  10. win32con.DS_MODALFRAME | win32con.WS_POPUP | win32con.WS_VISIBLE |
  11. win32con.WS_CAPTION | win32con.WS_SYSMENU | win32con.DS_SETFONT )
  12. _buttonstyle = (win32con.BS_PUSHBUTTON | win32con.WS_TABSTOP |
  13. win32con.WS_CHILD | win32con.WS_VISIBLE)
  14. ### The static template, contains all "normal" dialog items
  15. DIALOGTEMPLATE = [
  16. # the dialog itself is the first element in the template
  17. ["Example slider", (0, 0, 50, 43), _dialogstyle, None, (8, "MS SansSerif")],
  18. # rest of elements are the controls within the dialog
  19. # standard "Close" button
  20. [128, "Close", win32con.IDCANCEL, (0, 30, 50, 13), _buttonstyle], ]
  21. ### ID of the control to be created during dialog initialisation
  22. IDC_SLIDER = 9500
  23. def __init__(self ):
  24. dialog.Dialog.__init__(self, self.DIALOGTEMPLATE)
  25. def OnInitDialog(self):
  26. rc = dialog.Dialog.OnInitDialog(self)
  27. # now initialise your controls that you want to create
  28. # programmatically, including those which are OLE controls
  29. # those created directly by win32ui.Create*
  30. # and your "custom controls" which are subclasses/whatever
  31. win32ui.EnableControlContainer()
  32. self.slider = win32ui.CreateSliderCtrl( )
  33. self.slider.CreateWindow( win32con.WS_TABSTOP | win32con.WS_VISIBLE,
  34. (0,0,100,30),
  35. self._obj_,
  36. self.IDC_SLIDER)
  37. self.HookMessage(self.OnSliderMove, win32con.WM_HSCROLL)
  38. return rc
  39. def OnSliderMove(self, params):
  40. print("Slider moved")
  41. def OnCancel(self):
  42. print("The slider control is at position", self.slider.GetPos())
  43. self._obj_.OnCancel()
  44. ###
  45. def demo():
  46. dia = MyDialog()
  47. dia.DoModal()
  48. if __name__ == "__main__":
  49. demo()