customprint.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. # A demo of an Application object that has some custom print functionality.
  2. # If you desire, you can also run this from inside Pythonwin, in which
  3. # case it will do the demo inside the Pythonwin environment.
  4. # This sample was contributed by Roger Burnham.
  5. from pywin.mfc import docview, dialog, afxres
  6. from pywin.framework import app
  7. import win32con
  8. import win32ui
  9. import win32api
  10. PRINTDLGORD = 1538
  11. IDC_PRINT_MAG_EDIT = 1010
  12. class PrintDemoTemplate(docview.DocTemplate):
  13. def _SetupSharedMenu_(self):
  14. pass
  15. class PrintDemoView(docview.ScrollView):
  16. def OnInitialUpdate(self):
  17. ret = self._obj_.OnInitialUpdate()
  18. self.colors = {'Black' : (0x00<<0) + (0x00<<8) + (0x00<<16),
  19. 'Red' : (0xff<<0) + (0x00<<8) + (0x00<<16),
  20. 'Green' : (0x00<<0) + (0xff<<8) + (0x00<<16),
  21. 'Blue' : (0x00<<0) + (0x00<<8) + (0xff<<16),
  22. 'Cyan' : (0x00<<0) + (0xff<<8) + (0xff<<16),
  23. 'Magenta': (0xff<<0) + (0x00<<8) + (0xff<<16),
  24. 'Yellow' : (0xff<<0) + (0xff<<8) + (0x00<<16),
  25. }
  26. self.pens = {}
  27. for name, color in self.colors.items():
  28. self.pens[name] = win32ui.CreatePen(win32con.PS_SOLID,
  29. 5, color)
  30. self.pen = None
  31. self.size = (128,128)
  32. self.SetScaleToFitSize(self.size)
  33. self.HookCommand(self.OnFilePrint, afxres.ID_FILE_PRINT)
  34. self.HookCommand(self.OnFilePrintPreview,
  35. win32ui.ID_FILE_PRINT_PREVIEW)
  36. return ret
  37. def OnDraw(self, dc):
  38. oldPen = None
  39. x,y = self.size
  40. delta = 2
  41. colors = list(self.colors.keys())
  42. colors.sort()
  43. colors = colors*2
  44. for color in colors:
  45. if oldPen is None:
  46. oldPen = dc.SelectObject(self.pens[color])
  47. else:
  48. dc.SelectObject(self.pens[color])
  49. dc.MoveTo(( delta, delta))
  50. dc.LineTo((x-delta, delta))
  51. dc.LineTo((x-delta, y-delta))
  52. dc.LineTo(( delta, y-delta))
  53. dc.LineTo(( delta, delta))
  54. delta = delta + 4
  55. if x-delta <= 0 or y-delta <= 0:
  56. break
  57. dc.SelectObject(oldPen)
  58. def OnPrepareDC (self, dc, pInfo):
  59. if dc.IsPrinting():
  60. mag = self.prtDlg['mag']
  61. dc.SetMapMode(win32con.MM_ANISOTROPIC);
  62. dc.SetWindowOrg((0, 0))
  63. dc.SetWindowExt((1, 1))
  64. dc.SetViewportOrg((0, 0))
  65. dc.SetViewportExt((mag, mag))
  66. def OnPreparePrinting(self, pInfo):
  67. flags = (win32ui.PD_USEDEVMODECOPIES|
  68. win32ui.PD_PAGENUMS|
  69. win32ui.PD_NOPAGENUMS|
  70. win32ui.PD_NOSELECTION)
  71. self.prtDlg = ImagePrintDialog(pInfo, PRINTDLGORD, flags)
  72. pInfo.SetPrintDialog(self.prtDlg)
  73. pInfo.SetMinPage(1)
  74. pInfo.SetMaxPage(1)
  75. pInfo.SetFromPage(1)
  76. pInfo.SetToPage(1)
  77. ret = self.DoPreparePrinting(pInfo)
  78. return ret
  79. def OnBeginPrinting(self, dc, pInfo):
  80. return self._obj_.OnBeginPrinting(dc, pInfo)
  81. def OnEndPrinting(self, dc, pInfo):
  82. del self.prtDlg
  83. return self._obj_.OnEndPrinting(dc, pInfo)
  84. def OnFilePrintPreview(self, *arg):
  85. self._obj_.OnFilePrintPreview()
  86. def OnFilePrint(self, *arg):
  87. self._obj_.OnFilePrint()
  88. def OnPrint(self, dc, pInfo):
  89. doc = self.GetDocument()
  90. metrics = dc.GetTextMetrics()
  91. cxChar = metrics['tmAveCharWidth']
  92. cyChar = metrics['tmHeight']
  93. left, top, right, bottom = pInfo.GetDraw()
  94. dc.TextOut(0, 2*cyChar, doc.GetTitle())
  95. top = top + (7*cyChar)/2
  96. dc.MoveTo(left, top)
  97. dc.LineTo(right, top)
  98. top = top + cyChar
  99. # this seems to have not effect...
  100. # get what I want with the dc.SetWindowOrg calls
  101. pInfo.SetDraw((left, top, right, bottom))
  102. dc.SetWindowOrg((0, -top))
  103. self.OnDraw(dc)
  104. dc.SetTextAlign(win32con.TA_LEFT|win32con.TA_BOTTOM)
  105. rect = self.GetWindowRect()
  106. rect = self.ScreenToClient(rect)
  107. height = (rect[3]-rect[1])
  108. dc.SetWindowOrg((0, -(top+height+cyChar)))
  109. dc.MoveTo(left, 0)
  110. dc.LineTo(right, 0)
  111. x = 0
  112. y = (3*cyChar)/2
  113. dc.TextOut(x, y, doc.GetTitle())
  114. y = y + cyChar
  115. class PrintDemoApp(app.CApp):
  116. def __init__(self):
  117. app.CApp.__init__(self)
  118. def InitInstance(self):
  119. template = PrintDemoTemplate(None, None,
  120. None, PrintDemoView)
  121. self.AddDocTemplate(template)
  122. self._obj_.InitMDIInstance()
  123. self.LoadMainFrame()
  124. doc = template.OpenDocumentFile(None)
  125. doc.SetTitle('Custom Print Document')
  126. class ImagePrintDialog(dialog.PrintDialog):
  127. sectionPos = 'Image Print Demo'
  128. def __init__(self, pInfo, dlgID, flags=win32ui.PD_USEDEVMODECOPIES):
  129. dialog.PrintDialog.__init__(self, pInfo, dlgID, flags=flags)
  130. mag = win32ui.GetProfileVal(self.sectionPos,
  131. 'Document Magnification',
  132. 0)
  133. if mag <= 0:
  134. mag = 2
  135. win32ui.WriteProfileVal(self.sectionPos,
  136. 'Document Magnification',
  137. mag)
  138. self['mag'] = mag
  139. def OnInitDialog(self):
  140. self.magCtl = self.GetDlgItem(IDC_PRINT_MAG_EDIT)
  141. self.magCtl.SetWindowText(repr(self['mag']))
  142. return dialog.PrintDialog.OnInitDialog(self)
  143. def OnOK(self):
  144. dialog.PrintDialog.OnOK(self)
  145. strMag = self.magCtl.GetWindowText()
  146. try:
  147. self['mag'] = int(strMag)
  148. except:
  149. pass
  150. win32ui.WriteProfileVal(self.sectionPos,
  151. 'Document Magnification',
  152. self['mag'])
  153. if __name__=='__main__':
  154. # Running under Pythonwin
  155. def test():
  156. template = PrintDemoTemplate(None, None,
  157. None, PrintDemoView)
  158. template.OpenDocumentFile(None)
  159. test()
  160. else:
  161. app = PrintDemoApp()