-
Notifications
You must be signed in to change notification settings - Fork 102
Clipboard helpers were added #15 #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
4fd9fa5
2743694
f3640b8
bdc1551
1d3fdbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,13 @@ | ||
| using System; | ||
| using System.Collections; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Runtime.InteropServices; | ||
| using System.Text; | ||
| using NetCoreEx.BinaryExtensions; | ||
| using NetCoreEx.Geometry; | ||
| using WinApi.Gdi32; | ||
| using WinApi.Kernel32; | ||
|
|
||
| namespace WinApi.User32 | ||
| { | ||
|
|
@@ -213,5 +220,63 @@ public static bool InverseAdjustWindowRectEx( | |
| if (res) Rectangle.Subtract(ref lpRect, ref rc); | ||
| return res; | ||
| } | ||
|
|
||
| public static void PasteTextToClipboard(byte[] data) | ||
| { | ||
| if (User32Methods.OpenClipboard(new IntPtr())) | ||
| { | ||
| var bytesLength = data.Length; | ||
| var allocatedMemory = Marshal.AllocHGlobal(bytesLength); | ||
| try | ||
| { | ||
| Marshal.Copy(data, 0, allocatedMemory, bytesLength); | ||
|
|
||
| var result = User32Methods.SetClipboardData((uint)ClipboardFormat.CF_TEXT, allocatedMemory); | ||
|
|
||
| if (result == IntPtr.Zero) | ||
|
||
| throw new Exception(Kernel32Methods.GetLastError().ToString()); | ||
| } | ||
| finally | ||
| { | ||
| Marshal.FreeHGlobal(allocatedMemory); | ||
| } | ||
| } | ||
| User32Methods.CloseClipboard(); | ||
|
|
||
| } | ||
|
|
||
| public static unsafe string GetUnicodeTextFromClipboard() | ||
| { | ||
| var outString = default(string); | ||
|
||
| if (User32Methods.OpenClipboard(new IntPtr())) | ||
| { | ||
| var data = (char*)User32Methods.GetClipboardData((uint) ClipboardFormat.CF_UNICODETEXT); | ||
| outString = new string(data); | ||
| } | ||
| User32Methods.CloseClipboard(); | ||
|
||
| return outString; | ||
| } | ||
|
|
||
| public static unsafe string GetTextFromClipboard() | ||
|
||
| { | ||
| var outString = default(string); | ||
| if (User32Methods.OpenClipboard(new IntPtr())) | ||
| { | ||
| var data = (char*)User32Methods.GetClipboardData((uint)ClipboardFormat.CF_TEXT); | ||
| outString = new string(data); | ||
| } | ||
| User32Methods.CloseClipboard(); | ||
| return outString; | ||
| } | ||
|
|
||
| public static unsafe ClipboardFormat GetPriorityClipboardFormat(ClipboardFormat[] format) | ||
| { | ||
| fixed (ClipboardFormat* first = &format[0]) | ||
| { | ||
| var result = User32Methods.GetPriorityClipboardFormat((IntPtr) first, format.Length); | ||
|
|
||
| return result >= 0 ? (ClipboardFormat) result : ClipboardFormat.CF_ZERO; | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The try-finally is useless here.
Marshal.Copyis not going to throw. InfactAllocHGlobalis the one that can throw that's outside the scope - though it's not needed to handle it. Just simple statements will do, since ifOutOfMemoryis thrown, the application will have to react to it anyway.