m2m模型翻译
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.

122 lines
4.1 KiB

6 months ago
  1. # -*- coding: utf-8 -*-
  2. #*****************************************************************************
  3. # Copyright (C) 2003-2006 Jack Trainor.
  4. # Copyright (C) 2006 Jorgen Stenarson. <jorgen.stenarson@bostream.nu>
  5. #
  6. # Distributed under the terms of the BSD License. The full license is in
  7. # the file COPYING, distributed as part of this software.
  8. #*****************************************************************************
  9. ###################################
  10. #
  11. # Based on recipe posted to ctypes-users
  12. # see archive
  13. # http://aspn.activestate.com/ASPN/Mail/Message/ctypes-users/1771866
  14. #
  15. #
  16. ###################################################################################
  17. #
  18. # The Python win32clipboard lib functions work well enough ... except that they
  19. # can only cut and paste items from within one application, not across
  20. # applications or processes.
  21. #
  22. # I've written a number of Python text filters I like to run on the contents of
  23. # the clipboard so I need to call the Windows clipboard API with global memory
  24. # for my filters to work properly.
  25. #
  26. # Here's some sample code solving this problem using ctypes.
  27. #
  28. # This is my first work with ctypes. It's powerful stuff, but passing arguments
  29. # in and out of functions is tricky. More sample code would have been helpful,
  30. # hence this contribution.
  31. #
  32. ###################################################################################
  33. from __future__ import print_function, unicode_literals, absolute_import
  34. import ctypes
  35. import ctypes.wintypes as wintypes
  36. from ctypes import *
  37. from pyreadline.keysyms.winconstants import CF_UNICODETEXT, GHND
  38. from pyreadline.unicode_helper import ensure_unicode,ensure_str
  39. OpenClipboard = windll.user32.OpenClipboard
  40. OpenClipboard.argtypes = [wintypes.HWND]
  41. OpenClipboard.restype = wintypes.BOOL
  42. EmptyClipboard = windll.user32.EmptyClipboard
  43. GetClipboardData = windll.user32.GetClipboardData
  44. GetClipboardData.argtypes = [wintypes.UINT]
  45. GetClipboardData.restype = wintypes.HANDLE
  46. GetClipboardFormatName = windll.user32.GetClipboardFormatNameA
  47. GetClipboardFormatName.argtypes = [wintypes.UINT, c_char_p, c_int]
  48. SetClipboardData = windll.user32.SetClipboardData
  49. SetClipboardData.argtypes = [wintypes.UINT, wintypes.HANDLE]
  50. SetClipboardData.restype = wintypes.HANDLE
  51. EnumClipboardFormats = windll.user32.EnumClipboardFormats
  52. EnumClipboardFormats.argtypes = [c_int]
  53. CloseClipboard = windll.user32.CloseClipboard
  54. CloseClipboard.argtypes = []
  55. GlobalAlloc = windll.kernel32.GlobalAlloc
  56. GlobalAlloc.argtypes = [wintypes.UINT, c_size_t]
  57. GlobalAlloc.restype = wintypes.HGLOBAL
  58. GlobalLock = windll.kernel32.GlobalLock
  59. GlobalLock.argtypes = [wintypes.HGLOBAL]
  60. GlobalLock.restype = c_void_p
  61. GlobalUnlock = windll.kernel32.GlobalUnlock
  62. GlobalUnlock.argtypes = [c_int]
  63. _strncpy = ctypes.windll.kernel32.lstrcpynW
  64. _strncpy.restype = c_wchar_p
  65. _strncpy.argtypes = [c_wchar_p, c_wchar_p, c_size_t]
  66. def enum():
  67. OpenClipboard(0)
  68. q = EnumClipboardFormats(0)
  69. while q:
  70. q = EnumClipboardFormats(q)
  71. CloseClipboard()
  72. def getformatname(format):
  73. buffer = c_buffer(" "*100)
  74. bufferSize = sizeof(buffer)
  75. OpenClipboard(0)
  76. GetClipboardFormatName(format, buffer, bufferSize)
  77. CloseClipboard()
  78. return buffer.value
  79. def GetClipboardText():
  80. text = ""
  81. if OpenClipboard(0):
  82. hClipMem = GetClipboardData(CF_UNICODETEXT)
  83. if hClipMem:
  84. text = wstring_at(GlobalLock(hClipMem))
  85. GlobalUnlock(hClipMem)
  86. CloseClipboard()
  87. return text
  88. def SetClipboardText(text):
  89. buffer = create_unicode_buffer(ensure_unicode(text))
  90. bufferSize = sizeof(buffer)
  91. hGlobalMem = GlobalAlloc(GHND, c_size_t(bufferSize))
  92. GlobalLock.restype = c_void_p
  93. lpGlobalMem = GlobalLock(hGlobalMem)
  94. _strncpy(cast(lpGlobalMem, c_wchar_p),
  95. cast(addressof(buffer), c_wchar_p),
  96. c_size_t(bufferSize))
  97. GlobalUnlock(c_int(hGlobalMem))
  98. if OpenClipboard(0):
  99. EmptyClipboard()
  100. SetClipboardData(CF_UNICODETEXT, hGlobalMem)
  101. CloseClipboard()
  102. if __name__ == '__main__':
  103. txt = GetClipboardText() # display last text clipped
  104. print(txt)