golang設置title並獲取窗口句柄

package maingit

import (
  "fmt"github

  "syscall"golang

  "github.com/lxn/win"
)ui

func main() {string

  SetConsoleTitle("golang hwnd test")it

  hwnd := FindWindow("golang hwnd test")test

}import

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

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

  _, _, 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}

相關文章
相關標籤/搜索