Python win32con.CF_TEXT Examples

The following are 8 code examples of win32con.CF_TEXT(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module win32con , or try the search function .
Example #1
Source File: testClipboard.py    From ironpython2 with Apache License 2.0 6 votes vote down vote up
def testWin32ToCom(self):
        # Set the data via the std win32 clipboard functions.
        val = str2bytes("Hello again!") # ensure always bytes, even in py3k
        win32clipboard.OpenClipboard()
        win32clipboard.SetClipboardData(win32con.CF_TEXT, val)
        win32clipboard.CloseClipboard()
        # and get it via an IDataObject provided by COM
        do = pythoncom.OleGetClipboard()
        cf = win32con.CF_TEXT, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL
        stg = do.GetData(cf)
        got = stg.data
        # The data we get back has the \0, as our STGMEDIUM has no way of 
        # knowing if it meant to be a string, or a binary buffer, so
        # it must return it too.
        self.failUnlessEqual(got, str2bytes("Hello again!\0")) 
Example #2
Source File: clipboard.py    From dragonfly with GNU Lesser General Public License v3.0 6 votes vote down vote up
def __init__(self, contents=None, text=None, from_system=False):
        self._contents = {}

        # If requested, retrieve current system clipboard contents.
        if from_system:
            self.copy_from_system()

        # Process given contents for this Clipboard instance.
        if contents:
            try:
                self._contents = dict(contents)
            except Exception as e:
                raise TypeError("Invalid contents: %s (%r)" % (e, contents))

        # Handle special case of text content.
        if not text is None:
            self._contents[self.format_unicode] = text_type(text)

        # Use a binary string for CF_TEXT content.
        cf_text_content = self._contents.get(self.format_text)
        if isinstance(cf_text_content, text_type):
            enc = locale.getpreferredencoding()
            self._contents[self.format_text] = cf_text_content.encode(enc) 
Example #3
Source File: test_clipboard.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_unicode_text(self):
        val = "test-val"
        SetClipboardText(val)
        # GetClipboardData doesn't to auto string conversions - so on py3k,
        # CF_TEXT returns bytes.
        expected = str2bytes(val)
        self.failUnlessEqual(GetClipboardData(win32con.CF_TEXT), expected)
        SetClipboardText(val, win32con.CF_UNICODETEXT)
        self.failUnlessEqual(GetClipboardData(win32con.CF_UNICODETEXT), val) 
Example #4
Source File: test_clipboard.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_string(self):
        val = str2bytes("test")
        SetClipboardData(win32con.CF_TEXT, val)
        self.failUnlessEqual(GetClipboardData(win32con.CF_TEXT), val) 
Example #5
Source File: test_clipboard.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def test_mem(self):
        val = str2bytes("test")
        expected = str2bytes("test\0")
        SetClipboardData(win32con.CF_TEXT, val)
        # Get the raw data - this will include the '\0'
        raw_data = GetGlobalMemory(GetClipboardDataHandle(win32con.CF_TEXT))
        self.failUnlessEqual(expected, raw_data) 
Example #6
Source File: testClipboard.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def __init__(self, strval):
        global num_do_objects
        num_do_objects += 1
        self.strval = strval
        self.supported_fe = []
        for cf in (win32con.CF_TEXT, win32con.CF_UNICODETEXT):
            fe = cf, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL
            self.supported_fe.append(fe) 
Example #7
Source File: testClipboard.py    From ironpython2 with Apache License 2.0 5 votes vote down vote up
def testComToWin32(self):
        # Set the data via our DataObject
        do = TestDataObject("Hello from Python")
        do = WrapCOMObject(do, iid=pythoncom.IID_IDataObject)
        pythoncom.OleSetClipboard(do)
        # Then get it back via the standard win32 clipboard functions.
        win32clipboard.OpenClipboard()
        got = win32clipboard.GetClipboardData(win32con.CF_TEXT)
        # CF_TEXT gives bytes on py3k - use str2bytes() to ensure that's true.
        expected = str2bytes("Hello from Python")
        self.assertEqual(got, expected)
        # Now check unicode
        got = win32clipboard.GetClipboardData(win32con.CF_UNICODETEXT)
        self.assertEqual(got, u"Hello from Python")
        win32clipboard.CloseClipboard() 
Example #8
Source File: win32clipboardDemo.py    From ironpython2 with Apache License 2.0 4 votes vote down vote up
def TestText():
    OpenClipboard()
    try:
        text = "Hello from Python"
        text_bytes = str2bytes(text)
        SetClipboardText(text)
        got = GetClipboardData(win32con.CF_TEXT)
        # CF_TEXT always gives us 'bytes' back .
        assert  got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
    finally:
        CloseClipboard()

    OpenClipboard()
    try:
        # CF_UNICODE text always gives unicode objects back.
        got = GetClipboardData(win32con.CF_UNICODETEXT)
        assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
        assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)

        # CF_OEMTEXT is a bytes-based format.
        got = GetClipboardData(win32con.CF_OEMTEXT)
        assert  got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)

        # Unicode tests
        EmptyClipboard()
        text = u"Hello from Python unicode"
        text_bytes = str2bytes(text)
        # Now set the Unicode value
        SetClipboardData(win32con.CF_UNICODETEXT, text)
        # Get it in Unicode.
        got = GetClipboardData(win32con.CF_UNICODETEXT)
        assert  got == text, "Didnt get the correct result back - '%r'." % (got,)
        assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)

        # Close and open the clipboard to ensure auto-conversions take place.
    finally:
        CloseClipboard()

    OpenClipboard()
    try:

        # Make sure I can still get the text as bytes
        got = GetClipboardData(win32con.CF_TEXT)
        assert  got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
        # Make sure we get back the correct types.
        got = GetClipboardData(win32con.CF_UNICODETEXT)
        assert type(got)==types.UnicodeType, "Didnt get the correct result back - '%r'." % (got,)
        got = GetClipboardData(win32con.CF_OEMTEXT)
        assert  got == text_bytes, "Didnt get the correct result back - '%r'." % (got,)
        print "Clipboard text tests worked correctly"
    finally:
        CloseClipboard()