Nota
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare ad accedere o modificare le directory.
L'accesso a questa pagina richiede l'autorizzazione. È possibile provare a modificare le directory.
Questo articolo illustra come aggiungere codice HTML (Hypertext Markup Language) agli Appunti di Microsoft Windows tramite Visual C++.
Versione originale del prodotto: Visual C++
Numero KB originale: 274308
Riepilogo
Questo articolo include una funzione di esempio che è possibile usare nelle applicazioni per semplificare il processo di aggiunta di codice HTML agli Appunti.
Formato appunti HTML
HTML ha un proprio formato di Appunti denominato formato HTML (CF_HTML) che è possibile usare per fornire i dati ad altre applicazioni, ad esempio Excel, Word o altre app Office licazioni.
CF_HTML è interamente un formato basato su testo che include una descrizione, un contesto e un frammento all'interno di tale contesto. Quando si compilano dati da inviare agli Appunti, è necessario includere una descrizione dei dati per indicare la versione degli Appunti e gli offset per il contesto e il frammento. Il calcolo degli offset può essere la parte difficile. Tuttavia, è possibile usare la routine seguente per semplificare questa attività.
Per altre informazioni, vedere Formato Appunti HTML.
Codice di esempio
// CopyHtml() - Copies given HTML to the clipboard.
// The HTML/BODY blanket is provided, so you only need to
// call it like CallHtml("<b>This is a test</b>");
void CopyHTML(char *html)
{
// Create temporary buffer for HTML header...
char *buf = new char [400 + strlen(html)];
if(!buf) return;
// Get clipboard id for HTML format...
static int cfid = 0;
if(!cfid) cfid = RegisterClipboardFormat("HTML Format");
// Create a template string for the HTML header...
strcpy(buf,
"Version:0.9\r\n"
"StartHTML:00000000\r\n"
"EndHTML:00000000\r\n"
"StartFragment:00000000\r\n"
"EndFragment:00000000\r\n"
"<html><body>\r\n"
"<!--StartFragment -->\r\n");
// Append the HTML...
strcat(buf, html);
strcat(buf, "\r\n");
// Finish up the HTML format...
strcat(buf,
"<!--EndFragment-->\r\n"
"</body>\r\n"
"</html>");
// Now go back, calculate all the lengths, and write out the
// necessary header information. Note, wsprintf() truncates the
// string when you overwrite it so you follow up with code to replace
// the 0 appended at the end with a '\r'...
char *ptr = strstr(buf, "StartHTML");
wsprintf(ptr+10, "%08u", strstr(buf, "<html>") - buf);
*(ptr+10+8) = '\r';
ptr = strstr(buf, "EndHTML");
wsprintf(ptr+8, "%08u", strlen(buf));
*(ptr+8+8) = '\r';
ptr = strstr(buf, "StartFragment");
wsprintf(ptr+14, "%08u", strstr(buf, "<!--StartFrag") - buf);
*(ptr+14+8) = '\r';
ptr = strstr(buf, "EndFragment");
wsprintf(ptr+12, "%08u", strstr(buf, "<!--EndFrag") - buf);
*(ptr+12+8) = '\r';
// Now you have everything in place ready to put on the clipboard.
// Open the clipboard...
if(OpenClipboard(0))
{
// Empty what's in there...
EmptyClipboard();
// Allocate global memory for transfer...
HGLOBAL hText = GlobalAlloc(GMEM_MOVEABLE |GMEM_DDESHARE, strlen(buf)+4);
// Put your string in the global memory...
char *ptr = (char *)GlobalLock(hText);
strcpy(ptr, buf);
GlobalUnlock(hText);
::SetClipboardData(cfid, hText);
CloseClipboard();
// Free memory...
GlobalFree(hText);
}
// Clean up...
delete [] buf;
}
Quando si usa questa funzione per copiare un frammento di codice HTML negli Appunti, potrebbe essere simile all'esempio seguente:
char *html =
"<b>This is a test</b><hr>"
"<li>entry 1"
"<li>entry 2";
CopyHTML(html);
Ulteriori informazioni
L'uso di un approccio che invia codice HTML agli Appunti potrebbe essere particolarmente utile per i client di automazione di Office. Ad esempio, se si dispone di un client di automazione che deve generare dati formattati per le celle in Excel o paragrafi in Word, è possibile compilare i dati nel codice HTML, inviarli agli Appunti e incollarli nell'applicazione. Usando questa tecnica, è possibile ridurre il numero di chiamate out-of-process al client di automazione.