您的位置:首页 > Web前端 > HTML

[AHK]用HTML做ahk界面

2016-12-07 13:16 417 查看
;activex gui 2 - test  joedf - 2014/09/19
;http://ahkscript.org/boards/viewtopic.php?f=7&t=4588&p=26809#p26809
;~ >Read & Write<
;~ Webpage >> AHK
;~ AHK >> WebPage

#SingleInstance, off
OnExit,OnExit

HTML_page =
( Ltrim Join
<!DOCTYPE html>
<html>
<head>
<style>
body{font-family:sans-serif;background-color:#1A1A1A;color:white}
#title{font-size:36px;}
input{margin:4px;Border: 2px white solid;background-color:black;color:white;}
p{font-size:16px;border:solid 1px #666;padding:4px;}
#footer{text-align:center;}
</style>
</head>
<body>
<div id="title">Hello World</div>
<textarea rows="4" cols="70" id="MyTextBox">1234567890-=\ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$^&*()_+|~</textarea>
<p id="footer">
<input type="button" id="MyButton1" value="Show Content in AHK MsgBox">
<input type="button" id="MyButton2" value="Change Content with AHK">
<input type="button" id="MyButton3" value="Greetings from AHK">
</p>
</body>
</html>
)

Gui Add, ActiveX, x0 y0 w640 h480 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

;Use DOM access just like javascript!
MyButton1 := wb.document.getElementById("MyButton1")
MyButton2 := wb.document.getElementById("MyButton2")
MyButton3 := wb.document.getElementById("MyButton3")
ComObjConnect(MyButton1, "MyButton1_") ;connect button events
ComObjConnect(MyButton2, "MyButton2_")
ComObjConnect(MyButton3, "MyButton3_")
Gui Show, w640 h480
return

GuiClose:
ExitApp
OnExit:
FileDelete,%A_Temp%\*.DELETEME.html ;clean tmp file
ExitApp

; Our Event Handlers
MyButton1_OnClick() {
global wb
MsgBox % wb.Document.getElementById("MyTextBox").Value
}
MyButton2_OnClick() {
global wb
FormatTime, TimeString, %A_Now%, dddd MMMM d, yyyy HH:mm:ss
data := "AHK Version " A_AhkVersion " - " (A_IsUnicode ? "Unicode" : "Ansi") " " (A_PtrSize == 4 ? "32" : "64") "bit`nCurrent time: " TimeString
wb.Document.getElementById("MyTextBox").value := data
}
MyButton3_OnClick() {
MsgBox Hello world!
}
;------------------
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%
WB.Navigate("file://" . f)
}


 


html 与 ahk 伪通信


转自:http://blog.csdn.net/dofy/article/details/8260005

最近帮朋友搞一个东西, 用 ahk 做的壳(全屏无标题置顶窗口, 用 ahk 比较容易实现), 内容是加载的 html (里面包括一些 flash, 和 flash 与 html 的通信), 但这都不是要说的重点, 重点是后来要求 html(js/flash)
控制关闭 ahk, 这个需求难着了我了, 一直没想到如何实现.

今天无意间又看了一下 ahk 帮助文档关于 activeX 的部分, 发现例子里有关于用 ComObjConnect 处理对象公开事件的方法, 于是想到了一个比较巧妙的方法, 实现非常简单, 以至于都不好意思写一篇文章来介绍这个方法, 但还是记录一下, 因为确实很好用.

ahk 代码:

[plain] view
plain copy

 print?

#NoEnv  

#SingleInstance focus  

  

Gui, Add, ActiveX, w700 h400 vWB, Shell.Explorer  

WB.Navigate(A_ScriptDir . "\index.html")  

; 重点来了  

ComObjConnect(WB, WB_events)   

  

Gui, Show  

  

Return  

  

class WB_events  

{  

    ; 处理方法, 当然也可以监听其他事件  

    ; 参考: http://msdn.microsoft.com/en-us/library/aa768334  

    NavigateComplete2(wb, url)  

    {  

        ; 用 SplitPath 简单解析一下很方便  

        SplitPath, url, fullCmd, DISCARD, opt, cmd  

  

        if (cmd == "test")  

        {  

            traytip, Test, This is "test" command.  

        }  

        else if (cmd == "__CLOSE__" )  

        {  

            ExitApp  

        }  

        else  

        {  

            traytip, %fullCmd%, cmd: %cmd%`, opt: %opt%  

        }  

    }  

}  

  

GuiEscape:  

GuiClose:  

exitapp  

html 代码:

[html] view
plain copy

 print?

<ul>  

    <li ><a href="test" target="_cp" >test</a></li>  

    <li ><a href="command.options" target="_cp" >aaa.bbb</a></li>  

    <li ><a href="__CLOSE__" target="_cp" >close</a></li>  

</ul>  

  

<iframe name="_cp" src="about:blank" style="display:none;"></iframe>  

简要说明:

其实就是在 html 里放一个用来接收"命令" 的 iframe, 将需要发送的"命令"写在链接上, ahk 中监听 NavigateComplete2 事件, 处理函数中解析 url (格式自己随便定义了, 我想到个简单规则就是 command.options, 这样很容易利用 SplitPath 方法把各部分解析出来加以利用, 接下来做什么就全交给 ahk 了 ;)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: