Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

print_desktop.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import pywintypes
  2. import win32api
  3. import win32con
  4. import win32gui
  5. import win32print
  6. pname = win32print.GetDefaultPrinter()
  7. print(pname)
  8. p = win32print.OpenPrinter(pname)
  9. print("Printer handle: ", p)
  10. print_processor = win32print.GetPrinter(p, 2)["pPrintProcessor"]
  11. ## call with last parm set to 0 to get total size needed for printer's DEVMODE
  12. dmsize = win32print.DocumentProperties(0, p, pname, None, None, 0)
  13. ## dmDriverExtra should be total size - fixed size
  14. driverextra = (
  15. dmsize - pywintypes.DEVMODEType().Size
  16. ) ## need a better way to get DEVMODE.dmSize
  17. dm = pywintypes.DEVMODEType(driverextra)
  18. dm.Fields = dm.Fields | win32con.DM_ORIENTATION | win32con.DM_COPIES
  19. dm.Orientation = win32con.DMORIENT_LANDSCAPE
  20. dm.Copies = 2
  21. win32print.DocumentProperties(
  22. 0, p, pname, dm, dm, win32con.DM_IN_BUFFER | win32con.DM_OUT_BUFFER
  23. )
  24. pDC = win32gui.CreateDC(print_processor, pname, dm)
  25. printerwidth = win32print.GetDeviceCaps(pDC, win32con.PHYSICALWIDTH)
  26. printerheight = win32print.GetDeviceCaps(pDC, win32con.PHYSICALHEIGHT)
  27. hwnd = win32gui.GetDesktopWindow()
  28. l, t, r, b = win32gui.GetWindowRect(hwnd)
  29. desktopheight = b - t
  30. desktopwidth = r - l
  31. dDC = win32gui.GetWindowDC(hwnd)
  32. dcDC = win32gui.CreateCompatibleDC(dDC)
  33. dcBM = win32gui.CreateCompatibleBitmap(dDC, desktopwidth, desktopheight)
  34. win32gui.SelectObject(dcDC, dcBM)
  35. win32gui.StretchBlt(
  36. dcDC,
  37. 0,
  38. 0,
  39. desktopwidth,
  40. desktopheight,
  41. dDC,
  42. 0,
  43. 0,
  44. desktopwidth,
  45. desktopheight,
  46. win32con.SRCCOPY,
  47. )
  48. pcDC = win32gui.CreateCompatibleDC(pDC)
  49. pcBM = win32gui.CreateCompatibleBitmap(pDC, printerwidth, printerheight)
  50. win32gui.SelectObject(pcDC, pcBM)
  51. win32gui.StretchBlt(
  52. pcDC,
  53. 0,
  54. 0,
  55. printerwidth,
  56. printerheight,
  57. dcDC,
  58. 0,
  59. 0,
  60. desktopwidth,
  61. desktopheight,
  62. win32con.SRCCOPY,
  63. )
  64. win32print.StartDoc(pDC, ("desktop.bmp", None, None, 0))
  65. win32print.StartPage(pDC)
  66. win32gui.StretchBlt(
  67. pDC,
  68. 0,
  69. 0,
  70. int(printerwidth * 0.9),
  71. int(printerheight * 0.9),
  72. pcDC,
  73. 0,
  74. 0,
  75. printerwidth,
  76. printerheight,
  77. win32con.SRCCOPY,
  78. )
  79. font = win32gui.LOGFONT()
  80. font.lfHeight = int(printerheight / 20)
  81. font.lfWidth = font.lfHeight
  82. font.lfWeight = 150
  83. font.lfItalic = 1
  84. font.lfUnderline = 1
  85. hf = win32gui.CreateFontIndirect(font)
  86. win32gui.SelectObject(pDC, hf)
  87. win32gui.SetBkMode(pDC, win32con.TRANSPARENT)
  88. win32gui.SetTextColor(pDC, win32api.RGB(0, 255, 0))
  89. win32gui.DrawText(
  90. pDC,
  91. "Printed by Python!",
  92. -1,
  93. (0, 0, int(printerwidth * 0.9), int(printerheight * 0.9)),
  94. win32con.DT_RIGHT | win32con.DT_BOTTOM | win32con.DT_SINGLELINE,
  95. )
  96. win32print.EndPage(pDC)
  97. win32print.EndDoc(pDC)
  98. win32print.ClosePrinter(p)
  99. win32gui.DeleteObject(dcBM)
  100. win32gui.DeleteObject(pcBM)
  101. win32gui.DeleteObject(hf)
  102. win32gui.DeleteDC(dDC)
  103. win32gui.DeleteDC(dcDC)
  104. win32gui.DeleteDC(pDC)
  105. win32gui.DeleteDC(pcDC)