update clipboard func

master
兔子 8 months ago
parent 7a2fe997fb
commit e56f9d7cc9

@ -164,3 +164,141 @@ func RegisterClipboardFormat(lpszFormat string) DWORD {
r, _, _ := syscall.Syscall(rcf, 1, uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(lpszFormat))), 0, 0)
return DWORD(r)
}
func EmptyClipboard() error {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
ec, err := syscall.GetProcAddress(syscall.Handle(user32), "EmptyClipboard")
if err != nil {
return errors.New("Can't Load EmptyClipboard API")
}
if r, _, errno := syscall.Syscall(ec, 0, 0, 0, 0); r == 0 {
return error(errno)
}
return nil
}
func CountClipboardFormats() (int, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
ccf, err := syscall.GetProcAddress(syscall.Handle(user32), "CountClipboardFormats")
if err != nil {
return 0, errors.New("Can't Load CountClipboardFormats API")
}
r, _, errno := syscall.Syscall(ccf, 0, 0, 0, 0)
if r == 0 {
return 0, error(errno)
}
return int(r), nil
}
func GetClipboardOwner() (HWND, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
gco, err := syscall.GetProcAddress(syscall.Handle(user32), "GetClipboardOwner")
if err != nil {
return 0, errors.New("Can't Load GetClipboardOwner API")
}
r, _, errno := syscall.Syscall(gco, 0, 0, 0, 0)
if r == 0 {
return 0, error(errno)
}
return HWND(r), nil
}
func GetUpdatedClipboardFormats() ([]DWORD, error) {
var formats []DWORD
for i := 0; ; i++ {
format, err := EnumClipboardFormats(DWORD(i))
if err != nil {
break
}
formats = append(formats, format)
}
return formats, nil
}
func IsClipboardFormatAvailable(uFormat DWORD) (bool, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return false, err
}
defer syscall.FreeLibrary(user32)
icfa, err := syscall.GetProcAddress(syscall.Handle(user32), "IsClipboardFormatAvailable")
if err != nil {
return false, err
}
r, _, _ := syscall.Syscall(icfa, 1, uintptr(uFormat), 0, 0)
return r != 0, nil
}
func AddClipboardFormatListener(hWnd HWND) (bool, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return false, err
}
defer syscall.FreeLibrary(user32)
acfl, err := syscall.GetProcAddress(syscall.Handle(user32), "AddClipboardFormatListener")
if err != nil {
return false, err
}
r, _, _ := syscall.Syscall(acfl, 1, uintptr(hWnd), 0, 0)
return r != 0, nil
}
func RemoveClipboardFormatListener(hWnd HWND) (bool, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return false, err
}
defer syscall.FreeLibrary(user32)
rcfl, err := syscall.GetProcAddress(syscall.Handle(user32), "RemoveClipboardFormatListener")
if err != nil {
return false, err
}
r, _, _ := syscall.Syscall(rcfl, 1, uintptr(hWnd), 0, 0)
return r != 0, nil
}
func SetClipboardData(uFormat DWORD, hMem HGLOBAL) (HGLOBAL, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
scd, err := syscall.GetProcAddress(syscall.Handle(user32), "SetClipboardData")
if err != nil {
return 0, errors.New("Can't Load SetClipboardData API")
}
r, _, errno := syscall.Syscall(scd, 2, uintptr(uFormat), uintptr(hMem), 0)
if r == 0 {
return 0, error(errno)
}
return HGLOBAL(r), nil
}
func SetClipboardViewer(hWndNewViewer HWND) (HWND, error) {
user32, err := syscall.LoadLibrary("user32.dll")
if err != nil {
return 0, errors.New("Can't Load User32 API")
}
defer syscall.FreeLibrary(user32)
scv, err := syscall.GetProcAddress(syscall.Handle(user32), "SetClipboardViewer")
if err != nil {
return 0, errors.New("Can't Load SetClipboardViewer API")
}
r, _, errno := syscall.Syscall(scv, 1, uintptr(hWndNewViewer), 0, 0)
if r == 0 {
return 0, error(errno)
}
return HWND(r), nil
}

Loading…
Cancel
Save