robotgo_win.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
  2. // file at the top-level directory of this distribution and at
  3. // https://github.com/go-vgo/robotgo/blob/master/LICENSE
  4. //
  5. // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
  6. // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
  7. // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
  8. // option. This file may not be copied, modified, or distributed
  9. // except according to those terms.
  10. //go:build windows
  11. // +build windows
  12. package robotgo
  13. import (
  14. "syscall"
  15. "unsafe"
  16. // "github.com/lxn/win"
  17. "github.com/tailscale/win"
  18. )
  19. // FindWindow find window hwnd by name
  20. func FindWindow(name string) win.HWND {
  21. hwnd := win.FindWindow(nil, syscall.StringToUTF16Ptr(name))
  22. return hwnd
  23. }
  24. // GetHWND get foreground window hwnd
  25. func GetHWND() win.HWND {
  26. hwnd := win.GetForegroundWindow()
  27. return hwnd
  28. }
  29. // SendInput send n input event
  30. func SendInput(nInputs uint32, pInputs unsafe.Pointer, cbSize int32) uint32 {
  31. return win.SendInput(nInputs, pInputs, cbSize)
  32. }
  33. // SendMsg send a message with hwnd
  34. func SendMsg(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uintptr {
  35. return win.SendMessage(hwnd, msg, wParam, lParam)
  36. }
  37. // SetActiveWindow set window active with hwnd
  38. func SetActiveWindow(hwnd win.HWND) win.HWND {
  39. return win.SetActiveWindow(hwnd)
  40. }
  41. // SetFocus set window focus with hwnd
  42. func SetFocus(hwnd win.HWND) win.HWND {
  43. return win.SetFocus(hwnd)
  44. }
  45. // SetForeg set the window into the foreground by hwnd
  46. func SetForeg(hwnd win.HWND) bool {
  47. return win.SetForegroundWindow(hwnd)
  48. }
  49. // GetMain get the main display hwnd
  50. func GetMain() win.HWND {
  51. return win.GetActiveWindow()
  52. }
  53. // GetMainId get the main display id
  54. func GetMainId() int {
  55. return int(GetMain())
  56. }
  57. // ScaleF get the system scale value
  58. // if "displayId == -2" this function will get the desktop scale value
  59. func ScaleF(displayId ...int) (f float64) {
  60. if len(displayId) > 0 && displayId[0] != -1 {
  61. if displayId[0] >= 0 {
  62. dpi := GetDPI(win.HWND(displayId[0]))
  63. f = float64(dpi) / 96.0
  64. }
  65. if displayId[0] == -2 {
  66. f = float64(GetDPI(GetDesktopWindow())) / 96.0
  67. }
  68. } else {
  69. f = float64(GetMainDPI()) / 96.0
  70. }
  71. if f == 0.0 {
  72. f = 1.0
  73. }
  74. return f
  75. }
  76. // GetDesktopWindow get the desktop window hwnd id
  77. func GetDesktopWindow() win.HWND {
  78. return win.GetDesktopWindow()
  79. }
  80. // GetMainDPI get the display dpi
  81. func GetMainDPI() int {
  82. return int(GetDPI(GetHWND()))
  83. }
  84. // GetDPI get the window dpi
  85. func GetDPI(hwnd win.HWND) uint32 {
  86. return win.GetDpiForWindow(hwnd)
  87. }
  88. // GetSysDPI get the system metrics dpi
  89. func GetSysDPI(idx int32, dpi uint32) int32 {
  90. return win.GetSystemMetricsForDpi(idx, dpi)
  91. }