|
| 1 | +//go:build windows |
| 2 | +// +build windows |
| 3 | + |
| 4 | +package autoit |
| 5 | + |
| 6 | +import ( |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "syscall" |
| 10 | + "unsafe" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + SWShowNormal = 1 |
| 15 | + INTDEFAULT = -2147483647 |
| 16 | + DefaultMouseButton = "left" |
| 17 | +) |
| 18 | + |
| 19 | +// HWND -- window handle |
| 20 | +type HWND uintptr |
| 21 | + |
| 22 | +// RECT -- http://msdn.microsoft.com/en-us/library/windows/desktop/dd162897.aspx |
| 23 | +type RECT struct { |
| 24 | + Left, Top, Right, Bottom int32 |
| 25 | +} |
| 26 | + |
| 27 | +// POINT -- |
| 28 | +type POINT struct { |
| 29 | + X, Y int32 |
| 30 | +} |
| 31 | + |
| 32 | +var ( |
| 33 | + dll64 *syscall.LazyDLL |
| 34 | + controlClick *syscall.LazyProc |
| 35 | + controlSend *syscall.LazyProc |
| 36 | + run *syscall.LazyProc |
| 37 | + winActivate *syscall.LazyProc |
| 38 | + winWait *syscall.LazyProc |
| 39 | +) |
| 40 | + |
| 41 | +func LoadAuto() { |
| 42 | + autoItX3 := "AutoItX3_x64.dll" |
| 43 | + if (^uint(0) >> 63) == 0 { |
| 44 | + autoItX3 = "AutoItX3.dll" |
| 45 | + } |
| 46 | + dll64 := syscall.NewLazyDLL(filepath.Join(filepath.Dir(os.Args[0]), autoItX3)) |
| 47 | + controlClick = dll64.NewProc("AU3_ControlClick") |
| 48 | + controlSend = dll64.NewProc("AU3_ControlSend") |
| 49 | + run = dll64.NewProc("AU3_Run") |
| 50 | + winActivate = dll64.NewProc("AU3_WinActivate") |
| 51 | + winWait = dll64.NewProc("AU3_WinWait") |
| 52 | +} |
| 53 | + |
| 54 | +// Run -- Run a windows program |
| 55 | +// flag 3(max) 6(min) 9(normal) 0(hide) |
| 56 | +func Run(szProgram string, args ...interface{}) int { |
| 57 | + var szDir string |
| 58 | + var flag int |
| 59 | + var ok bool |
| 60 | + if len(args) == 0 { |
| 61 | + szDir = "" |
| 62 | + flag = SWShowNormal |
| 63 | + } else if len(args) == 1 { |
| 64 | + if szDir, ok = args[0].(string); !ok { |
| 65 | + panic("szDir must be a string") |
| 66 | + } |
| 67 | + flag = SWShowNormal |
| 68 | + } else if len(args) == 2 { |
| 69 | + if szDir, ok = args[0].(string); !ok { |
| 70 | + panic("szDir must be a string") |
| 71 | + } |
| 72 | + if flag, ok = args[1].(int); !ok { |
| 73 | + panic("flag must be a int") |
| 74 | + } |
| 75 | + } else { |
| 76 | + panic("Too more parameter") |
| 77 | + } |
| 78 | + pid, _, lastErr := run.Call(strPtr(szProgram), strPtr(szDir), intPtr(flag)) |
| 79 | + if int(pid) == 0 { |
| 80 | + println(lastErr) |
| 81 | + } |
| 82 | + return int(pid) |
| 83 | +} |
| 84 | + |
| 85 | +// WinWait -- wait window to active |
| 86 | +func WinWait(szTitle string, args ...interface{}) int { |
| 87 | + var szText string |
| 88 | + var nTimeout int |
| 89 | + var ok bool |
| 90 | + if len(args) == 0 { |
| 91 | + szText = "" |
| 92 | + nTimeout = 0 |
| 93 | + } else if len(args) == 1 { |
| 94 | + if szText, ok = args[0].(string); !ok { |
| 95 | + panic("szText must be a string") |
| 96 | + } |
| 97 | + nTimeout = 0 |
| 98 | + } else if len(args) == 2 { |
| 99 | + if szText, ok = args[0].(string); !ok { |
| 100 | + panic("szText must be a string") |
| 101 | + } |
| 102 | + if nTimeout, ok = args[1].(int); !ok { |
| 103 | + panic("nTimeout must be a int") |
| 104 | + } |
| 105 | + } else { |
| 106 | + panic("Too more parameter") |
| 107 | + } |
| 108 | + |
| 109 | + handle, _, lastErr := winWait.Call(strPtr(szTitle), strPtr(szText), intPtr(nTimeout)) |
| 110 | + if int(handle) == 0 { |
| 111 | + println(lastErr) |
| 112 | + } |
| 113 | + return int(handle) |
| 114 | +} |
| 115 | + |
| 116 | +// ControlClick -- Sends a mouse click command to a given control. |
| 117 | +func ControlClick(title, text, control string, args ...interface{}) int { |
| 118 | + var button string |
| 119 | + var x, y, nClicks int |
| 120 | + var ok bool |
| 121 | + |
| 122 | + if len(args) == 0 { |
| 123 | + button = DefaultMouseButton |
| 124 | + nClicks = 1 |
| 125 | + x = INTDEFAULT |
| 126 | + y = INTDEFAULT |
| 127 | + } else if len(args) == 1 { |
| 128 | + if button, ok = args[0].(string); !ok { |
| 129 | + panic("button must be a string") |
| 130 | + } |
| 131 | + nClicks = 1 |
| 132 | + x = INTDEFAULT |
| 133 | + y = INTDEFAULT |
| 134 | + } else if len(args) == 2 { |
| 135 | + if button, ok = args[0].(string); !ok { |
| 136 | + panic("button must be a string") |
| 137 | + } |
| 138 | + if nClicks, ok = args[1].(int); !ok { |
| 139 | + panic("nClicks must be a int") |
| 140 | + } |
| 141 | + x = INTDEFAULT |
| 142 | + y = INTDEFAULT |
| 143 | + } else if len(args) == 4 { |
| 144 | + if button, ok = args[0].(string); !ok { |
| 145 | + panic("button must be a string") |
| 146 | + } |
| 147 | + if nClicks, ok = args[1].(int); !ok { |
| 148 | + panic("nClicks must be a int") |
| 149 | + } |
| 150 | + if x, ok = args[2].(int); !ok { |
| 151 | + panic("x must be a int") |
| 152 | + } |
| 153 | + if y, ok = args[3].(int); !ok { |
| 154 | + panic("y must be a int") |
| 155 | + } |
| 156 | + } else { |
| 157 | + panic("Error parameters") |
| 158 | + } |
| 159 | + ret, _, lastErr := controlClick.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(button), intPtr(nClicks), intPtr(x), intPtr(y)) |
| 160 | + if int(ret) == 0 { |
| 161 | + println(lastErr) |
| 162 | + } |
| 163 | + return int(ret) |
| 164 | +} |
| 165 | + |
| 166 | +// WinActivate ( "title" [, "text"]) int |
| 167 | +func WinActivate(title string, args ...interface{}) int { |
| 168 | + text := "" |
| 169 | + var ok bool |
| 170 | + argsLen := len(args) |
| 171 | + if argsLen > 1 { |
| 172 | + panic("argument count > 2") |
| 173 | + } |
| 174 | + if argsLen == 1 { |
| 175 | + if text, ok = args[0].(string); !ok { |
| 176 | + panic("text must be a string") |
| 177 | + } |
| 178 | + } |
| 179 | + ret, _, lastErr := winActivate.Call(strPtr(title), strPtr(text)) |
| 180 | + if int(ret) == 0 { |
| 181 | + println(lastErr) |
| 182 | + } |
| 183 | + return int(ret) |
| 184 | +} |
| 185 | + |
| 186 | +// ControlSend -- Sends a string of characters to a control. |
| 187 | +func ControlSend(title, text, control, sendText string, args ...interface{}) int { |
| 188 | + var nMode int |
| 189 | + var ok bool |
| 190 | + if len(args) == 0 { |
| 191 | + nMode = 0 |
| 192 | + } else if len(args) == 1 { |
| 193 | + if nMode, ok = args[0].(int); !ok { |
| 194 | + panic("nMode must be a int") |
| 195 | + } |
| 196 | + } else { |
| 197 | + panic("Too more parameter") |
| 198 | + } |
| 199 | + ret, _, lastErr := controlSend.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(sendText), intPtr(nMode)) |
| 200 | + if int(ret) == 0 { |
| 201 | + println(lastErr) |
| 202 | + } |
| 203 | + return int(ret) |
| 204 | +} |
| 205 | + |
| 206 | +func findTermChr(buff []uint16) int { |
| 207 | + for i, char := range buff { |
| 208 | + if char == 0x0 { |
| 209 | + return i |
| 210 | + } |
| 211 | + } |
| 212 | + panic("not supposed to happen") |
| 213 | +} |
| 214 | + |
| 215 | +func intPtr(n int) uintptr { |
| 216 | + return uintptr(n) |
| 217 | +} |
| 218 | + |
| 219 | +func strPtr(s string) uintptr { |
| 220 | + return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s))) |
| 221 | +} |
0 commit comments