ahk实现全局预览

tc的预览很方便,有时候在微信或者其他什么地方也想调用tc的预览,所以做了一个脚本,可以在任何地方,通过复制文件地址,调用tc预览,可以设置在微信等地方只要复制就可以预览,复制图片复制文件均可预览

效果预览

ahk实现全局预览

ahk实现全局预览

 

代码

GroupAdd,no_preview,ahk_exe TOTALCMD.EXE ;{
GroupAdd,auto_clip,ahk_exe WeChat.exe
GroupAdd,auto_clip,ahk_exe TIM.exe
GroupAdd,auto_clip,ahk_exe QQ.exe
#Persistent
OnClipboardChange("ClipChanged")
//}

;hzn global preview{
ClipChanged(Type) {
EnvGet,COMMANDER_PATH,COMMANDER_PATH
global clip_old
if (Clipboard!=clip_old){
if (WinActive("ahk_group auto_clip") && FileExist(Clipboard))
	run %COMMANDER_PATH%\TOTALCMD.EXE /S=L "%Clipboard%"
}
clip_old:=Clipboard
}
 #IfWinNotActive ahk_group no_preview
F3::
/*
功能解读,复制了网页地址使用可以打开网址,复制了文件路径使用可以调用tc的预览打开
(可以设置qq和微信等复制图片或者文件后自动打开)
其他类型的文本,使用ahk的gui进行预览
*/
if RegExMatch(Clipboard, "^"".*""$"){
    clipboard := RegExReplace(clipboard, "^""")
    clipboard := RegExReplace(clipboard, """$")
}
if RegExMatch(Clipboard, "^(https?|ftp)://[^\s/$.?#].[^\s]*$")
	run %Clipboard%
else if FileExist(Clipboard)
	run %COMMANDER_PATH%\TOTALCMD.EXE /S=L "%Clipboard%"
Else
	Gosub ding_text
Return
#IfWinNotActive
;}

ding_text: ;{
;    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_f3_flag:=0
$+F4::
    if(!shift_f3_flag)
        WinHide ,%A_ScriptName%
    else
        WinShow  ,%A_ScriptName%
    shift_f3_flag:=!shift_f3_flag
    send {Shift down}{F3}{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,% " " ,% "&nbsp;",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
;}

给TA捐赠
共{{data.count}}人
人已捐赠
其他

PaddleOCR 实战

2023-8-27 22:05:12

其他

DD调用,哪里出错了哈,运行没反应

2023-11-27 9:34:08

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索