golang设置title并获取窗口句柄

package main

import (
  "fmt"

  "syscall"

  "github.com/lxn/win"
)

func main() {

  SetConsoleTitle("golang hwnd test")

  hwnd := FindWindow("golang hwnd test")

}

func SetConsoleTitle(title string) {
  kernel32, loadErr := syscall.LoadLibrary("kernel32.dll")
  if loadErr != nil {
    fmt.Println("loadErr", loadErr)
  }
  defer syscall.FreeLibrary(kernel32)

  _SetConsoleTitle, _ := syscall.GetProcAddress(kernel32, "SetConsoleTitleW")

  _, _, callErr := syscall.Syscall(_SetConsoleTitle, 1, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(title))), 0, 0)
  if callErr != 0 {
    fmt.Println("callErr", callErr)
  }
}

func FindWindow(str string) win.HWND {
  hwnd := win.FindWindow(nil, syscall.StringToUTF16Ptr(str))

  return hwnd
}

 

posted @ 2019-04-29 09:08  sherlock-merlin  阅读(1865)  评论(0编辑  收藏  举报