Search code examples
c++windows-runtimec++-winrt

Does the WinRT Clipboard History only support text and images?


I'm making a C++/WinRT app that makes use of the Window 10 clipboard history but for some reason I can only grab text and images.

My code is a bit like this..

auto history = Clipboard::GetHistoryItemsAsync().get();

if (history.Status() == ClipboardHistoryItemsResultStatus::Success)
{
    auto historyItemList = history.Items();
    for (const auto& historyitem : historyItemList)
    {
        //Works
        if (dataPackageView.Contains(StandardDataFormats::Text()))
        {
            GetClipboardText(dataPackageView);
        }

        //Works
        if (dataPackageView.Contains(StandardDataFormats::Bitmap()))
        {
            GetClipboardBitmap(dataPackageView);
        }

        //Never triggers
        if (dataPackageView.Contains(StandardDataFormats::Html()))
        {
            GetClipboardHtml(dataPackageView);
        }
    
        //Never triggers
        if (dataPackageView.Contains(StandardDataFormats::StorageItems()))
        {
            GetClipboardStorageItems(dataPackageView);
        }
        //etc..

I'd like to make use of the other dataformats. EG: For StorageItems, I expected if I CTRL+C an item from my desktop or if I did it programatically with

dataPackage.SetStorageItems({ std::begin(files), std::end(files) });    
Clipboard::SetContent(dataPackage);
Clipboard::Flush(); 

The file would end up in the clipboard history so I could handle it like in the first snippet. But the file never appears in the returned Clipboard::GetHistoryItemsAsync() container. I also don't see the file in the history viewer when you press WIN+V.

So what i'm wondering is;

  • Does Windows 10 clipboard history "only" support raw text and images? can I not make use of the other dataformat properties? (ApplicationLink, Html, Rtf, StorageItems, Uri, Weblink etc)
  • I gave StorageItems as an example but it happens with the other formats too. If I CTRL+C a weblink (such as: https://stackoverflow.com/questions/ask), I expected dataPackageView.Contains(StandardDataFormats::WebLink()) Or Uri to trigger, but they don't
  • If the clipboard history can support other formats, how do you actually make use of it?

Edit: Forgot to add. When I programatically added a StorageItem to the clipboard I also tried it like below. Don't know why I can't add strings to an IVector though.

auto options = ClipboardContentOptions();

auto formats = Windows::Foundation::Collections::IVector<hstring>();

formats.Append(StandardDataFormats::StorageItems()); //Unhandled Exception

options.IsAllowedInHistory(true);
options.IsRoamable(true);
options.HistoryFormats() = formats;
options.RoamingFormats() = formats;


Clipboard::Clipboard::Clear();
Clipboard::SetContentWithOptions(dataPackage, options);
Clipboard::Flush();

Solution

  • As observed by my esteemed colleague Faywang - MSFT, Windows' clipboard history and cloud clipboard sync implementation supports only a limited number of clipboard data formats, and this is reflected in the UWP clipboard API in the Windows Runtime Clipboard class.

    It's important to remember that what clipboard formats are supported by clipboard history is not a contractual detail of clipboard history. Please keep that in mind when you read on:

    As of Windows 10 version 2004 (aka build 19041, aka the May 2020 update), clipboard history supports the following clipboard data formats defined in StandardDataFormats:

    • Plain text: StandardDataFormats.Text
    • Various URI formats: StandardDataFormats.Uri, StandardDataFormats.WebLink, and StandardDataFormats.ApplicationLink
    • HTML clipboard format: StandardDataFormats.Html
    • Bitmaps: StandardDataFormats.Bitmap

    For compatibility with classic desktop applications (aka Win32 apps), clipboard history also supports a few clipboard data formats whose names are not in StandardDataFormats, but which the Windows system can auto-convert to and from the above-listed StandardDataFormats formats when needed by an app trying to paste a particular format.

    Clipboard history does not support any other custom or well-known clipboard formats.


    Given the above, the only thing I see in your results that seems strange is that your app doesn't appear to be reading the HTML format. I don't know of any Windows implementation bugs in this area - try fiddling with your code some more, and if you still have trouble, I'd definitely encourage you to post another question, or contact us at Microsoft another way (such as through Feedback Hub).

    I do observe that the Microsoft documentation around clipboard history from a conceptual technical perspective is lacking. I'd also encourage you to file GitHub issues against the places where clipboard history is documented, such as https://github.com/MicrosoftDocs/winrt-api - we're always listening.