win32rcparser_demo.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. # A demo of the win32rcparser module and using win32gui
  2. import win32gui
  3. import win32api
  4. import win32con
  5. import win32rcparser
  6. import commctrl
  7. import sys, os
  8. this_dir = os.path.abspath(os.path.dirname(__file__))
  9. g_rcname = os.path.abspath(
  10. os.path.join( this_dir, "..", "test", "win32rcparser", "test.rc"))
  11. if not os.path.isfile(g_rcname):
  12. raise RuntimeError("Can't locate test.rc (should be at '%s')" % (g_rcname,))
  13. class DemoWindow:
  14. def __init__(self, dlg_template):
  15. self.dlg_template = dlg_template
  16. def CreateWindow(self):
  17. self._DoCreate(win32gui.CreateDialogIndirect)
  18. def DoModal(self):
  19. return self._DoCreate(win32gui.DialogBoxIndirect)
  20. def _DoCreate(self, fn):
  21. message_map = {
  22. win32con.WM_INITDIALOG: self.OnInitDialog,
  23. win32con.WM_CLOSE: self.OnClose,
  24. win32con.WM_DESTROY: self.OnDestroy,
  25. win32con.WM_COMMAND: self.OnCommand,
  26. }
  27. return fn(0, self.dlg_template, 0, message_map)
  28. def OnInitDialog(self, hwnd, msg, wparam, lparam):
  29. self.hwnd = hwnd
  30. # centre the dialog
  31. desktop = win32gui.GetDesktopWindow()
  32. l,t,r,b = win32gui.GetWindowRect(self.hwnd)
  33. dt_l, dt_t, dt_r, dt_b = win32gui.GetWindowRect(desktop)
  34. centre_x, centre_y = win32gui.ClientToScreen( desktop, ( (dt_r-dt_l)//2, (dt_b-dt_t)//2) )
  35. win32gui.MoveWindow(hwnd, centre_x-(r//2), centre_y-(b//2), r-l, b-t, 0)
  36. def OnCommand(self, hwnd, msg, wparam, lparam):
  37. # Needed to make OK/Cancel work - no other controls are handled.
  38. id = win32api.LOWORD(wparam)
  39. if id in [win32con.IDOK, win32con.IDCANCEL]:
  40. win32gui.EndDialog(hwnd, id)
  41. def OnClose(self, hwnd, msg, wparam, lparam):
  42. win32gui.EndDialog(hwnd, 0)
  43. def OnDestroy(self, hwnd, msg, wparam, lparam):
  44. pass
  45. def DemoModal():
  46. # Load the .rc file.
  47. resources = win32rcparser.Parse(g_rcname)
  48. for id, ddef in resources.dialogs.items():
  49. print("Displaying dialog", id)
  50. w=DemoWindow(ddef)
  51. w.DoModal()
  52. if __name__=='__main__':
  53. flags = 0
  54. for flag in """ICC_DATE_CLASSES ICC_ANIMATE_CLASS ICC_ANIMATE_CLASS
  55. ICC_BAR_CLASSES ICC_COOL_CLASSES ICC_DATE_CLASSES
  56. ICC_HOTKEY_CLASS ICC_INTERNET_CLASSES ICC_LISTVIEW_CLASSES
  57. ICC_PAGESCROLLER_CLASS ICC_PROGRESS_CLASS ICC_TAB_CLASSES
  58. ICC_TREEVIEW_CLASSES ICC_UPDOWN_CLASS ICC_USEREX_CLASSES
  59. ICC_WIN95_CLASSES """.split():
  60. flags |= getattr(commctrl, flag)
  61. win32gui.InitCommonControlsEx(flags)
  62. # Need to do this go get rich-edit working.
  63. win32api.LoadLibrary("riched20.dll")
  64. DemoModal()