update clipboard func

master
兔子 8 months ago
parent 9b003f3e9d
commit ad19f5cd42

@ -215,16 +215,35 @@ func GetClipboardOwner() (HWND, error) {
return HWND(r), nil return HWND(r), nil
} }
func GetUpdatedClipboardFormats() ([]DWORD, error) { func GetUpdatedClipboardFormats(lpuiFormats unsafe.Pointer, cFormats int, pcFormats unsafe.Pointer) (int, error) {
var formats []DWORD user32, err := syscall.LoadLibrary("user32.dll")
for i := 0; ; i++ {
format, err := EnumClipboardFormats(DWORD(i))
if err != nil { if err != nil {
break return 0, errors.New("Can't Load User32 API")
} }
formats = append(formats, format) defer syscall.FreeLibrary(user32)
gucf, err := syscall.GetProcAddress(syscall.Handle(user32), "GetUpdatedClipboardFormats")
if err != nil {
return 0, errors.New("Can't Load GetUpdatedClipboardFormats API")
} }
return formats, nil r, _, errno := syscall.Syscall(gucf, 3, uintptr(lpuiFormats), uintptr(cFormats), uintptr(pcFormats))
if r == 0 {
return 0, error(errno)
}
return int(r), nil
}
func GetUpdatedClipboardFormatsAll() ([]DWORD, error) {
var res []DWORD
formats := make([]uint32, 32)
var count uint32
_, err := GetUpdatedClipboardFormats(unsafe.Pointer(&formats[0]), len(formats), unsafe.Pointer(&count))
if err != nil {
return nil, err
}
for i := 0; i < int(count); i++ {
res = append(res, DWORD(formats[i]))
}
return res, err
} }
func IsClipboardFormatAvailable(uFormat DWORD) (bool, error) { func IsClipboardFormatAvailable(uFormat DWORD) (bool, error) {

@ -16,6 +16,7 @@ func TestClipboardReadText(t *testing.T) {
t.Error(err) t.Error(err)
} }
tp := CF_UNICODETEXT tp := CF_UNICODETEXT
fmt.Println("lenghth", len(formats))
for _, f := range formats { for _, f := range formats {
fmt.Println("Clipboard Format:", f) fmt.Println("Clipboard Format:", f)
d, e := GetClipboardFormatName(f) d, e := GetClipboardFormatName(f)
@ -25,9 +26,12 @@ func TestClipboardReadText(t *testing.T) {
fmt.Println("HTML Format:", tp) fmt.Println("HTML Format:", tp)
} }
if d == "DataObject" { if d == "DataObject" {
tp = CF_HDROP tp = f
fmt.Println("DataObject:", tp) fmt.Println("DataObject:", tp)
} }
if f > 20000 {
tp = f
}
} }
mem, err := GetClipboardData(tp) mem, err := GetClipboardData(tp)
@ -56,10 +60,19 @@ func TestClipboardReadText(t *testing.T) {
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
fmt.Println("Size:", size)
var buf []byte var buf []byte
for i := 0; i < int(size); i++ { for i := 0; i < int(size); i++ {
buf = append(buf, *(*byte)(unsafe.Pointer(uintptr(p) + uintptr(i)))) buf = append(buf, *(*byte)(unsafe.Pointer(uintptr(p) + uintptr(i))))
} }
fmt.Println("\n\nClipboard Data:", string(buf)) fmt.Println("\n\nClipboard Data:", "ok")
fmt.Println("Clipboard Test Done") fmt.Println("Clipboard Test Done")
} }
func TestGetUpdatedClipboardFormatsAll(t *testing.T) {
d, e := GetUpdatedClipboardFormatsAll()
if e != nil {
t.Error(e)
}
fmt.Println(d)
}

Loading…
Cancel
Save