fontdemo.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Demo of Generic document windows, DC, and Font usage
  2. # by Dave Brennan (brennan@hal.com)
  3. # usage examples:
  4. # >>> from fontdemo import *
  5. # >>> d = FontDemo('Hello, Python')
  6. # >>> f1 = { 'name':'Arial', 'height':36, 'weight':win32con.FW_BOLD}
  7. # >>> d.SetFont(f1)
  8. # >>> f2 = {'name':'Courier New', 'height':24, 'italic':1}
  9. # >>> d.SetFont (f2)
  10. import win32ui
  11. import win32con
  12. import win32api
  13. from pywin.mfc import docview
  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 Windows
  17. # size point size of font in logical units
  18. # weight weight of font (win32con.FW_NORMAL, win32con.FW_BOLD)
  19. # italic boolean; true if set to anything but None
  20. # underline boolean; true if set to anything but None
  21. class FontView(docview.ScrollView):
  22. def __init__(self, doc, text = 'Python Rules!', font_spec = {'name':'Arial', 'height':42}):
  23. docview.ScrollView.__init__(self, doc)
  24. self.font = win32ui.CreateFont (font_spec)
  25. self.text = text
  26. self.width = self.height = 0
  27. # set up message handlers
  28. self.HookMessage (self.OnSize, win32con.WM_SIZE)
  29. def OnAttachedObjectDeath(self):
  30. docview.ScrollView.OnAttachedObjectDeath(self)
  31. del self.font
  32. def SetFont (self, new_font):
  33. # Change font on the fly
  34. self.font = win32ui.CreateFont (new_font)
  35. # redraw the entire client window
  36. selfInvalidateRect (None)
  37. def OnSize (self, params):
  38. lParam = params[3]
  39. self.width = win32api.LOWORD(lParam)
  40. self.height = win32api.HIWORD(lParam)
  41. def OnPrepareDC (self, dc, printinfo):
  42. # Set up the DC for forthcoming OnDraw call
  43. self.SetScrollSizes(win32con.MM_TEXT, (100,100))
  44. dc.SetTextColor (win32api.RGB(0,0,255))
  45. dc.SetBkColor (win32api.GetSysColor (win32con.COLOR_WINDOW))
  46. dc.SelectObject (self.font)
  47. dc.SetTextAlign (win32con.TA_CENTER | win32con.TA_BASELINE)
  48. def OnDraw (self, dc):
  49. if (self.width == 0 and self.height == 0):
  50. left, top, right, bottom = self.GetClientRect()
  51. self.width = right - left
  52. self.height = bottom - top
  53. x, y = self.width // 2, self.height // 2
  54. dc.TextOut (x, y, self.text)
  55. def FontDemo():
  56. # create doc/view
  57. template = docview.DocTemplate(win32ui.IDR_PYTHONTYPE, None, None, FontView)
  58. doc=template.OpenDocumentFile(None)
  59. doc.SetTitle ('Font Demo')
  60. # print "template is ", template, "obj is", template._obj_
  61. template.close()
  62. # print "closed"
  63. # del template
  64. if __name__=='__main__':
  65. import demoutils
  66. if demoutils.NeedGoodGUI():
  67. FontDemo()