实现一个悬浮在桌面上的 富文本展示工具,从剪贴板中获取 HTML 格式或纯文本格式的内容,渲染为富文本内容并显示在 GUI 中。用户可以通过快捷键控制显示或隐藏 GUI,且支持简单的交互功能。
; By Tmz
; ahk的gui中调用activeX空间渲染从粘贴板中获取的带格式的文本(html格式),然后通过快捷键悬浮在桌面上。
; F1:显示gui
; Shift+F1:隐藏/显示gui
; 拖动:选中任意文本内容,然后鼠标变为箭头即可拖动
; ESC : 退出当前选中gui
#SingleInstance, off
OnExit,OnExit
$F1::
; xx1=C:\Users\Administrator\Desktop\xx1.html
; xx2=C:\Users\Administrator\Desktop\xx2.html
; xx3=C:\Users\Administrator\Desktop\xx3.html
IfExist, %xx1%
FileDelete ,%xx1%
IfExist, %xx2%
FileDelete ,%xx2%
IfExist, %xx3%
FileDelete ,%xx3%
;获取容器高度
html:=ClipboardGet_HTML("html")
FileAppend, %html%,%xx1% ,UTF-8
if(html=0) ;普通文本格式
{
;组装html
html:=getHtml()
FileAppend, %html%, %xx3%,UTF-8
}
html:=changeHtml(html)
if(html=0)
{
html:=getHtml()
html:=changeHtml(html)
}
FileAppend, %html%, %xx2%,UTF-8
HTML_page:=html
width_outter:=getClipboardMaxLineWidthPX()
width_outter:=width_outter<250?250:width_outter
width_outter:=width_outter>850?850:width_outter
width_inner:=width_outter+20
wpx:= "w" width_outter
wpx2:="w" width_inner
; wpx2:="w600"
Gui New
Gui Add, ActiveX, x0 y0 %wpx2% h870 vWB, Shell.Explorer ; The final parameter is the name of the ActiveX component.
WB.silent := true ;Surpress JS Error boxes
Display(WB,HTML_page)
;Wait for IE to load the page, before we connect the event handlers
while WB.readystate != 4 or WB.busy
sleep 10
;;获取内部div高度
hdiv:=WB.document.getElementById("mainDiv").offsetHeight
hdiv:=hdiv>850?850:hdiv
hpx:="h" hdiv
;Use DOM access just like javascript!
Gui +AlwaysOntop
Gui -Caption +Border +ToolWindow
Gui Show, %wpx% %hpx%
return
;隐藏或显示桌面上的展示,贴图操作
DetectHiddenText, On
shift_f1_flag:=0
$+F1::
if(!shift_f1_flag)
WinHide ,%A_ScriptName%
else
WinShow ,%A_ScriptName%
shift_f1_flag:=!shift_f1_flag
send {Shift down}{F1}{Shift up}
return
GuiClose:
Gui Destroy
OnExit:
FileDelete,%A_Temp%\*.DELETEME.html ;clean tmp file
Gui Destroy
return
GuiEscape:
Gui Destroy
return
;------------------
Display(WB,html_str) {
Count:=0
while % FileExist(f:=A_Temp "\" A_TickCount A_NowUTC "-tmp" Count ".DELETEME.html")
Count+=1
FileAppend,%html_str%,%f%,UTF-8
WB.Navigate("file://" . f)
}
;获取粘贴板的html格式数据
ClipboardGet_HTML( byref Data ) { ; http://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 )
, Data := StrGet( pData, dataL, "UTF-8" )
, DllCall( "GlobalUnlock", UInt,hData )
DllCall( "CloseClipboard" )
Return dataL ? Data : 0
}
;获取粘贴板的html,并添加一些内容<head>...</head> 和 <div>....<div>
changeHtml(clipHtml)
{
;屏蔽浏览器间隙
head=<html><head><style>*{margin: 0px;padding: 0px;}#mainDiv{overflow-x: visible;padding: 10px;}</style></head>
;主div用于测量高度
beginDiv=<body id="body1"><div id="mainDiv" contenteditable="true" >
endDiv=</div></body>
js=<script language="javascript" type="text/javascript">if(document.getElementsByTagName("pre")[0]!=null){var rgb = document.getElementsByTagName("pre")[0].style.backgroundColor;document.getElementById("body1").style.backgroundColor = rgb;document.getElementById("body1").style.backgroundColor = rgb;}document.onkeypress = function() {var evt = (evt) ? evt : window.event;if(evt.keyCode==80){document.execCommand("Copy");}evt.keyCode = 0;evt.returnValue = false }</script>
endDiv:=endDiv js
beginCount:=0
htmlTxt:=""
Loop, parse, clipHtml, `n, `r
{
index:=A_Index
if(index=6 &&!A_LoopField ) ;不处理office系列的各种格式
return 0
if index> 6
{
tempField:=A_LoopField
if(index=7 && strBeginWith(tempField,"<html>"))
StringReplace, tempField, tempField, <html> , % head
if(inStr(tempField,"<body>") && !beginCount)
{
StringReplace, tempField, tempField, <body>, % beginDiv
beginCount+=1
}
htmlTxt:=htmlTxt tempField "`r`n"
}
}
StringGetPos, position, htmlTxt, </body> ,R
str1:=SubStr(htmlTxt,1,positIon)
str2:=SubStr(htmlTxt,position+1)
StringReplace, str2, str2, </body>, % endDiv
htmlTxt:=str1 str2
return htmlTxt
}
;获取粘贴板最长行的像素
getClipboardMaxLineWidthPX()
{
a_length:=9 ;字符占用宽度
b_length:=18 ;汉字占用宽度
clip:=clipboard
max_width_px:=0
Loop, parse, clip, `n, `r
{
index:=A_Index
line:=A_loopfield
temp_width_px:=0
Loop, parse, line
{
char:=A_loopfield
if chr(char)<128
temp_width_px:=temp_width_px+a_length
else
temp_width_px:=temp_width_px+b_length
}
;msgBox % "temp_width_px:"temp_width_px " max_width_px:"max_width_px
if(temp_width_px >max_width_px)
max_width_px:=temp_width_px
}
return max_width_px
}
;粘贴板文本转为html格式文件
getHtml()
{
clip:=clipboard
head=<html><head><meta charset="utf-8"><style>body { background-color:#3c3f41; font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;font-size:15px; color:#ffffff;line-height:18px;letter-spacing:1.06px}</style></head><body>
head:= "1`r`n2`r`n3`r`n4`r`n5`r`n6`r`n" head
Loop, parse, clip, `n, `r
{
line:=A_loopField
StringReplace, line, line,% " " ,% " ",All
head:=head "<p>" line "</p>"
}
head:=head "</body></html>"
return head
}
strBeginWith(str,suffix)
{
return str!=LTrim(str,suffix)
}
;移动选框
WM_LBUTTONDOWN(){
Static init:=OnMessage(0x0201, "WM_LBUTTONDOWN")
if(A_Cursor="Arrow")
PostMessage, 0xA1, 2
}
;当前窗口GUI界面
#If WinActive(A_ScriptName)
$^c::
;获取选中文本
; tooltip ctl+c
ControlSend,% "Internet Explorer_Server1" , P, %A_ScriptName%
return
; msgBox % VB
#If
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。

评论(0)