Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
June 20, 2020 How to work Get & Set text in clipboard in Windows ? | ||||
---|---|---|---|---|
| ||||
Hi all, I would like to know how to get & set text in clipboard. I am using windows machine. Thanks in advance. --Vinod Chandran |
June 20, 2020 Re: How to work Get & Set text in clipboard in Windows ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On Saturday, 20 June 2020 at 13:32:22 UTC, Vinod K Chandran wrote: > I would like to know how to get & set text in clipboard. I am using windows machine. This is an example of setting the clipboard using the Windows API in D: ``` /// Returns: true on success bool setClipboard(string str) { import core.stdc.string: memcpy; import std.string: toStringz; import core.sys.windows.windows: GlobalAlloc, GlobalLock, GlobalUnlock, OpenClipboard, EmptyClipboard, SetClipboardData, CloseClipboard, CF_TEXT, CF_UNICODETEXT, GMEM_MOVEABLE; auto hMem = GlobalAlloc(GMEM_MOVEABLE, str.length + 1); if (hMem is null) { return false; } memcpy(GlobalLock(hMem), str.ptr, str.length); (cast(char*) hMem)[str.length] = '\0'; // zero terminator GlobalUnlock(hMem); if (!OpenClipboard(null)) { return false; } EmptyClipboard(); if (!SetClipboardData(CF_TEXT, hMem)) { return false; } CloseClipboard(); return true; } ``` Here's an example of getting it, from arsd: https://github.com/adamdruppe/arsd/blob/ae17d5a497de93454f58421cd9d4a5ecd70594c0/terminal.d#L2043 Here is a C example from GLFW: https://github.com/glfw/glfw/blob/51a465ee2b50234f984efce0e229f7e9afceda9a/src/win32_window.c#L2150 |
June 20, 2020 Re: How to work Get & Set text in clipboard in Windows ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis | On Saturday, 20 June 2020 at 13:46:05 UTC, Dennis wrote:
>
Thanks a lot. Well, i thought it should be a one liner like-
Clipboard.SetText(sText)
But after reading your reply, i realized that this is D, not a scripting language. :)
|
June 22, 2020 Re: How to work Get & Set text in clipboard in Windows ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Vinod K Chandran | On Saturday, 20 June 2020 at 19:39:56 UTC, Vinod K Chandran wrote:
> On Saturday, 20 June 2020 at 13:46:05 UTC, Dennis wrote:
>>
> Thanks a lot. Well, i thought it should be a one liner like-
> Clipboard.SetText(sText)
> But after reading your reply, i realized that this is D, not a scripting language. :)
It would be a one-liner if it was an api. Such utility APIs are quite missing in D.
How about putting them together into a package?
|
June 22, 2020 Re: How to work Get & Set text in clipboard in Windows ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to aberba | On Monday, 22 June 2020 at 10:26:12 UTC, aberba wrote: > > It would be a one-liner if it was an api. Such utility APIs are quite missing in D. Um, You are right. > > How about putting them together into a package? Well, this is an inspiration to me. Let me try. :) |
Copyright © 1999-2021 by the D Language Foundation