85 lines
1.9 KiB
Go
85 lines
1.9 KiB
Go
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"syscall"
|
|
"unicode/utf16"
|
|
"unsafe"
|
|
)
|
|
|
|
const (
|
|
mbOK = 0x00000000
|
|
mbOKCancel = 0x00000001
|
|
mbIconError = 0x00000010
|
|
mbIconQuestion = 0x00000020
|
|
mbIconInformation = 0x00000040
|
|
idOK = 1
|
|
)
|
|
|
|
const (
|
|
cfUnicodeText = 13
|
|
gmemMoveable = 0x0002
|
|
)
|
|
|
|
var (
|
|
user32DLL = syscall.NewLazyDLL("user32.dll")
|
|
messageBoxW = user32DLL.NewProc("MessageBoxW")
|
|
openClip = user32DLL.NewProc("OpenClipboard")
|
|
closeClip = user32DLL.NewProc("CloseClipboard")
|
|
emptyClip = user32DLL.NewProc("EmptyClipboard")
|
|
setClipData = user32DLL.NewProc("SetClipboardData")
|
|
globalAlloc = kernel32DLL.NewProc("GlobalAlloc")
|
|
globalLock = kernel32DLL.NewProc("GlobalLock")
|
|
globalUnlock = kernel32DLL.NewProc("GlobalUnlock")
|
|
)
|
|
|
|
func showInfo(title, text string) {
|
|
showMessage(title, text, mbOK|mbIconInformation)
|
|
}
|
|
|
|
func showError(title, text string) {
|
|
showMessage(title, text, mbOK|mbIconError)
|
|
}
|
|
|
|
func confirm(title, text string) bool {
|
|
return showMessage(title, text, mbOKCancel|mbIconQuestion) == idOK
|
|
}
|
|
|
|
func showMessage(title, text string, flags uint) int {
|
|
titlePtr, _ := syscall.UTF16PtrFromString(title)
|
|
textPtr, _ := syscall.UTF16PtrFromString(text)
|
|
result, _, _ := messageBoxW.Call(
|
|
0,
|
|
uintptr(unsafe.Pointer(textPtr)),
|
|
uintptr(unsafe.Pointer(titlePtr)),
|
|
uintptr(flags),
|
|
)
|
|
return int(result)
|
|
}
|
|
|
|
func copyToClipboard(text string) {
|
|
opened, _, _ := openClip.Call(0)
|
|
if opened == 0 {
|
|
return
|
|
}
|
|
defer closeClip.Call()
|
|
emptyClip.Call()
|
|
|
|
encoded := utf16.Encode([]rune(text))
|
|
encoded = append(encoded, 0)
|
|
size := uintptr(len(encoded) * 2)
|
|
handle, _, _ := globalAlloc.Call(gmemMoveable, size)
|
|
if handle == 0 {
|
|
return
|
|
}
|
|
pointer, _, _ := globalLock.Call(handle)
|
|
if pointer == 0 {
|
|
return
|
|
}
|
|
target := unsafe.Slice((*uint16)(unsafe.Pointer(pointer)), len(encoded))
|
|
copy(target, encoded)
|
|
globalUnlock.Call(handle)
|
|
setClipData.Call(cfUnicodeText, handle)
|
|
}
|