[AHK2] 一个窗口示例

发布时间 2023-12-29 17:03:23作者: 落寞的雪

下面是最近写的工具中的一个窗口,具有不错的效果。
值得一提的是使用到了第三方控件ActiveX,嵌入了一个html。

class PasteText extends Gui {

  static destroyed := true

  __New() {
    super.__New('-Caption +Border')
    theme := Light()
    this.BackColor := theme.window_Bgc
    this.SetFont('s12', 'consolas')
    headerBar := this.AddText('h20 Background' theme.headerBar_Bgc, '粘贴文本')
    headerBar.OnEvent('Click', (*) => PostMessage(0xA1, 2))
    edit := this.AddEdit('Section vEdit w650 h400 Background' theme.edit_Bgc)
    WB := this.AddActiveX('xp vActiveX w0 h0', "Shell.Explorer").Value
    WB.Navigate("file:\\G:\AHK\gitee_ahk2\_self\test.html") ; html 路径
    edit.GetPos(&ex, , &ew)
    btn := this.AddButton('ys Section w75', '确定')
    btn.OnEvent('Click', (*) => this.Submit())
    this.AddButton('xp w75', '取消').OnEvent('Click', (*) => this.Des())
    btn.GetPos(&bx, , &bw)
    headerBar.Move(, , bx - ex + bw)
    this.AddStatusBar(, 'init')
    ; 快速编辑
    this.AddButton('xp w75', '清空').OnEvent('Click', (*) => this['Edit'].Value := '')
    this.AddButton('xp w75', '读取').OnEvent('Click', (*) => this['Edit'].Value := A_Clipboard)
    this.AddButton('xp w75', 'HTML').OnEvent('Click', (*) => this._EnableActiveX())
    this.AddButton('xp w75', '行号').OnEvent('Click', (*) => this._AddLineNum())
  }

  _AddLineNum() {
    group := this['Edit'].Value.split('`n')
    final := ''
    flag := false
    index := 1
    for v in group {
      if flag {
        flag := false
        continue
      }
      if !v {
        flag := true
        final .= '`r`n'
      } else {
        flag := false
        final .= index <= 9 ? '0' : ''
        final .= index++ . ' ' . v . '`r`n'
      }
    }
    this['Edit'].Value := final
  }

  _EnableActiveX() {
    static state := true
    if (state) {
      this['Edit'].GetPos(&x, &y, &w, &h)
      this['Edit'].Move(, , 0, 0)
      this['ActiveX'].Move(x, y, w, h)
    } else {
      this['ActiveX'].GetPos(&x, &y, &w, &h)
      this['ActiveX'].Move(, , 0, 0)
      this['Edit'].Move(x, y, w, h)
    }
    state := !state
  }

  static AnimatedShow() {
    static ins ; singleton
    if (PasteText.destroyed) {
      ins := PasteText()
      this.destroyed := false
    } else return
    ins.Show('Minimize')
    WinSetTransparent(0, 'ahk_id' ins.Hwnd)
    ins.Restore()
    ins.GetClientPos(, , , &ch)
    WinSetTransparent(255, 'ahk_id' ins.Hwnd)
    loop ch {
      ins.Move(, , , A_Index)
    }
    ControlFocus(ins['Edit'], 'A')
  }

  static Show(data?) {
    if IsSet(data) && data {
      PasteText._PasteText(data)
    } else {
      PasteText.AnimatedShow()
    }
  }

  Des() {
    this.Destroy()
    PasteText.destroyed := true
  }

  Submit(*) {
    data := this['Edit'].Value
    if (data) {
      this.GetClientPos(, , , &ch)
      while --ch > 0
        this.Move(, , , ch)
      this.Des()
      PasteText._PasteText(data)
    }
  }

  static _PasteText(data) {
    ; do something
  }

}
class Light{
  __New() {
      this.default_Fc := '682d00'
      this.window_Bgc := 'f6eeda'
      this.title_Fc := '682d00'
      this.headerBar_Bgc := 'FFFEF0'
      this.fileNameText_Fc := '682d00'
      this.hanatEmoText_Fc := '682d00'
      this.edit_Bgc := 'fffaf4'
      this.edit_Fc := '165146'
      this.lv_Fc := '00667d'
      this.lv_Bgc := 'fffaed'
      this.statusBar_Bgc := 'e8e1cd'
  }
}