Copier de l'HTML dans le pressepapier

thonyboy Messages postés 165 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 16 février 2011 - 24 avril 2009 à 17:08
thonyboy Messages postés 165 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 16 février 2011 - 24 avril 2009 à 17:21
Bonjour

je voudrais copier un lien HTML du type
\\serveur\espace de travail\chemin accentué
dans le presse papier

si je fais un clipboard.astext cela me copie du fichier en texte brut

A faire des recherche, j'ai trouvé une fonction sur le net qui permet de copier dans le presse papier au format HTML (il y avait peut etre plus simple mais je n'ai rien trouvé donc)

Ca fonctionne, sauf que les caractères accentués (éèà etc) sont transformé en hyroglyphes quand je fais le paste dans Outlook.

Quelqu'un aurait une idée pour me dépatouiller de ce probleme ?

Merci a tous

3 réponses

thonyboy Messages postés 165 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 16 février 2011
24 avril 2009 à 17:09
Pour completer mon message, voici le code de la fonction que j'ai trouvé et que j'utilise



{
If you've ever tried sticking html into the clipboard using the usual CF_TEXT
format then you might have been disappointed to discover that wysiwyg html
editors paste your offering as if it were just text,
rather than recognising it as html. For that you need the CF_HTML format.
CF_HTML is entirely text format and uses the transformation format UTF-8.
It includes a description, a context, and within the context, the fragment.

As you may know one can place multiple items of data onto the clipboard for
a single clipboard entry, which means that the same data can be pasted in a
variety of different formats in order to cope with target
applications of varying sophistocation.

The following example shows how to stick CF_TEXT (and CF_HTML)
into the clipboard.
}

{
Vielleicht hast du schon mal probiert, HTML-formatierter Text in die
Zwischenablage zu kopieren mit dem gewöhnlichen CF_TEXT Format.
Wenn man dann z.B in Word (Bearbeiten, Inhalte Einfügen) auswählt,
gibt's das HTML Format aber nicht zur Auswahl.
Lösung: Mit RegisterClipboardFormat das Format CF_HTML registrieren,
dann einen String so formatieren, wie es auf der Microsoft Seite beschrieben
ist (Siehe Link unten) und ihn dann mit der SetClipboardData API in
die Zwischenablage kopieren.
Das folgende Beispiel zeigt, wie man zwei verschiedene Formate (Text und HTML)
in die Zwischenablage einfügen kann.
}

function FormatHTMLClipboardHeader(HTMLText: string): string;
const
CrLf = #13#10;
begin
Result := 'Version:0.9' + CrLf;
Result := Result + 'StartHTML:-1' + CrLf;
Result := Result + 'EndHTML:-1' + CrLf;
Result := Result + 'StartFragment:000081' + CrLf;
Result := Result + 'EndFragment:°°°°°°' + CrLf;
Result := Result + HTMLText + CrLf;
Result := StringReplace(Result, '°°°°°°', Format('%.6d', [Length(Result)]), []);
end;

//The second parameter is optional and is put into the clipboard as CF_HTML.
//Function can be used standalone or in conjunction with the VCL clipboard so long as
//you use the USEVCLCLIPBOARD conditional define
//($define USEVCLCLIPBOARD}
//(and clipboard.open, clipboard.close).
//Code from http://www.lorriman.com
procedure CopyHTMLToClipBoard(const str: string; const htmlStr: string = '');
var
gMem: HGLOBAL;
lp: PChar;
Strings: array[0..1] of string;
Formats: array[0..1] of UINT;
i: Integer;
begin
gMem := 0;
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(OpenClipBoard(0));
{$ENDIF}
try
//most descriptive first as per api docs
Strings[0] := FormatHTMLClipboardHeader(htmlStr);
Strings[1] := str;
Formats[0] := RegisterClipboardFormat('HTML Format');
Formats[1] := CF_TEXT;
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(EmptyClipBoard);
{$ENDIF}
for i := 0 to High(Strings) do
begin
if Strings[i] = '' then Continue;
//an extra "1" for the null terminator
gMem := GlobalAlloc(GMEM_DDESHARE + GMEM_MOVEABLE, Length(Strings[i]) + 1);
{Succeeded, now read the stream contents into the memory the pointer points at}
try
Win32Check(gmem <> 0);
lp := GlobalLock(gMem);
Win32Check(lp <> nil);
CopyMemory(lp, PChar(Strings[i]), Length(Strings[i]) + 1);
finally
GlobalUnlock(gMem);
end;
Win32Check(gmem <> 0);
SetClipboardData(Formats[i], gMEm);
Win32Check(gmem <> 0);
gmem := 0;
end;
finally
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(CloseClipBoard);
{$ENDIF}
end;
end;
0
JulioDelphi Messages postés 2226 Date d'inscription dimanche 5 octobre 2003 Statut Membre Dernière intervention 18 novembre 2010 14
24 avril 2009 à 17:15
Une petite mise en forme serait la bienvenue. Change de navigateur au besoin.
0
thonyboy Messages postés 165 Date d'inscription mercredi 23 avril 2003 Statut Membre Dernière intervention 16 février 2011
24 avril 2009 à 17:21
{
If you've ever tried sticking html into the clipboard using the usual CF_TEXT
format then you might have been disappointed to discover that wysiwyg html
editors paste your offering as if it were just text,
rather than recognising it as html. For that you need the CF_HTML format.
CF_HTML is entirely text format and uses the transformation format UTF-8.
It includes a description, a context, and within the context, the fragment.

As you may know one can place multiple items of data onto the clipboard for
a single clipboard entry, which means that the same data can be pasted in a
variety of different formats in order to cope with target
applications of varying sophistocation.

The following example shows how to stick CF_TEXT (and CF_HTML)
into the clipboard.
}

{
Vielleicht hast du schon mal probiert, HTML-formatierter Text in die
Zwischenablage zu kopieren mit dem gewöhnlichen CF_TEXT Format.
Wenn man dann z.B in Word (Bearbeiten, Inhalte Einfügen) auswählt,
gibt's das HTML Format aber nicht zur Auswahl.
Lösung: Mit RegisterClipboardFormat das Format CF_HTML registrieren,
dann einen String so formatieren, wie es auf der Microsoft Seite beschrieben
ist (Siehe Link unten) und ihn dann mit der SetClipboardData API in
die Zwischenablage kopieren.
Das folgende Beispiel zeigt, wie man zwei verschiedene Formate (Text und HTML)
in die Zwischenablage einfügen kann.
}

function FormatHTMLClipboardHeader(HTMLText: string): string;
const
CrLf = #13#10;
begin
Result := 'Version:0.9' + CrLf;
Result := Result + 'StartHTML:-1' + CrLf;
Result := Result + 'EndHTML:-1' + CrLf;
Result := Result + 'StartFragment:000081' + CrLf;
Result := Result + 'EndFragment:°°°°°°' + CrLf;
Result := Result + HTMLText + CrLf;
Result := StringReplace(Result, '°°°°°°', Format('%.6d', [Length(Result)]), []);
end;

//The second parameter is optional and is put into the clipboard as CF_HTML.
//Function can be used standalone or in conjunction with the VCL clipboard so long as
//you use the USEVCLCLIPBOARD conditional define
//($define USEVCLCLIPBOARD}
//(and clipboard.open, clipboard.close).
//Code from http://www.lorriman.com
procedure CopyHTMLToClipBoard(const str: string; const htmlStr: string = '');
var
gMem: HGLOBAL;
lp: PChar;
Strings: array[0..1] of string;
Formats: array[0..1] of UINT;
i: Integer;
begin
gMem := 0;
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(OpenClipBoard(0));
{$ENDIF}
try
//most descriptive first as per api docs
Strings[0] := FormatHTMLClipboardHeader(htmlStr);
Strings[1] := str;
Formats[0] := RegisterClipboardFormat('HTML Format');
Formats[1] := CF_TEXT;
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(EmptyClipBoard);
{$ENDIF}
for i := 0 to High(Strings) do
begin
if Strings[i] = '' then Continue;
//an extra "1" for the null terminator
gMem := GlobalAlloc(GMEM_DDESHARE + GMEM_MOVEABLE, Length(Strings[i]) + 1);
{Succeeded, now read the stream contents into the memory the pointer points at}
try
Win32Check(gmem <> 0);
lp := GlobalLock(gMem);
Win32Check(lp <> nil);
CopyMemory(lp, PChar(Strings[i]), Length(Strings[i]) + 1);
finally
GlobalUnlock(gMem);
end;
Win32Check(gmem <> 0);
SetClipboardData(Formats[i], gMEm);
Win32Check(gmem <> 0);
gmem := 0;
end;
finally
{$IFNDEF USEVCLCLIPBOARD}
Win32Check(CloseClipBoard);
{$ENDIF}
end;
end;
0
Rejoignez-nous