driver_windows.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package glfw
  2. import (
  3. "fmt"
  4. "runtime"
  5. "syscall"
  6. "unsafe"
  7. )
  8. type MB uint32
  9. const (
  10. MB_OK MB = 0x0000_0000
  11. MB_ICONERROR MB = 0x0000_0010
  12. )
  13. func toNativePtr(s string) *uint16 {
  14. pstr, err := syscall.UTF16PtrFromString(s)
  15. if err != nil {
  16. panic(fmt.Sprintf("toNativePtr() failed \"%s\": %s", s, err))
  17. }
  18. return pstr
  19. }
  20. // https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-messageboxw
  21. func messageBoxError(text, caption string) {
  22. user32 := syscall.NewLazyDLL("user32.dll")
  23. MessageBox := user32.NewProc("MessageBoxW")
  24. uType := MB_OK | MB_ICONERROR
  25. syscall.Syscall6(MessageBox.Addr(), 4,
  26. uintptr(unsafe.Pointer(nil)), uintptr(unsafe.Pointer(toNativePtr(text))),
  27. uintptr(unsafe.Pointer(toNativePtr(caption))), uintptr(uType),
  28. 0, 0)
  29. }
  30. func logError(msg string, err error) {
  31. text := fmt.Sprintf("Fyne error: %v", msg)
  32. if err != nil {
  33. text = text + fmt.Sprintf("\n Cause:%v", err)
  34. }
  35. _, file, line, ok := runtime.Caller(1)
  36. if ok {
  37. text = text + fmt.Sprintf("\n At: %s:%d", file, line)
  38. }
  39. messageBoxError(text, "Fyne Error")
  40. }