[AHK2] 为toolwindow窗口添加阴影效果

发布时间 2023-12-29 16:58:12作者: 落寞的雪

如下示例

  g := Gui('-Caption +Border') ; 或包含 +ToolWindow
  g.SetFont('s13', 'consolas')
  g.AddText('c4c071d', data)
  g.BackColor := 'f6eeda'
  g.Show('x100 y50')
  WinSetTransColor('white', g)
  FrameShadow(g.Hwnd)
FrameShadow(hwnd) {
  enabled := 0
  DllCall("dwmapi\DwmIsCompositionEnabled", "int*", &enabled)
  if !enabled {
    DllCall("SetClassLong", "uint", hwnd, "int", -26, "int", 0x20000)
  }
  else {
    bf := Buffer(4)
    NumPut('uint', -1, bf) ; 要应用此阴影效果,首先要将white设置为透明: WinSetTransColor('white', g)
    DllCall("dwmapi\DwmSetWindowAttribute", "ptr", hwnd, "uint", 2, "int*", 2, "uint", 4)
    DllCall("dwmapi\DwmExtendFrameIntoClientArea", "ptr", hwnd, "ptr", bf)
  }
}

但这不使用于所有情况,如果控件本身有白色则不适合使用。
而是使用下面的替代方案:

FrameShadow(hwnd) {
  enabled := 0
  DllCall("dwmapi\DwmIsCompositionEnabled", "int*", &enabled)
  if !enabled {
    DllCall("SetClassLong", "uint", hwnd, "int", -26, "int", 0x20000)
  }
  else {
    bf := Buffer(16)
    NumPut('uint', 0, bf)
    NumPut('uint', 0, bf, 4)
    NumPut('uint', 0, bf, 8)
    NumPut('uint', 1, bf, 12)
    DllCall("dwmapi\DwmSetWindowAttribute", "ptr", hwnd, "uint", 2, "int*", 2, "uint", 16)
    DllCall("dwmapi\DwmExtendFrameIntoClientArea", "ptr", hwnd, "ptr", bf)
  }
}

这样做的缺陷是会在底部留下空白(非0的边)。