ViliVi
ViliVi

Reputation: 25

Copy hyperlink to clipboard with alternative text for text editors that do not support hyperlink

Is it possible to store a hyperlink on the clipboard with some text that would be pasted to text editors that do not support hyperlinks? Notepad, for example.

I already have code that generates hyperlinks for files in Explorer. Currently the Paste option is disabled in Notepad if I copy a hyperlink to the clipboard, but I am able to copy my hyperlink into Outlook or Word. In my case, I want to paste the file path in Notepad.

I assume that this isn't possible without a specific handler on the text editor side. But if this is possible, I will be grateful for the link of some article or sample.

Upvotes: 0

Views: 580

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 598448

Simply open the clipboard and then call SetClipboardData multiple times, one for each format you want to provide. You can put CF_HTML and CF_TEXT (in that order) on the clipboard at the same time. This is documented behavior:

Multiple Clipboard Formats

A window can place more than one clipboard object on the clipboard, each representing the same information in a different clipboard format. When placing information on the clipboard, the window should provide data in as many formats as possible. To find out how many formats are currently used on the clipboard, call the CountClipboardFormats function.

Clipboard formats that contain the most information should be placed on the clipboard first, followed by less descriptive formats. A window pasting information from the clipboard typically retrieves a clipboard object in the first format it recognizes. Because clipboard formats are enumerated in the order they are placed on the clipboard, the first recognized format is also the most descriptive.

For example, suppose a user copies styled text from a word-processing document. The window containing the document might first place data on the clipboard in a registered format, such as RTF. Subsequently, the window would place data on the clipboard in a less descriptive format, such as text (CF_TEXT).

When the content of the clipboard is pasted into another window, the window retrieves data in the most descriptive format it recognizes. If the window recognizes RTF, the corresponding data is pasted into the document. Otherwise, the text data is pasted into the document and the formatting information is lost.

Upvotes: 1

Related Questions