This is my Solution to get copying "Html Format" to the clipboard. This was tested on Windows 7 x64 AutoHotkkey_L v1.1.05.04.
I found no issues so far with this script.
Code:
CF_HTML := "HTML Format"
pushHTML(sHtmlFragment, title = "", sourceUrl = "")
{
Header =
(
Version:1.0
StartHTML:aaaaaaaa
EndHTML:bbbbbbbb
StartFragment:cccccccc
EndFragment:dddddddd
StartSelection:cccccccc
EndSelection:cccccccc
)
; http://blogs.msdn.com/b/jmstall/archive/2007/01/21/sample-code-html-clipboard.aspx
Pre := "<!DOCTYPE HTML PUBLIC ""-//W3C//DTD HTML 4.0 Transitional//EN""><HTML><HEAD><TITLE>" . title . "</TITLE></HEAD><BODY><!--StartFragment-->"
Post := "<!--EndFragment--></BODY></HTML>"
offSet := 6
sData := Header
if (sourceUrl){
sData := sData . "SourceURL:" . sourceUrl
}
iStartHTML := StrLen(sData) + offSet
sData := sData . Pre
iStartFrag := StrLen(sData) + offSet
sData := sData . sHtmlFragment
iEndFrag := StrLen(sData) + offSet
sData := sData . Post
iEndHTML := StrLen(sData) + offSet
iPadLen := 8
StringReplace sData, sData, aaaaaaaa, % StrPad(iStartHTML, "0", iPadLen)
StringReplace sData, sData, bbbbbbbb, % StrPad(iEndHTML, "0", iPadLen)
StringReplace sData, sData, cccccccc, % StrPad(iStartFrag, "0", iPadLen), All
StringReplace sData, sData, dddddddd, % StrPad(iEndFrag, "0", iPadLen)
Return, sData
}
StrPad(Str, PadChar,PadLen,Left=1)
{
StringLen, sLen, str
if (sLen >= PadLen)
return str
sDif := PadLen - sLen
strPad := ""
Loop, %sDif%
{
strPad := strPad . PadChar
}
Retval := ""
If (Left=1)
{
Retval := strPad . Str
}
else
{
Retval := str . strPad
}
return Retval
}
SetClipboardData(_format, ByRef @data, _dataSize=0, _bEmptyClipboard=true)
{
local res, mem, str, cfFormat, errorCode
errorCode = 0
If (_dataSize = 0)
{
; Assume it is a simple string; otherwise, should provide real length...
_dataSize := StrLen(@data)
If (_dataSize = 0)
{
errorCode = SIZE
Goto SCD_End
}
}
; Open the clipboard, and empty it.
res := DllCall("OpenClipboard", "Uint", 0)
If (res = 0)
{
errorCode = OC
Goto SCD_End
}
If (_bEmptyClipboard)
{
DllCall("EmptyClipboard")
}
; Allocate a global memory object for the text
mem := DllCall("GlobalAlloc"
, "Uint", 2 ; GMEM_MOVEABLE
, "Uint", _dataSize + 1) ; +1 in case it is a zero-terminated string
If (mem = 0)
{
errorCode = GA
Goto SCD_End
}
; Lock the handle and copy the text to the buffer
str := DllCall("GlobalLock", "Uint", mem)
; str is a pointer to a memory area, so is treated as Uint
;~ ; In case it is a zero-terminated string, we put the final zero
StrPut(@data, str , _dataSize +1,Encoding = "utf-16")
DllCall("GlobalUnlock", "Uint", mem)
; Handle format
If _format is integer
{
cfFormat := _format
}
Else
{
cfFormat := DllCall("RegisterClipboardFormat", "Str", _format)
If (cfFormat = 0)
{
errorCode = RCF
Goto SCD_End
}
}
; Place the handle on the clipboard
res := DllCall("SetClipboardData"
, "Uint", cfFormat
, "Uint", mem)
If (res = 0)
{
errorCode = SCD
Goto SCD_End
}
SCD_End:
If errorCode != 0
{
errorCode = %errorCode% (%A_LastError%)
}
; Close the clipboard
DllCall("CloseClipboard")
ErrorLevel := errorCode
}
; only needed if you want to retrieve the html from the clipboard
ClipboardGet_HTML( byref Data ) { ; www.autohotkey.com/forum/viewtopic.php?p=392624#392624
If CBID := DllCall( "RegisterClipboardFormat", Str,"HTML Format", Uint )
If DllCall( "IsClipboardFormatAvailable", Uint,CBID ) <> 0
If DllCall( "OpenClipboard", Uint,0 ) <> 0
If hData := DllCall( "GetClipboardData", Uint,CBID, Uint )
DataL := DllCall( "GlobalSize", Uint,hData, Uint )
, pData := DllCall( "GlobalLock", Uint,hData, Uint )
, VarSetCapacity( data, dataL * ( A_IsUnicode ? 2 : 1 ) ), StrGet := "StrGet"
, A_IsUnicode ? Data := %StrGet%( pData, dataL, 0 )
: DllCall( "lstrcpyn", Str,Data, Uint,pData, Uint,DataL )
, DllCall( "GlobalUnlock", Uint,hData )
DllCall( "CloseClipboard" )
Return dataL ? dataL : 0
}
Simple Example:
Code:
RawHtml := "<h2><span style='color: rgb(0, 100, 0);'><u>Hello World</u></span></h2>"
Html := pushHTML(RawHtml, "My Hello World")
SetClipboardData(CF_HTML, Html)
MsgBox, % Html
MsgBox, Html is now on the clipboard and can be pasted into a Html editor.