From 7d4e2d44caffa545ffe9a2e8d1d19789cd91ed65 Mon Sep 17 00:00:00 2001 From: Igor Vasilenko Date: Sun, 20 Aug 2023 22:03:20 +0100 Subject: [PATCH] Add bitmap setting to clipboard on for windows --- include/GLFW/glfw3.h | 3 +++ src/input.c | 6 ++++++ src/internal.h | 1 + src/win32_init.c | 1 + src/win32_platform.h | 1 + src/win32_window.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 55 insertions(+) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 58b395cd..ec5bcba7 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -5823,6 +5823,9 @@ GLFWAPI int glfwGetGamepadState(int jid, GLFWgamepadstate* state); */ GLFWAPI void glfwSetClipboardString(GLFWwindow* window, const char* string); +// hacky way for a screenshot +GLFWAPI void glfwSetClipboardBitmap(unsigned char * data, int width, int height); + /*! @brief Returns the contents of the clipboard as a string. * * This function returns the contents of the system clipboard, if it contains diff --git a/src/input.c b/src/input.c index 36128e10..d2b8a0bd 100644 --- a/src/input.c +++ b/src/input.c @@ -1459,6 +1459,12 @@ GLFWAPI void glfwSetClipboardString(GLFWwindow* handle, const char* string) _glfw.platform.setClipboardString(string); } +GLFWAPI void glfwSetClipboardBitmap(unsigned char * data, int width, int height) +{ + _GLFW_REQUIRE_INIT(); + _glfw.platform.setClipboardBitmap(); +} + GLFWAPI const char* glfwGetClipboardString(GLFWwindow* handle) { _GLFW_REQUIRE_INIT_OR_RETURN(NULL); diff --git a/src/internal.h b/src/internal.h index fe0369aa..d15641e7 100644 --- a/src/internal.h +++ b/src/internal.h @@ -750,6 +750,7 @@ struct _GLFWplatform void (*getRequiredInstanceExtensions)(char**); GLFWbool (*getPhysicalDevicePresentationSupport)(VkInstance,VkPhysicalDevice,uint32_t); VkResult (*createWindowSurface)(VkInstance,_GLFWwindow*,const VkAllocationCallbacks*,VkSurfaceKHR*); + void (*setClipboardBitmap)(); }; // Library global data diff --git a/src/win32_init.c b/src/win32_init.c index ef2615f1..afde41c0 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -677,6 +677,7 @@ GLFWbool _glfwConnectWin32(int platformID, _GLFWplatform* platform) _glfwGetRequiredInstanceExtensionsWin32, _glfwGetPhysicalDevicePresentationSupportWin32, _glfwCreateWindowSurfaceWin32, + _glfwSetClipboardBitmapWin32 }; *platform = win32; diff --git a/src/win32_platform.h b/src/win32_platform.h index 82b34bb9..de9ed1b8 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -591,6 +591,7 @@ GLFWbool _glfwCreateStandardCursorWin32(_GLFWcursor* cursor, int shape); void _glfwDestroyCursorWin32(_GLFWcursor* cursor); void _glfwSetCursorWin32(_GLFWwindow* window, _GLFWcursor* cursor); void _glfwSetClipboardStringWin32(const char* string); +void _glfwSetClipboardBitmapWin32(); const char* _glfwGetClipboardStringWin32(void); EGLenum _glfwGetEGLPlatformWin32(EGLint** attribs); diff --git a/src/win32_window.c b/src/win32_window.c index 676640bf..b2eb868b 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -2291,6 +2291,49 @@ void _glfwSetCursorWin32(_GLFWwindow* window, _GLFWcursor* cursor) updateCursorImage(window); } +void _glfwSetClipboardBitmapWin32(unsigned char * data, int width, int height) { + // HDC hdcScreen; + // HDC hdcMemDC = NULL; + // HBITMAP hbmScreen = NULL; + HBITMAP fillBm = NULL; + // Retrieve the handle to a display device context for the client + // area of the window. + // hdcScreen = GetDC(NULL); + + // and a device context to put it in + // hdcMemDC = CreateCompatibleDC(hdcScreen); + + // int width = 1920; + // int height = 1080; + + // Create a compatible bitmap from the Window DC. + // hbmScreen = CreateCompatibleBitmap(hdcScreen, width, height); + fillBm = CreateBitmap(width, height, 1, 32, data); + + // get a new bitmap + // HBITMAP hOldBitmap = (HBITMAP)SelectObject(hdcMemDC, hbmScreen); + // BitBlt(hdcMemDC, 0, 0, width, height, hdcScreen, 0, 0, SRCCOPY); + // hbmScreen = (HBITMAP)SelectObject(hdcMemDC, hOldBitmap); + + + if (!OpenClipboard(_glfw.win32.helperWindowHandle)) { + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, "Win32: Failed to open clipboard"); + return; + } + EmptyClipboard(); + + // place handle to clipboard + SetClipboardData(CF_BITMAP, fillBm); + + // Close the clipboard. + CloseClipboard(); + DeleteObject(fillBm); + // DeleteObject(hbmScreen); + // DeleteObject(hOldBitmap); + // DeleteObject(hdcMemDC); + // ReleaseDC(NULL, hdcScreen); +} + void _glfwSetClipboardStringWin32(const char* string) { int characterCount;