主要实现了一个自定义的 HTML 按钮功能,通过嵌入 HTML 和 JavaScript 创建按钮,替代标准的 AHK GUI 按钮。

 

; 缺点:略耗内存
Global Html按钮1, Html按钮2

新按钮1 := New HtmlButton("Html按钮1", "确定", "MyButton1_", 11, 10)  ; 这行也可以免掉坐标
新按钮2 := New HtmlButton("Html按钮2", "取消", "MyButton2_", "ys")

Gui Add, Button, Section xs w78 h26, 确定
Gui Add, Button, ys w78 h26, 取消

Gui Show, w200 h90
Return

F1::
if (onoff := !onoff)
    新按钮1.Text("Html按钮1", "Text")
  else
    新按钮1.Text("Html按钮1", "重试")
Return

GuiClose:
  ExitApp

; 按钮事件处理
MyButton1_OnClick() {
  static a := 0
  ToolTip % "执行AHK_1: " a += 1, , ,1
}

MyButton2_OnClick() {
  static a := 0
  ToolTip % "执行AHK_2: " a += 1, , ,2
}


; 用网页按钮样式来代替标准按钮,兼容到XP系统。如果Gui开启的-DPIScale,需要把HtmlButton最后一个参数"DPIScale"设置成非0即可修正匹配。
Class HtmlButton
{
  __New(ButtonGlobalVar, ButtonName, gLabelFunc, OptionsOrX:="", y:="", w:=78 , h:=26, GuiLabel:="", TextColor:="001C30", DPIScale:=False) {
    Static Count:=0
      f := A_Temp "\" A_TickCount "-tmp" ++Count ".DELETEME.html"

    Html_Str =
    (
      <!DOCTYPE html><html><head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <style>body {overflow-x:hidden;overflow-y:hidden;}
        button { color: #%TextColor%;
          background-color: #F4F4F4;
          border-radius:2px;
          border: 1px solid #A7A7A7;
          cursor: pointer; }
        button:hover {background-color: #BEE7FD;}
      </style></head><body>
      <button id="MyButton%Count%" style="position:absolute;left:0px;top:0px;width:%w%px;height:%h%px;font-size:12px;font-family:'Microsoft YaHei UI';" oncontextmenu="return false;">%ButtonName%</button></body></html>
    )
    if (OptionsOrX!="")
      if OptionsOrX is Number
        x := "x" OptionsOrX
       else
        Options := " " OptionsOrX
    (y != "" && y := " y" y)
    Gui, %GuiLabel%Add, ActiveX, %  x . y . " w" w " h" h " v" ButtonGlobalVar . Options, Shell.Explorer
    FileAppend, % Html_Str, % f
    %ButtonGlobalVar%.Navigate("file://" . f)
    , this.Html_Str := Html_Str
    , this.ButtonName := ButtonName
    , this.gLabelFunc := gLabelFunc
    , this.Count := Count 
    , %ButtonGlobalVar%.silent := True
    , this.ConnectEvents(ButtonGlobalVar, f)
    if !DPIScale
      %ButtonGlobalVar%.ExecWB(63, 1, Round((A_ScreenDPI/96*100)*A_ScreenDPI/96) ) ; Fix ActiveX control DPI scaling
  }

  Text(ButtonGlobalVar, ButtonText) {
    Html_Str := StrReplace(this.Html_Str, ">" this.ButtonName "</bu", ">" ButtonText "</bu")
    FileAppend, % Html_Str, % f := A_Temp "\" A_TickCount "-tmp.DELETEME.html"
    %ButtonGlobalVar%.Navigate("file://" . f)
    , this.ConnectEvents(ButtonGlobalVar, f)
  }

  ConnectEvents(ButtonGlobalVar, f) {
    While %ButtonGlobalVar%.readystate != 4 or %ButtonGlobalVar%.busy
      Sleep 5
    this.MyButton := %ButtonGlobalVar%.document.getElementById("MyButton" this.Count)
    , ComObjConnect(this.MyButton, this.gLabelFunc)
    FileDelete, % f
  }
}

 

声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。