main_windows.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //go:build windows
  2. package yu_clipboard
  3. import (
  4. yu_sys "gogs.qqck.cn/s/tools/sys"
  5. )
  6. func Get() string {
  7. if !yu_sys.IsClipboardFormatAvailable.CallBool(yu_sys.CF_UNICODETEXT) {
  8. return ""
  9. }
  10. if !yu_sys.OpenClipboard.CallBool(0) {
  11. return ""
  12. }
  13. defer yu_sys.CloseClipboard.Call()
  14. return yu_sys.GetClipboardData.CallUnicodeToUtf8(yu_sys.CF_UNICODETEXT)
  15. // j_hdata := yu_sys.GetClipboardData.CallUintptr(yu_sys.CF_UNICODETEXT)
  16. // if j_hdata == 0 {
  17. // return ""
  18. // }
  19. // j_data_ptr := yu_sys.GlobalLock.CallUintptr(j_hdata)
  20. // if j_data_ptr == 0 {
  21. // return ""
  22. // }
  23. // defer yu_sys.GlobalUnlock.Call(j_data_ptr)
  24. // return yu_strings.UnicodePtrToUtf8(j_data_ptr)
  25. }
  26. func Set(s string) bool {
  27. if !yu_sys.OpenClipboard.CallBool(0) {
  28. return false
  29. }
  30. defer yu_sys.CloseClipboard.Call()
  31. if !yu_sys.EmptyClipboard.CallBool() {
  32. return false
  33. }
  34. if s == "" {
  35. return true
  36. }
  37. j_unicode_size, _, _ := yu_sys.MultiByteToWideChar.Call(65001, 0, s, len(s), 0, 0)
  38. if j_unicode_size < 1 {
  39. return false
  40. }
  41. j_unicode_size = j_unicode_size*2 + 2
  42. j_hdata, _, _ := yu_sys.GlobalAlloc.Call(yu_sys.GMEM_MOVEABLE, j_unicode_size)
  43. if j_hdata == 0 {
  44. return false
  45. }
  46. defer yu_sys.GlobalFree.Call(j_hdata)
  47. j_data_ptr, _, _ := yu_sys.GlobalLock.Call(j_hdata)
  48. if j_data_ptr == 0 {
  49. return false
  50. }
  51. defer yu_sys.GlobalUnlock.Call(j_data_ptr)
  52. yu_sys.MultiByteToWideChar.Call(65001, 0, s, len(s), j_data_ptr, j_unicode_size)
  53. return yu_sys.SetClipboardData.CallUintptr(yu_sys.CF_UNICODETEXT, j_hdata) != 0
  54. }