[AHK2-UI] 实现自己的Show()方法

发布时间 2023-08-23 14:44:33作者: 落寞的雪

为什么

这其实是一种两阶段XX的设计模式,比如两阶段终止:调用终止方法时并不立即终止,而是设置终止信号,由别人自身决定终止的操作。

同样,实现Show()方法算是一种两阶段启动:外部调用Show()方法时,由自身决定show前做什么,show后又做什么,以及如何show。

例子

这是一个Show()方法:

static Show(config, *) {
  ; singleton
  for w in WinGetList('ahk_pid ' ProcessExist())
    if (g := GuiFromHwnd(w)) && g.base = this.Prototype
      return g.Show()
  ; =========Show()前

  ins := PlanEGui(config.theme)
  ins.Show('Minimize')

  ; =========Show()后
  WinSetTransparent(0, 'ahk_id' ins.Hwnd)
  ins.Restore()

  ins.GetClientPos(, , , &ch)
  ins.Move(, A_ScreenHeight / 4, , 0)

  WinSetTransparent(255, 'ahk_id' ins.Hwnd)
  loop ch {
    ins.Move(, , , A_Index)
  }

  ins.AddText('xm+5 h800 w400 vHistory c' ins.currTheme.edit_Fc,)
  ins['History'].SetFont('s12')
  ControlFocus ins['SendBox'].Hwnd, "A"
}

在这个Show()方法中,

  • show前:判断是否已存在当前脚本的实例,如果有返回此实例。
  • show时:设置必须的gui选项并组合传入的option。
  • show后:
    • 实现更好的启动画面:将窗口实例透明度设为0,然后恢复窗口并将宽设为0,再逐渐恢复宽度,使窗口缓慢出现。
    • 为实例进行额外的操作。
    • 聚焦期望的控件。