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.

win32clipboard_bitmapdemo.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. import win32api
  2. import win32clipboard
  3. import win32con
  4. import win32gui
  5. class ViewerWindow:
  6. def __init__(self):
  7. self.hwndNextViewer = None
  8. def OnPaint(self, hwnd, msg, wp, lp):
  9. dc, ps = win32gui.BeginPaint(hwnd)
  10. wndrect = win32gui.GetClientRect(hwnd)
  11. wndwidth = wndrect[2] - wndrect[0]
  12. wndheight = wndrect[3] - wndrect[1]
  13. win32clipboard.OpenClipboard()
  14. try:
  15. try:
  16. hbitmap = win32clipboard.GetClipboardData(win32clipboard.CF_BITMAP)
  17. except TypeError:
  18. font = win32gui.LOGFONT()
  19. font.lfHeight = 15 # int(wndheight/20)
  20. font.lfWidth = 15 # font.lfHeight
  21. # font.lfWeight=150
  22. hf = win32gui.CreateFontIndirect(font)
  23. win32gui.SelectObject(dc, hf)
  24. win32gui.SetBkMode(dc, win32con.TRANSPARENT)
  25. win32gui.SetTextColor(dc, win32api.RGB(0, 0, 0))
  26. win32gui.DrawText(
  27. dc,
  28. "No bitmaps are in the clipboard\n(try pressing the PrtScn button)",
  29. -1,
  30. (0, 0, wndwidth, wndheight),
  31. win32con.DT_CENTER,
  32. )
  33. else:
  34. bminfo = win32gui.GetObject(hbitmap)
  35. dcDC = win32gui.CreateCompatibleDC(None)
  36. win32gui.SelectObject(dcDC, hbitmap)
  37. win32gui.StretchBlt(
  38. dc,
  39. 0,
  40. 0,
  41. wndwidth,
  42. wndheight,
  43. dcDC,
  44. 0,
  45. 0,
  46. bminfo.bmWidth,
  47. bminfo.bmHeight,
  48. win32con.SRCCOPY,
  49. )
  50. win32gui.DeleteDC(dcDC)
  51. win32gui.EndPaint(hwnd, ps)
  52. finally:
  53. win32clipboard.CloseClipboard()
  54. return 0
  55. def OnDrawClipboard(self, hwnd, msg, wp, lp):
  56. win32gui.InvalidateRect(hwnd, None, True)
  57. def OnChangeCBChain(self, hwnd, msg, wp, lp):
  58. # If the next window is closing, repair the chain.
  59. if wp == self.hwndNextViewer:
  60. self.hwndNextViewer = lp
  61. # Otherwise, pass the message to the next link.
  62. elif self.hwndNextViewer:
  63. win32gui.SendMessage(self.hwndNextViewer, msg, wp, lp)
  64. def OnCreate(self, hwnd, msg, wp, lp):
  65. self.hwndNextViewer = win32gui.SetClipboardViewer(hwnd)
  66. def OnClose(self, hwnd, msg, wp, lp):
  67. win32clipboard.ChangeClipboardChain(hwnd, self.hwndNextViewer)
  68. win32gui.DestroyWindow(hwnd)
  69. win32gui.PostQuitMessage(0)
  70. def go(self):
  71. wndproc = {
  72. win32con.WM_PAINT: self.OnPaint,
  73. win32con.WM_CLOSE: self.OnClose,
  74. win32con.WM_CREATE: self.OnCreate,
  75. win32con.WM_DRAWCLIPBOARD: self.OnDrawClipboard,
  76. win32con.WM_CHANGECBCHAIN: self.OnChangeCBChain,
  77. }
  78. wc = win32gui.WNDCLASS()
  79. wc.lpszClassName = "test_win32clipboard_bmp"
  80. wc.style = win32con.CS_GLOBALCLASS | win32con.CS_VREDRAW | win32con.CS_HREDRAW
  81. wc.hbrBackground = win32con.COLOR_WINDOW + 1
  82. wc.lpfnWndProc = wndproc
  83. class_atom = win32gui.RegisterClass(wc)
  84. hwnd = win32gui.CreateWindowEx(
  85. 0,
  86. class_atom,
  87. "ClipboardViewer",
  88. win32con.WS_CAPTION
  89. | win32con.WS_VISIBLE
  90. | win32con.WS_THICKFRAME
  91. | win32con.WS_SYSMENU,
  92. 100,
  93. 100,
  94. 900,
  95. 900,
  96. 0,
  97. 0,
  98. 0,
  99. None,
  100. )
  101. win32clipboard.SetClipboardViewer(hwnd)
  102. win32gui.PumpMessages()
  103. win32gui.UnregisterClass(class_atom, None)
  104. if __name__ == "__main__":
  105. w = ViewerWindow()
  106. w.go()