This commit is contained in:
恍兮惚兮 2024-11-15 09:11:45 +08:00
parent b65f5a5994
commit 3e62d72c2e

View File

@ -106,6 +106,7 @@ static void clipboard_callback_1(void (*callback)(const wchar_t *, bool), HANDLE
callbackx(hWnd);
}
#else
// 根据文档这样做是正确的且在win11下管用。但到xp上就读不到了。。
static HWND nextviewer;
switch (message)
{
@ -157,7 +158,7 @@ static void clipboard_callback_1(void (*callback)(const wchar_t *, bool), HANDLE
DispatchMessage(&msg);
}
}
#ifndef WINXP
DECLARE_API HWND clipboard_callback(void (*callback)(const wchar_t *, bool))
{
HANDLE hsema = CreateSemaphoreW(0, 0, 10, 0);
@ -167,23 +168,45 @@ DECLARE_API HWND clipboard_callback(void (*callback)(const wchar_t *, bool))
WaitForSingleObject(hsema, INFINITE);
CloseHandle(hsema);
#ifndef WINXP
if (AddClipboardFormatListener(hwnd))
return hwnd;
else
return NULL;
#else
return hwnd;
#endif
}
#else
static int running = false;
DECLARE_API HWND clipboard_callback(void (*callback)(const wchar_t *, bool))
{
running = true;
std::thread([=]()
{
std::wstring last;
while(running){
Sleep(100);
auto data = clipboard_get_internal();
if(data){
if(last==data.value())continue;
last=data.value();
auto ohwnd = GetClipboardOwner();
DWORD pid;
GetWindowThreadProcessId(ohwnd, &pid);
callback(data.value().c_str(), pid == GetCurrentProcessId());
}
} })
.detach();
return NULL;
}
#endif
DECLARE_API void clipboard_callback_stop(HWND hwnd)
{
#ifndef WINXP
if (!hwnd)
return;
#ifndef WINXP
RemoveClipboardFormatListener(hwnd);
#endif
DestroyWindow(hwnd);
#else
running = false;
#endif
}
DECLARE_API bool clipboard_set_image(HWND hwnd, void *ptr, size_t size)