goautoit.go 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188
  1. //go:build windows && amd64
  2. // +build windows,amd64
  3. package goautoit
  4. import (
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "runtime"
  9. "syscall"
  10. "unicode/utf16"
  11. "unsafe"
  12. )
  13. // properties available in AutoItX.
  14. const (
  15. SWHide = 0
  16. SWMaximize = 3
  17. SWMinimize = 6
  18. SWRestore = 9
  19. SWShow = 5
  20. SWShowDefault = 10
  21. SWShowMaximized = 3
  22. SWShowMinimized = 2
  23. SWShowminNoActive = 7
  24. SWShowMa = 8
  25. SWShowNoActive = 4
  26. SWShowNormal = 1
  27. INTDEFAULT = -2147483647
  28. DefaultMouseButton = "left"
  29. )
  30. // HWND -- window handle
  31. type HWND uintptr
  32. // RECT -- http://msdn.microsoft.com/en-us/library/windows/desktop/dd162897.aspx
  33. type RECT struct {
  34. Left, Top, Right, Bottom int32
  35. }
  36. // POINT --
  37. type POINT struct {
  38. X, Y int32
  39. }
  40. var (
  41. dll64 *syscall.LazyDLL
  42. clipGet *syscall.LazyProc
  43. clipPut *syscall.LazyProc
  44. controlClick *syscall.LazyProc
  45. controlClickByHandle *syscall.LazyProc
  46. controlCommand *syscall.LazyProc
  47. controlCommandByHandle *syscall.LazyProc
  48. controlDisable *syscall.LazyProc
  49. controlDisableByHandle *syscall.LazyProc
  50. controlEnable *syscall.LazyProc
  51. controlEnableByHandle *syscall.LazyProc
  52. controlFocus *syscall.LazyProc
  53. controlFocusByHandle *syscall.LazyProc
  54. controlGetHandle *syscall.LazyProc
  55. controlGetHandleAsText *syscall.LazyProc
  56. controlGetPos *syscall.LazyProc
  57. controlGetPosByHandle *syscall.LazyProc
  58. controlGetText *syscall.LazyProc
  59. controlGetTextByHandle *syscall.LazyProc
  60. controlHide *syscall.LazyProc
  61. controlHideByHandle *syscall.LazyProc
  62. controlListView *syscall.LazyProc
  63. controlListViewByHandle *syscall.LazyProc
  64. controlMove *syscall.LazyProc
  65. controlMoveByHandle *syscall.LazyProc
  66. controlSend *syscall.LazyProc
  67. controlSendByHandle *syscall.LazyProc
  68. controlSetText *syscall.LazyProc
  69. controlSetTextByHandle *syscall.LazyProc
  70. controlShow *syscall.LazyProc
  71. controlShowByHandle *syscall.LazyProc
  72. controlTreeView *syscall.LazyProc
  73. controlTreeViewByHandle *syscall.LazyProc
  74. isAdmin *syscall.LazyProc
  75. mouseClick *syscall.LazyProc
  76. mouseClickDrag *syscall.LazyProc
  77. mouseDown *syscall.LazyProc
  78. mouseGetCursor *syscall.LazyProc
  79. mouseGetPos *syscall.LazyProc
  80. mouseMove *syscall.LazyProc
  81. mouseUp *syscall.LazyProc
  82. mouseWheel *syscall.LazyProc
  83. opt *syscall.LazyProc
  84. processClose *syscall.LazyProc
  85. processExists *syscall.LazyProc
  86. processSetPriority *syscall.LazyProc
  87. processWait *syscall.LazyProc
  88. processWaitClose *syscall.LazyProc
  89. run *syscall.LazyProc
  90. runAs *syscall.LazyProc
  91. runAsWait *syscall.LazyProc
  92. runWait *syscall.LazyProc
  93. send *syscall.LazyProc
  94. winActivate *syscall.LazyProc
  95. winActive *syscall.LazyProc
  96. winCloseByHandle *syscall.LazyProc
  97. winGetHandle *syscall.LazyProc
  98. winGetText *syscall.LazyProc
  99. winGetTitle *syscall.LazyProc
  100. winMinimizeAll *syscall.LazyProc
  101. winMinimizeAllundo *syscall.LazyProc
  102. winMove *syscall.LazyProc
  103. winGetState *syscall.LazyProc
  104. winSetState *syscall.LazyProc
  105. winWait *syscall.LazyProc
  106. )
  107. func init() {
  108. log.SetFlags(log.LstdFlags | log.Llongfile)
  109. // dll64, err = syscall.LoadDLL("D:\\Program Files (x86)\\AutoIt3\\AutoItX\\AutoItX3_x64.dll")
  110. // defer dll64.Release()
  111. currentPath, _ := os.Getwd()
  112. dll64Path := filepath.Join(currentPath, "AutoItX3_x64.dll")
  113. if runtime.GOARCH != "amd64" {
  114. dll64Path = filepath.Join(currentPath, "AutoItX3.dll")
  115. }
  116. dll64 = syscall.NewLazyDLL(dll64Path)
  117. // dll64 = syscall.NewLazyDLL(os.Getenv("GOPATH") + "\\src\\github.com\\shadow1163\\goautoit\\lib\\AutoItX3_x64.dll")
  118. clipGet = dll64.NewProc("AU3_ClipGet")
  119. clipPut = dll64.NewProc("AU3_ClipPut")
  120. controlClick = dll64.NewProc("AU3_ControlClick")
  121. controlClickByHandle = dll64.NewProc("AU3_ControlClickByHandle")
  122. controlCommand = dll64.NewProc("AU3_ControlCommand")
  123. controlCommandByHandle = dll64.NewProc("AU3_ControlCommandByHandle")
  124. controlGetHandle = dll64.NewProc("AU3_ControlGetHandle")
  125. controlGetHandleAsText = dll64.NewProc("AU3_ControlGetHandleAsText")
  126. controlGetPos = dll64.NewProc("AU3_ControlGetPos")
  127. controlGetPosByHandle = dll64.NewProc("AU3_ControlGetPosByHandle")
  128. controlGetText = dll64.NewProc("AU3_ControlGetText")
  129. controlGetTextByHandle = dll64.NewProc("AU3_ControlGetTextByHandle")
  130. controlHide = dll64.NewProc("AU3_ControlHide")
  131. controlHideByHandle = dll64.NewProc("AU3_ControlHideByHandle")
  132. controlListView = dll64.NewProc("AU3_ControlListView")
  133. controlListViewByHandle = dll64.NewProc("AU3_ControlListViewByHandle")
  134. controlMove = dll64.NewProc("AU3_ControlMove")
  135. controlMoveByHandle = dll64.NewProc("AU3_ControlMoveByHandle")
  136. controlSend = dll64.NewProc("AU3_ControlSend")
  137. controlSendByHandle = dll64.NewProc("AU3_ControlSendByHandle")
  138. controlSetText = dll64.NewProc("AU3_ControlSetText")
  139. controlSetTextByHandle = dll64.NewProc("AU3_ControlSetTextByHandle")
  140. controlShow = dll64.NewProc("AU3_ControlShow")
  141. controlShowByHandle = dll64.NewProc("AU3_ControlShowByHandle")
  142. controlTreeView = dll64.NewProc("AU3_ControlTreeView")
  143. controlTreeViewByHandle = dll64.NewProc("AU3_ControlTreeViewByHandle")
  144. isAdmin = dll64.NewProc("AU3_IsAdmin")
  145. mouseClick = dll64.NewProc("AU3_MouseClick")
  146. mouseClickDrag = dll64.NewProc("AU3_MouseClickDrag")
  147. mouseDown = dll64.NewProc("AU3_MouseDown")
  148. mouseGetCursor = dll64.NewProc("AU3_MouseGetCursor")
  149. mouseGetPos = dll64.NewProc("AU3_MouseGetPos")
  150. mouseMove = dll64.NewProc("AU3_MouseMove")
  151. mouseUp = dll64.NewProc("AU3_MouseUp")
  152. mouseWheel = dll64.NewProc("AU3_MouseWheel")
  153. opt = dll64.NewProc("AU3_Opt")
  154. processClose = dll64.NewProc("AU3_ProcessClose")
  155. processExists = dll64.NewProc("AU3_ProcessExists")
  156. processSetPriority = dll64.NewProc("AU3_ProcessSetPriority")
  157. processWait = dll64.NewProc("AU3_ProcessWait")
  158. processWaitClose = dll64.NewProc("AU3_ProcessWaitClose")
  159. run = dll64.NewProc("AU3_Run")
  160. runAs = dll64.NewProc("AU3_RunAs")
  161. runAsWait = dll64.NewProc("AU3_RunAsWait")
  162. runWait = dll64.NewProc("AU3_RunWait")
  163. send = dll64.NewProc("AU3_Send")
  164. winActivate = dll64.NewProc("AU3_WinActivate")
  165. winActive = dll64.NewProc("AU3_WinActive")
  166. winCloseByHandle = dll64.NewProc("AU3_WinCloseByHandle")
  167. winGetHandle = dll64.NewProc("AU3_WinGetHandle")
  168. winGetText = dll64.NewProc("AU3_WinGetText")
  169. winGetTitle = dll64.NewProc("AU3_WinGetTitle")
  170. winMinimizeAll = dll64.NewProc("AU3_WinMinimizeAll")
  171. winMinimizeAllundo = dll64.NewProc("AU3_WinMinimizeAllUndo")
  172. winMove = dll64.NewProc("AU3_WinMove")
  173. winGetState = dll64.NewProc("AU3_WinGetState")
  174. winSetState = dll64.NewProc("AU3_WinSetState")
  175. winWait = dll64.NewProc("AU3_WinWait")
  176. }
  177. // WinMinimizeAll -- all windows should be minimize
  178. func WinMinimizeAll() {
  179. winMinimizeAll.Call()
  180. }
  181. // WinMinimizeAllUndo -- undo minimize all windows
  182. func WinMinimizeAllUndo() {
  183. winMinimizeAllundo.Call()
  184. }
  185. // WinGetTitle -- get windows title
  186. func WinGetTitle(szTitle, szText string, bufSize int) string {
  187. // szTitle := "[active]"
  188. // szText := ""
  189. // bufSize := 256
  190. buff := make([]uint16, int(bufSize))
  191. ret, _, lastErr := winGetTitle.Call(strPtr(szTitle), strPtr(szText), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  192. log.Println(ret)
  193. log.Println(lastErr)
  194. return (goWString(buff))
  195. }
  196. // WinGetText -- get text in window
  197. func WinGetText(szTitle, szText string, bufSize int) string {
  198. buff := make([]uint16, int(bufSize))
  199. winGetText.Call(strPtr(szTitle), strPtr(szText), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  200. return (goWString(buff))
  201. }
  202. // Run -- Run a windows program
  203. // flag 3(max) 6(min) 9(normal) 0(hide)
  204. func Run(szProgram string, args ...interface{}) int {
  205. var szDir string
  206. var flag int
  207. var ok bool
  208. if len(args) == 0 {
  209. szDir = ""
  210. flag = SWShowNormal
  211. } else if len(args) == 1 {
  212. if szDir, ok = args[0].(string); !ok {
  213. panic("szDir must be a string")
  214. }
  215. flag = SWShowNormal
  216. } else if len(args) == 2 {
  217. if szDir, ok = args[0].(string); !ok {
  218. panic("szDir must be a string")
  219. }
  220. if flag, ok = args[1].(int); !ok {
  221. panic("flag must be a int")
  222. }
  223. } else {
  224. panic("Too more parameter")
  225. }
  226. pid, _, lastErr := run.Call(strPtr(szProgram), strPtr(szDir), intPtr(flag))
  227. // log.Println(pid)
  228. if int(pid) == 0 {
  229. log.Println(lastErr)
  230. }
  231. return int(pid)
  232. }
  233. // Send -- Send simulates input on the keyboard
  234. // flag: 0: normal, 1: raw
  235. func Send(key string, args ...interface{}) {
  236. var nMode int
  237. var ok bool
  238. if len(args) == 0 {
  239. nMode = 0
  240. } else if len(args) == 1 {
  241. if nMode, ok = args[0].(int); !ok {
  242. panic("nMode must be a int")
  243. }
  244. } else {
  245. panic("Too more parameter")
  246. }
  247. send.Call(strPtr(key), intPtr(nMode))
  248. }
  249. // WinWait -- wait window to active
  250. func WinWait(szTitle string, args ...interface{}) int {
  251. var szText string
  252. var nTimeout int
  253. var ok bool
  254. if len(args) == 0 {
  255. szText = ""
  256. nTimeout = 0
  257. } else if len(args) == 1 {
  258. if szText, ok = args[0].(string); !ok {
  259. panic("szText must be a string")
  260. }
  261. nTimeout = 0
  262. } else if len(args) == 2 {
  263. if szText, ok = args[0].(string); !ok {
  264. panic("szText must be a string")
  265. }
  266. if nTimeout, ok = args[1].(int); !ok {
  267. panic("nTimeout must be a int")
  268. }
  269. } else {
  270. panic("Too more parameter")
  271. }
  272. handle, _, lastErr := winWait.Call(strPtr(szTitle), strPtr(szText), intPtr(nTimeout))
  273. if int(handle) == 0 {
  274. log.Print("timeout or failure!!!")
  275. log.Println(lastErr)
  276. }
  277. return int(handle)
  278. }
  279. // MouseClick -- Perform a mouse click operation.
  280. func MouseClick(button string, args ...interface{}) int {
  281. var x, y, nClicks, nSpeed int
  282. var ok bool
  283. if len(args) == 0 {
  284. x = INTDEFAULT
  285. y = INTDEFAULT
  286. nClicks = 1
  287. nSpeed = 10
  288. } else if len(args) == 2 {
  289. if x, ok = args[0].(int); !ok {
  290. panic("x must be a int")
  291. }
  292. if y, ok = args[1].(int); !ok {
  293. panic("y must be a int")
  294. }
  295. nClicks = 1
  296. nSpeed = 10
  297. } else if len(args) == 3 {
  298. if x, ok = args[0].(int); !ok {
  299. panic("x must be a int")
  300. }
  301. if y, ok = args[1].(int); !ok {
  302. panic("y must be a int")
  303. }
  304. if nClicks, ok = args[2].(int); !ok {
  305. panic("nClicks must be a int")
  306. }
  307. nSpeed = 10
  308. } else if len(args) == 4 {
  309. if x, ok = args[0].(int); !ok {
  310. panic("x must be a int")
  311. }
  312. if y, ok = args[1].(int); !ok {
  313. panic("y must be a int")
  314. }
  315. if nClicks, ok = args[2].(int); !ok {
  316. panic("nClicks must be a int")
  317. }
  318. if nSpeed, ok = args[3].(int); !ok {
  319. panic("nSpeed must be a int")
  320. }
  321. } else {
  322. panic("Error parameters")
  323. }
  324. ret, _, lastErr := mouseClick.Call(strPtr(button), intPtr(x), intPtr(y), intPtr(nClicks), intPtr(nSpeed))
  325. if int(ret) != 1 {
  326. log.Print("failure!!!")
  327. log.Println(lastErr)
  328. }
  329. return int(ret)
  330. }
  331. // ControlClick -- Sends a mouse click command to a given control.
  332. func ControlClick(title, text, control string, args ...interface{}) int {
  333. var button string
  334. var x, y, nClicks int
  335. var ok bool
  336. if len(args) == 0 {
  337. button = DefaultMouseButton
  338. nClicks = 1
  339. x = INTDEFAULT
  340. y = INTDEFAULT
  341. } else if len(args) == 1 {
  342. if button, ok = args[0].(string); !ok {
  343. panic("button must be a string")
  344. }
  345. nClicks = 1
  346. x = INTDEFAULT
  347. y = INTDEFAULT
  348. } else if len(args) == 2 {
  349. if button, ok = args[0].(string); !ok {
  350. panic("button must be a string")
  351. }
  352. if nClicks, ok = args[1].(int); !ok {
  353. panic("nClicks must be a int")
  354. }
  355. x = INTDEFAULT
  356. y = INTDEFAULT
  357. } else if len(args) == 4 {
  358. if button, ok = args[0].(string); !ok {
  359. panic("button must be a string")
  360. }
  361. if nClicks, ok = args[1].(int); !ok {
  362. panic("nClicks must be a int")
  363. }
  364. if x, ok = args[2].(int); !ok {
  365. panic("x must be a int")
  366. }
  367. if y, ok = args[3].(int); !ok {
  368. panic("y must be a int")
  369. }
  370. } else {
  371. panic("Error parameters")
  372. }
  373. ret, _, lastErr := controlClick.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(button), intPtr(nClicks), intPtr(x), intPtr(y))
  374. if int(ret) == 0 {
  375. log.Print("failure!!!")
  376. log.Println(lastErr)
  377. }
  378. return int(ret)
  379. }
  380. // ControlClickByHandle -- Sends a mouse click command to a given control.
  381. func ControlClickByHandle(handle, control HWND, args ...interface{}) int {
  382. var button string
  383. var x, y, nClicks int
  384. var ok bool
  385. if len(args) == 0 {
  386. button = DefaultMouseButton
  387. nClicks = 1
  388. x = INTDEFAULT
  389. y = INTDEFAULT
  390. } else if len(args) == 1 {
  391. if button, ok = args[0].(string); !ok {
  392. panic("button must be a string")
  393. }
  394. nClicks = 1
  395. x = INTDEFAULT
  396. y = INTDEFAULT
  397. } else if len(args) == 2 {
  398. if button, ok = args[0].(string); !ok {
  399. panic("button must be a string")
  400. }
  401. if nClicks, ok = args[1].(int); !ok {
  402. panic("nClicks must be a int")
  403. }
  404. x = INTDEFAULT
  405. y = INTDEFAULT
  406. } else if len(args) == 4 {
  407. if button, ok = args[0].(string); !ok {
  408. panic("button must be a string")
  409. }
  410. if nClicks, ok = args[1].(int); !ok {
  411. panic("nClicks must be a int")
  412. }
  413. if x, ok = args[2].(int); !ok {
  414. panic("x must be a int")
  415. }
  416. if y, ok = args[3].(int); !ok {
  417. panic("y must be a int")
  418. }
  419. } else {
  420. panic("Error parameters")
  421. }
  422. ret, _, lastErr := controlClickByHandle.Call(uintptr(handle), uintptr(control), strPtr(button), intPtr(nClicks), intPtr(x), intPtr(y))
  423. if int(ret) == 0 {
  424. log.Print("failure!!!")
  425. log.Println(lastErr)
  426. }
  427. return int(ret)
  428. }
  429. // ClipGet -- get a string from clip
  430. func ClipGet(args ...interface{}) string {
  431. var nBufSize int
  432. var ok bool
  433. if len(args) == 0 {
  434. nBufSize = 256
  435. } else if len(args) == 1 {
  436. if nBufSize, ok = args[0].(int); !ok {
  437. panic("nBufSize must be a int")
  438. }
  439. } else {
  440. panic("Error parameters")
  441. }
  442. clip := make([]uint16, int(nBufSize))
  443. clipGet.Call(uintptr(unsafe.Pointer(&clip[0])), intPtr(nBufSize))
  444. return (goWString(clip))
  445. }
  446. // ClipPut -- put a string to clip
  447. func ClipPut(szClip string) int {
  448. ret, _, lastErr := clipPut.Call(strPtr(szClip))
  449. if int(ret) == 0 {
  450. log.Println(lastErr)
  451. }
  452. return int(ret)
  453. }
  454. // WinActivate ( "title" [, "text"]) int
  455. func WinActivate(title string, args ...interface{}) int {
  456. text := ""
  457. var ok bool
  458. argsLen := len(args)
  459. if argsLen > 1 {
  460. panic("argument count > 2")
  461. }
  462. if argsLen == 1 {
  463. if text, ok = args[0].(string); !ok {
  464. panic("text must be a string")
  465. }
  466. }
  467. ret, _, lastErr := winActivate.Call(strPtr(title), strPtr(text))
  468. if int(ret) == 0 {
  469. log.Print("failure!!!")
  470. log.Println(lastErr)
  471. }
  472. return int(ret)
  473. }
  474. // WinActive ( "title" [, "text"]) int
  475. func WinActive(title string, args ...interface{}) int {
  476. text := ""
  477. var ok bool
  478. argsLen := len(args)
  479. if argsLen > 1 {
  480. panic("argument count > 2")
  481. }
  482. if argsLen == 1 {
  483. if text, ok = args[0].(string); !ok {
  484. panic("text must be a string")
  485. }
  486. }
  487. ret, _, lastErr := winActive.Call(strPtr(title), strPtr(text))
  488. if int(ret) == 0 {
  489. log.Print("failure!!!")
  490. log.Println(lastErr)
  491. }
  492. return int(ret)
  493. }
  494. // WinGetHandle -- get window handle
  495. func WinGetHandle(title string, args ...interface{}) HWND {
  496. var text string
  497. var ok bool
  498. if len(args) == 0 {
  499. text = ""
  500. } else if len(args) == 1 {
  501. if text, ok = args[0].(string); !ok {
  502. panic("text must be a string")
  503. }
  504. } else {
  505. panic("Error parameters")
  506. }
  507. ret, _, lastErr := winGetHandle.Call(strPtr(title), strPtr(text))
  508. if int(ret) == 0 {
  509. log.Print("failure!!!")
  510. log.Println(lastErr)
  511. }
  512. return HWND(ret)
  513. }
  514. // WinMove ( "title", "text", x, y [, width [, height [, speed]]] ) int
  515. func WinMove(title, text string, x, y int, args ...interface{}) int {
  516. width := INTDEFAULT
  517. height := INTDEFAULT
  518. speed := 10
  519. var ok bool
  520. argsLen := len(args)
  521. if argsLen > 0 {
  522. if width, ok = args[0].(int); !ok {
  523. panic("width must be an integer")
  524. }
  525. if argsLen > 1 {
  526. if height, ok = args[1].(int); !ok {
  527. panic("height must be an integer")
  528. }
  529. if argsLen > 2 {
  530. if speed, ok = args[2].(int); !ok {
  531. panic("speed must be an integer")
  532. }
  533. if speed < 1 || speed > 100 {
  534. panic("speed must in range 1~100(slowest)")
  535. }
  536. if argsLen > 3 {
  537. panic("too many arguments")
  538. }
  539. }
  540. }
  541. }
  542. ret, _, lastErr := winMove.Call(strPtr(title), strPtr(text), intPtr(x), intPtr(y), intPtr(width),
  543. intPtr(height), intPtr(speed))
  544. if int(ret) == 0 {
  545. log.Print("failure!!!")
  546. log.Println(lastErr)
  547. }
  548. return int(ret)
  549. }
  550. // WinCloseByHandle --
  551. func WinCloseByHandle(hwnd HWND) int {
  552. ret, _, lastErr := winCloseByHandle.Call(uintptr(hwnd))
  553. if int(ret) == 0 {
  554. log.Print("failure!!!")
  555. log.Println(lastErr)
  556. }
  557. return int(ret)
  558. }
  559. // WinGetState ( "title" [, "text"] ) int
  560. func WinGetState(title string, args ...interface{}) int {
  561. text := ""
  562. var ok bool
  563. argsLen := len(args)
  564. if argsLen > 1 {
  565. panic("argument count > 2")
  566. }
  567. if argsLen == 1 {
  568. if text, ok = args[0].(string); !ok {
  569. panic("text must be a string")
  570. }
  571. }
  572. ret, _, lastErr := winGetState.Call(strPtr(title), strPtr(text))
  573. if int(ret) == 0 {
  574. log.Println("winGetState failure!!!", lastErr)
  575. }
  576. return int(ret)
  577. }
  578. // WinSetState ( "title", "text", flag) int
  579. func WinSetState(title, text string, flag int) int {
  580. ret, _, lastErr := winSetState.Call(strPtr(title), strPtr(text), intPtr(flag))
  581. if int(ret) == 0 {
  582. log.Print("WinSetState failure!!!")
  583. log.Println(lastErr)
  584. }
  585. return int(ret)
  586. }
  587. // ControlSend -- Sends a string of characters to a control.
  588. func ControlSend(title, text, control, sendText string, args ...interface{}) int {
  589. var nMode int
  590. var ok bool
  591. if len(args) == 0 {
  592. nMode = 0
  593. } else if len(args) == 1 {
  594. if nMode, ok = args[0].(int); !ok {
  595. panic("nMode must be a int")
  596. }
  597. } else {
  598. panic("Too more parameter")
  599. }
  600. ret, _, lastErr := controlSend.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(sendText), intPtr(nMode))
  601. if int(ret) == 0 {
  602. log.Println(lastErr)
  603. }
  604. return int(ret)
  605. }
  606. // ControlSendByHandle -- Sends a string of characters to a control.
  607. func ControlSendByHandle(handle, control HWND, sendText string, args ...interface{}) int {
  608. var nMode int
  609. var ok bool
  610. if len(args) == 0 {
  611. nMode = 0
  612. } else if len(args) == 1 {
  613. if nMode, ok = args[0].(int); !ok {
  614. panic("nMode must be a int")
  615. }
  616. } else {
  617. panic("Too more parameter")
  618. }
  619. ret, _, lastErr := controlSendByHandle.Call(uintptr(handle), uintptr(control), strPtr(sendText), intPtr(nMode))
  620. if int(ret) == 0 {
  621. log.Println(lastErr)
  622. }
  623. return int(ret)
  624. }
  625. // ControlSetText -- Sets text of a control.
  626. func ControlSetText(title, text, control, newText string) int {
  627. ret, _, lastErr := controlSetText.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(newText))
  628. if int(ret) == 0 {
  629. log.Println(lastErr)
  630. }
  631. return int(ret)
  632. }
  633. // ControlSetTextByHandle -- Sets text of a control.
  634. func ControlSetTextByHandle(handle, control HWND, newText string) int {
  635. ret, _, lastErr := controlSetTextByHandle.Call(uintptr(handle), uintptr(control), strPtr(newText))
  636. if int(ret) == 0 {
  637. log.Println(lastErr)
  638. }
  639. return int(ret)
  640. }
  641. // ControlCommand -- Sends a command to a control.
  642. func ControlCommand(title, text, control, command string, args ...interface{}) string {
  643. var Extra string
  644. var bufSize int
  645. var ok bool
  646. if len(args) == 0 {
  647. Extra = ""
  648. bufSize = 256
  649. } else if len(args) == 1 {
  650. if Extra, ok = args[0].(string); !ok {
  651. panic("Extra must be a string")
  652. }
  653. bufSize = 256
  654. } else if len(args) == 2 {
  655. if Extra, ok = args[0].(string); !ok {
  656. panic("Extra must be a string")
  657. }
  658. if bufSize, ok = args[1].(int); !ok {
  659. panic("bufferSize must be a int")
  660. }
  661. } else {
  662. panic("Error parameters")
  663. }
  664. buff := make([]uint16, int(bufSize))
  665. ret, _, lastErr := controlCommand.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(command), strPtr(Extra), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  666. if int(ret) == 0 {
  667. log.Println(lastErr)
  668. }
  669. return (goWString(buff))
  670. }
  671. // ControlCommandByHandle -- Sends a command to a control.
  672. func ControlCommandByHandle(handle, control HWND, command string, args ...interface{}) string {
  673. var Extra string
  674. var bufSize int
  675. var ok bool
  676. if len(args) == 0 {
  677. Extra = ""
  678. bufSize = 256
  679. } else if len(args) == 1 {
  680. if Extra, ok = args[0].(string); !ok {
  681. panic("Extra must be a string")
  682. }
  683. bufSize = 256
  684. } else if len(args) == 2 {
  685. if Extra, ok = args[0].(string); !ok {
  686. panic("Extra must be a string")
  687. }
  688. if bufSize, ok = args[1].(int); !ok {
  689. panic("bufferSize must be a int")
  690. }
  691. } else {
  692. panic("Error parameters")
  693. }
  694. buff := make([]uint16, int(bufSize))
  695. ret, _, lastErr := controlCommandByHandle.Call(uintptr(handle), uintptr(control), strPtr(command), strPtr(Extra), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  696. if int(ret) == 0 {
  697. log.Println(lastErr)
  698. }
  699. return (goWString(buff))
  700. }
  701. // ControlListView --Sends a command to a ListView32 control.
  702. func ControlListView(title, text, control, command string, args ...interface{}) string {
  703. var Extra1, Extra2 string
  704. var bufSize int
  705. var ok bool
  706. if len(args) == 0 {
  707. Extra1 = ""
  708. Extra2 = ""
  709. bufSize = 256
  710. } else if len(args) == 1 {
  711. if Extra1, ok = args[0].(string); !ok {
  712. panic("Extra1 must be a string")
  713. }
  714. Extra2 = ""
  715. bufSize = 256
  716. } else if len(args) == 2 {
  717. if Extra1, ok = args[0].(string); !ok {
  718. panic("Extra1 must be a string")
  719. }
  720. if Extra2, ok = args[1].(string); !ok {
  721. panic("Extra2 must be a string")
  722. }
  723. bufSize = 256
  724. } else if len(args) == 3 {
  725. if Extra1, ok = args[0].(string); !ok {
  726. panic("Extra1 must be a string")
  727. }
  728. if Extra2, ok = args[1].(string); !ok {
  729. panic("Extra2 must be a string")
  730. }
  731. if bufSize, ok = args[2].(int); !ok {
  732. panic("bufSize must be a int")
  733. }
  734. } else {
  735. panic("Error parameters")
  736. }
  737. buff := make([]uint16, int(bufSize))
  738. ret, _, lastErr := controlListView.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(command), strPtr(Extra1), strPtr(Extra2), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  739. if int(ret) == 0 {
  740. log.Println(lastErr)
  741. }
  742. return (goWString(buff))
  743. }
  744. // ControlListViewByHandle --Sends a command to a ListView32 control.
  745. func ControlListViewByHandle(handle, control HWND, command string, args ...interface{}) string {
  746. var Extra1, Extra2 string
  747. var bufSize int
  748. var ok bool
  749. if len(args) == 0 {
  750. Extra1 = ""
  751. Extra2 = ""
  752. bufSize = 256
  753. } else if len(args) == 1 {
  754. if Extra1, ok = args[0].(string); !ok {
  755. panic("Extra1 must be a string")
  756. }
  757. Extra2 = ""
  758. bufSize = 256
  759. } else if len(args) == 2 {
  760. if Extra1, ok = args[0].(string); !ok {
  761. panic("Extra1 must be a string")
  762. }
  763. if Extra2, ok = args[1].(string); !ok {
  764. panic("Extra2 must be a string")
  765. }
  766. bufSize = 256
  767. } else if len(args) == 3 {
  768. if Extra1, ok = args[0].(string); !ok {
  769. panic("Extra1 must be a string")
  770. }
  771. if Extra2, ok = args[1].(string); !ok {
  772. panic("Extra2 must be a string")
  773. }
  774. if bufSize, ok = args[2].(int); !ok {
  775. panic("bufSize must be a int")
  776. }
  777. } else {
  778. panic("Error parameters")
  779. }
  780. buff := make([]uint16, int(bufSize))
  781. ret, _, lastErr := controlListViewByHandle.Call(uintptr(handle), uintptr(control), strPtr(command), strPtr(Extra1), strPtr(Extra2), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  782. if int(ret) == 0 {
  783. log.Println(lastErr)
  784. }
  785. return (goWString(buff))
  786. }
  787. // ControlDisable -- Disables or "grays-out" a control.
  788. func ControlDisable(title, text, control string) int {
  789. ret, _, lastErr := controlDisable.Call(strPtr(title), strPtr(text), strPtr(control))
  790. if int(ret) == 0 {
  791. log.Println(lastErr)
  792. }
  793. return int(ret)
  794. }
  795. // ControlDisableByHandle -- Disables or "grays-out" a control.
  796. func ControlDisableByHandle(handle, control HWND) int {
  797. ret, _, lastErr := controlDisableByHandle.Call(uintptr(handle), uintptr(control))
  798. if int(ret) == 0 {
  799. log.Println(lastErr)
  800. }
  801. return int(ret)
  802. }
  803. // ControlEnable -- Enables a "grayed-out" control.
  804. func ControlEnable(title, text, control string) int {
  805. ret, _, lastErr := controlEnable.Call(strPtr(title), strPtr(text), strPtr(control))
  806. if int(ret) == 0 {
  807. log.Println(lastErr)
  808. }
  809. return int(ret)
  810. }
  811. // ControlEnableByHandle -- Enables a "grayed-out" control.
  812. func ControlEnableByHandle(handle, control HWND) int {
  813. ret, _, lastErr := controlEnableByHandle.Call(uintptr(handle), uintptr(control))
  814. if int(ret) == 0 {
  815. log.Println(lastErr)
  816. }
  817. return int(ret)
  818. }
  819. // ControlFocus -- Sets input focus to a given control on a window.
  820. func ControlFocus(title, text, control string) int {
  821. ret, _, lastErr := controlFocus.Call(strPtr(title), strPtr(text), strPtr(control))
  822. if int(ret) == 0 {
  823. log.Println(lastErr)
  824. }
  825. return int(ret)
  826. }
  827. // ControlFocusByHandle -- Sets input focus to a given control on a window.
  828. func ControlFocusByHandle(handle, control HWND) int {
  829. ret, _, lastErr := controlFocusByHandle.Call(uintptr(handle), uintptr(control))
  830. if int(ret) == 0 {
  831. log.Println(lastErr)
  832. }
  833. return int(ret)
  834. }
  835. // ControlGetHandle -- Retrieves the internal handle of a control.
  836. func ControlGetHandle(handle HWND, control string) HWND {
  837. ret, _, lastErr := controlGetHandle.Call(uintptr(handle), strPtr(control))
  838. if int(ret) == 0 {
  839. log.Println(lastErr)
  840. }
  841. return HWND(ret)
  842. }
  843. // ControlGetHandleAsText -- Retrieves the internal handle of a control.
  844. func ControlGetHandleAsText(title, text, control string, args ...interface{}) string {
  845. var bufSize int
  846. var ok bool
  847. if len(args) == 0 {
  848. bufSize = 256
  849. } else if len(args) == 1 {
  850. if bufSize, ok = args[0].(int); !ok {
  851. panic("bufSize must be a int")
  852. }
  853. } else {
  854. panic("Error parameters")
  855. }
  856. buff := make([]uint16, int(bufSize))
  857. ret, _, lastErr := controlGetHandleAsText.Call(strPtr(title), strPtr(text), strPtr(control), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  858. if int(ret) == 0 {
  859. log.Println(lastErr)
  860. }
  861. return (goWString(buff))
  862. }
  863. // ControlGetPos -- Retrieves the position and size of a control relative to its window.
  864. func ControlGetPos(title, text, control string) RECT {
  865. lprect := RECT{}
  866. ret, _, lastErr := controlGetPos.Call(strPtr(title), strPtr(text), strPtr(control), uintptr(unsafe.Pointer(&lprect)))
  867. if int(ret) == 0 {
  868. log.Println(lastErr)
  869. }
  870. return lprect
  871. }
  872. // ControlGetPosByHandle -- Retrieves the position and size of a control relative to its window.
  873. func ControlGetPosByHandle(title, text, control string) RECT {
  874. lprect := RECT{}
  875. ret, _, lastErr := controlGetPosByHandle.Call(strPtr(title), strPtr(text), strPtr(control), uintptr(unsafe.Pointer(&lprect)))
  876. if int(ret) == 0 {
  877. log.Println(lastErr)
  878. }
  879. return lprect
  880. }
  881. // ControlGetText -- Retrieves text from a control.
  882. func ControlGetText(title, text, control string, args ...interface{}) string {
  883. var bufSize int
  884. var ok bool
  885. if len(args) == 0 {
  886. bufSize = 256
  887. } else if len(args) == 1 {
  888. if bufSize, ok = args[0].(int); !ok {
  889. panic("bufSize must be a int")
  890. }
  891. } else {
  892. panic("Error parameters")
  893. }
  894. buff := make([]uint16, int(bufSize))
  895. ret, _, lastErr := controlGetText.Call(strPtr(title), strPtr(text), strPtr(control), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  896. if int(ret) == 0 {
  897. log.Println(lastErr)
  898. }
  899. return (goWString(buff))
  900. }
  901. // ControlGetTextByHandle -- Retrieves text from a control.
  902. func ControlGetTextByHandle(handle, control HWND, args ...interface{}) string {
  903. var bufSize int
  904. var ok bool
  905. if len(args) == 0 {
  906. bufSize = 256
  907. } else if len(args) == 1 {
  908. if bufSize, ok = args[0].(int); !ok {
  909. panic("bufSize must be a int")
  910. }
  911. } else {
  912. panic("Error parameters")
  913. }
  914. buff := make([]uint16, int(bufSize))
  915. ret, _, lastErr := controlGetTextByHandle.Call(uintptr(handle), uintptr(control), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  916. if int(ret) == 0 {
  917. log.Println(lastErr)
  918. }
  919. return (goWString(buff))
  920. }
  921. // ControlHide -- Hides a control.
  922. func ControlHide(title, text, control string) int {
  923. ret, _, lastErr := controlHide.Call(strPtr(title), strPtr(text), strPtr(control))
  924. if int(ret) == 1 {
  925. log.Println(lastErr)
  926. }
  927. return int(ret)
  928. }
  929. // ControlHideByHandle -- Hides a control.
  930. func ControlHideByHandle(title, text, control string) int {
  931. ret, _, lastErr := controlHideByHandle.Call(strPtr(title), strPtr(text), strPtr(control))
  932. if int(ret) == 1 {
  933. log.Println(lastErr)
  934. }
  935. return int(ret)
  936. }
  937. // ControlMove -- Hides a control.
  938. func ControlMove(title, text, control string, x, y int, args ...interface{}) int {
  939. var width, height int
  940. var ok bool
  941. if len(args) == 0 {
  942. width = -1
  943. height = -1
  944. } else if len(args) == 2 {
  945. if width, ok = args[0].(int); !ok {
  946. panic("width must be a int")
  947. }
  948. if height, ok = args[1].(int); !ok {
  949. panic("height must be a int")
  950. }
  951. } else {
  952. panic("Error parameters")
  953. }
  954. ret, _, lastErr := controlMove.Call(strPtr(title), strPtr(text), strPtr(control), intPtr(x), intPtr(y), intPtr(width), intPtr(height))
  955. if int(ret) == 1 {
  956. log.Println(lastErr)
  957. }
  958. return int(ret)
  959. }
  960. // ControlMoveByHandle -- Hides a control.
  961. func ControlMoveByHandle(handle, control HWND, x, y int, args ...interface{}) int {
  962. var width, height int
  963. var ok bool
  964. if len(args) == 0 {
  965. width = -1
  966. height = -1
  967. } else if len(args) == 2 {
  968. if width, ok = args[0].(int); !ok {
  969. panic("width must be a int")
  970. }
  971. if height, ok = args[1].(int); !ok {
  972. panic("height must be a int")
  973. }
  974. } else {
  975. panic("Error parameters")
  976. }
  977. ret, _, lastErr := controlMoveByHandle.Call(uintptr(handle), uintptr(control), intPtr(x), intPtr(y), intPtr(width), intPtr(height))
  978. if int(ret) == 1 {
  979. log.Println(lastErr)
  980. }
  981. return int(ret)
  982. }
  983. // ControlShow -- Shows a control that was hidden.
  984. func ControlShow(title, text, control string) int {
  985. ret, _, lastErr := controlShow.Call(strPtr(title), strPtr(text), strPtr(control))
  986. if int(ret) == 1 {
  987. log.Println(lastErr)
  988. }
  989. return int(ret)
  990. }
  991. // ControlShowByHandle -- Shows a control that was hidden.
  992. func ControlShowByHandle(handle, control HWND) int {
  993. ret, _, lastErr := controlShowByHandle.Call(uintptr(handle), uintptr(control))
  994. if int(ret) == 1 {
  995. log.Println(lastErr)
  996. }
  997. return int(ret)
  998. }
  999. // ControlTreeView -- Sends a command to a TreeView32 control.
  1000. func ControlTreeView(title, text, control, command string, args ...interface{}) string {
  1001. var Extra1, Extra2 string
  1002. var bufSize int
  1003. var ok bool
  1004. if len(args) == 0 {
  1005. Extra1 = ""
  1006. Extra2 = ""
  1007. bufSize = 256
  1008. } else if len(args) == 1 {
  1009. if Extra1, ok = args[0].(string); !ok {
  1010. panic("Extra1 must be a string")
  1011. }
  1012. Extra2 = ""
  1013. bufSize = 256
  1014. } else if len(args) == 2 {
  1015. if Extra1, ok = args[0].(string); !ok {
  1016. panic("Extra1 must be a string")
  1017. }
  1018. if Extra2, ok = args[1].(string); !ok {
  1019. panic("Extra2 must be a string")
  1020. }
  1021. bufSize = 256
  1022. } else if len(args) == 3 {
  1023. if Extra1, ok = args[0].(string); !ok {
  1024. panic("Extra1 must be a string")
  1025. }
  1026. if Extra2, ok = args[1].(string); !ok {
  1027. panic("Extra2 must be a string")
  1028. }
  1029. if bufSize, ok = args[2].(int); !ok {
  1030. panic("bufSize must be a int")
  1031. }
  1032. } else {
  1033. panic("Error parameters")
  1034. }
  1035. buff := make([]uint16, int(bufSize))
  1036. ret, _, lastErr := controlTreeView.Call(strPtr(title), strPtr(text), strPtr(control), strPtr(command), strPtr(Extra1), strPtr(Extra2), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  1037. if int(ret) == 0 {
  1038. log.Println(lastErr)
  1039. }
  1040. return (goWString(buff))
  1041. }
  1042. // ControlTreeViewByHandle -- Sends a command to a TreeView32 control.
  1043. func ControlTreeViewByHandle(handle, control HWND, command string, args ...interface{}) string {
  1044. var Extra1, Extra2 string
  1045. var bufSize int
  1046. var ok bool
  1047. if len(args) == 0 {
  1048. Extra1 = ""
  1049. Extra2 = ""
  1050. bufSize = 256
  1051. } else if len(args) == 1 {
  1052. if Extra1, ok = args[0].(string); !ok {
  1053. panic("Extra1 must be a string")
  1054. }
  1055. Extra2 = ""
  1056. bufSize = 256
  1057. } else if len(args) == 2 {
  1058. if Extra1, ok = args[0].(string); !ok {
  1059. panic("Extra1 must be a string")
  1060. }
  1061. if Extra2, ok = args[1].(string); !ok {
  1062. panic("Extra2 must be a string")
  1063. }
  1064. bufSize = 256
  1065. } else if len(args) == 3 {
  1066. if Extra1, ok = args[0].(string); !ok {
  1067. panic("Extra1 must be a string")
  1068. }
  1069. if Extra2, ok = args[1].(string); !ok {
  1070. panic("Extra2 must be a string")
  1071. }
  1072. if bufSize, ok = args[2].(int); !ok {
  1073. panic("bufSize must be a int")
  1074. }
  1075. } else {
  1076. panic("Error parameters")
  1077. }
  1078. buff := make([]uint16, int(bufSize))
  1079. ret, _, lastErr := controlTreeView.Call(uintptr(handle), uintptr(control), strPtr(command), strPtr(Extra1), strPtr(Extra2), uintptr(unsafe.Pointer(&buff[0])), intPtr(bufSize))
  1080. if int(ret) == 0 {
  1081. log.Println(lastErr)
  1082. }
  1083. return (goWString(buff))
  1084. }
  1085. // Opt -- set option
  1086. func Opt(option, value string) int {
  1087. ret, _, lastErr := opt.Call(strPtr(option), strPtr(value))
  1088. if int(ret) == 0 {
  1089. log.Println(lastErr)
  1090. }
  1091. return int(ret)
  1092. }
  1093. func findTermChr(buff []uint16) int {
  1094. for i, char := range buff {
  1095. if char == 0x0 {
  1096. return i
  1097. }
  1098. }
  1099. panic("not supposed to happen")
  1100. }
  1101. func intPtr(n int) uintptr {
  1102. return uintptr(n)
  1103. }
  1104. func strPtr(s string) uintptr {
  1105. return uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(s)))
  1106. }
  1107. // GoWString -- Convert a uint16 arrry C string to a Go String
  1108. func goWString(s []uint16) string {
  1109. pos := findTermChr(s)
  1110. // log.Println(string(utf16.Decode(s[0:pos])))
  1111. return (string(utf16.Decode(s[0:pos])))
  1112. }