Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Copy HTML Text into the Clipboard

Title: copy HTML Text into the Clipboard? { Ifyou'veevertriedstickinghtmlintotheclipboardusingtheusualCF_TEXT formatthenyoumighthavebeendisappointedtodiscoverthatwysiwyghtml editorspasteyourofferingasifitwerejusttext, ratherthanrecognisingitashtml.ForthatyouneedtheCF_HTMLformat. CF_HTMLisentirelytextformatandusesthetransformationformatUTF-8. Itincludesadescription,acontext,andwithinthecontext,thefragment. Asyoumayknowonecanplacemultipleitemsofdataontotheclipboardfor asingleclipboardentry,whichmeansthatthesamedatacanbepastedina varietyofdifferentformatsinordertocopewithtarget applicationsofvaryingsophistocation. ThefollowingexampleshowshowtostickCF_TEXT(andCF_HTML) intotheclipboard. } { Vielleichthastduschonmalprobiert,HTML-formatierterTextindie ZwischenablagezukopierenmitdemgewhnlichenCF_TEXTFormat. Wennmandannz.BinWord(Bearbeiten,InhalteEinfgen)auswhlt, gibt'sdasHTMLFormatabernichtzurAuswahl. Lsung:MitRegisterClipboardFormatdasFormatCF_HTMLregistrieren, danneinenStringsoformatieren,wieesaufderMicrosoftSeitebeschrieben ist(SieheLinkunten)undihndannmitderSetClipboardDataAPIin dieZwischenablagekopieren. DasfolgendeBeispielzeigt,wiemanzweiverschiedeneFormate(TextundHTML) indieZwischenablageeinfgenkann. } functionFormatHTMLClipboardHeader(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; //ThesecondparameterisoptionalandisputintotheclipboardasCF_HTML. //FunctioncanbeusedstandaloneorinconjunctionwiththeVCLclipboardsolongas //youusetheUSEVCLCLIPBOARDconditionaldefine //($defineUSEVCLCLIPBOARD} //(andclipboard.open,clipboard.close). //Codefromhttp://www.lorriman.com procedureCopyHTMLToClipBoard(conststr:string;consthtmlStr:string=''); var gMem:HGLOBAL; lp:PChar; Strings:array[0..1]ofstring; Formats:array[0..1]ofUINT; i:Integer; begin gMem:=0; {$IFNDEFUSEVCLCLIPBOARD} Win32Check(OpenClipBoard(0)); {$ENDIF} try //mostdescriptivefirstasperapidocs Strings[0]:=FormatHTMLClipboardHeader(htmlStr); Strings[1]:=str; Formats[0]:=RegisterClipboardFormat('HTMLFormat'); Formats[1]:=CF_TEXT; {$IFNDEFUSEVCLCLIPBOARD} Win32Check(EmptyClipBoard); {$ENDIF} fori:=0toHigh(Strings)do begin ifStrings[i]=''thenContinue; //anextra"1"forthenullterminator gMem:=GlobalAlloc(GMEM_DDESHARE+GMEM_MOVEABLE,Length(Strings[i])+1); {Succeeded,nowreadthestreamcontentsintothememorythepointerpointsat} try Win32Check(gmem0); lp:=GlobalLock(gMem); Win32Check(lpnil); CopyMemory(lp,PChar(Strings[i]),Length(Strings[i])+1); finally GlobalUnlock(gMem); end; Win32Check(gmem0); SetClipboardData(Formats[i],gMEm); Win32Check(gmem0); gmem:=0; end; finally {$IFNDEFUSEVCLCLIPBOARD} Win32Check(CloseClipBoard); {$ENDIF} end; end; //Example: procedureTForm1.Button1Click(Sender:TObject); begin CopyHTMLToClipBoard('SwissDelphiCenter','SwissDelphiCenter'); end;