win32clipboard_bitmapdemo.py 3.3 KB

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