add get text
MODIFY
Directory.Build.props
Directory.Build.props
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<LangVersion>latest</LangVersion>
<NoWarn>CS1591</NoWarn>
- <Version>1.0.1</Version>
+ <Version>1.1.0</Version>
<AssemblyVersion>1.0</AssemblyVersion>
<AssemblyInformationalVersion>$(Version)</AssemblyInformationalVersion>
<Authors>Simon Cropp</Authors>

MODIFY
README.md
README.md
# TextCopy
-A netstandard package to copy text to the clipboard.
+A netstandard package to copy text to and from the clipboard.
## NuGet [![NuGet Status](http://img.shields.io/nuget/v/TextCopy.svg?style=flat)](https://www.nuget.org/packages/TextCopy/)
@@ -12,10 +12,18 @@ https://nuget.org/packages/TextCopy/
### Usage
+#### SetText
+
```csharp
TextCopy.Clipboard.SetText("Text to place in clipboard");
```
+#### SetText
+
+```csharp
+var text = TextCopy.Clipboard.GetText();
+```
+
## Icon

MODIFY
TextCopy/Clipboard.cs
TextCopy/Clipboard.cs
@@ -5,7 +5,8 @@ namespace TextCopy
{
public static class Clipboard
{
- static Action<string> action = CreateClipboard();
+ static Action<string> setAction = CreateSet();
+ static Func<string> getFunc = CreateGet();
public static void SetText(string text)
{
@@ -14,10 +15,15 @@ namespace TextCopy
throw new ArgumentNullException(nameof(text));
}
- action(text);
+ setAction(text);
}
- static Action<string> CreateClipboard()
+ public static string GetText()
+ {
+ return getFunc();
+ }
+
+ static Action<string> CreateSet()
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
@@ -31,5 +37,16 @@ namespace TextCopy
return s => throw new NotSupportedException();
}
+
+ static Func<string> CreateGet()
+ {
+ if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
+ {
+ return WindowsClipboard.GetText;
+ }
+
+
+ return () => throw new NotSupportedException();
+ }
}
}
\ No newline at end of file

MODIFY
TextCopy/WindowsClipboard.cs
TextCopy/WindowsClipboard.cs
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
+using System.Text;
+using System.Threading;
static class WindowsClipboard
{
public static void SetText(string text)
{
- if (!OpenClipboard(IntPtr.Zero))
- {
- ThrowWin32();
- }
+ OpenClipboard();
EmptyClipboard();
- var hGlobal = IntPtr.Zero;
+ IntPtr hGlobal = default;
try
{
var bytes = (text.Length + 1) * 2;
hGlobal = Marshal.AllocHGlobal(bytes);
- if (hGlobal == IntPtr.Zero)
+ if (hGlobal == default)
{
- throw new Win32Exception(Marshal.GetLastWin32Error());
+ ThrowWin32();
}
var target = GlobalLock(hGlobal);
- if (target == IntPtr.Zero)
+ if (target == default)
{
ThrowWin32();
}
@@ -39,17 +38,16 @@ static class WindowsClipboard
GlobalUnlock(target);
}
- const uint cfUnicodeText = 13;
- if (SetClipboardData(cfUnicodeText, hGlobal) == IntPtr.Zero)
+ if (SetClipboardData(cfUnicodeText, hGlobal) == default)
{
ThrowWin32();
}
- hGlobal = IntPtr.Zero;
+ hGlobal = default;
}
finally
{
- if (hGlobal != IntPtr.Zero)
+ if (hGlobal != default)
{
Marshal.FreeHGlobal(hGlobal);
}
@@ -58,11 +56,82 @@ static class WindowsClipboard
}
}
+ public static void OpenClipboard()
+ {
+ var num = 10;
+ while (true)
+ {
+ if (OpenClipboard(default))
+ {
+ break;
+ }
+
+ if (--num == 0)
+ {
+ ThrowWin32();
+ }
+
+ Thread.Sleep(100);
+ }
+ }
+
+ public static string GetText()
+ {
+ if (!IsClipboardFormatAvailable(cfUnicodeText))
+ {
+ return null;
+ }
+
+ IntPtr handle = default;
+
+ IntPtr pointer = default;
+ try
+ {
+ OpenClipboard();
+ handle = GetClipboardData(cfUnicodeText);
+ if (handle == default)
+ {
+ return null;
+ }
+
+ pointer = GlobalLock(handle);
+ if (pointer == default)
+ {
+ return null;
+ }
+
+ var size = GlobalSize(handle);
+ var buff = new byte[size];
+
+ Marshal.Copy(pointer, buff, 0, size);
+
+ return Encoding.Unicode.GetString(buff).TrimEnd('\0');
+ }
+ finally
+ {
+ if (pointer != default)
+ {
+ GlobalUnlock(handle);
+ }
+
+ CloseClipboard();
+ }
+ }
+
+ const uint cfUnicodeText = 13;
+
static void ThrowWin32()
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
+ [DllImport("User32.dll", SetLastError = true)]
+ [return: MarshalAs(UnmanagedType.Bool)]
+ static extern bool IsClipboardFormatAvailable(uint format);
+
+ [DllImport("User32.dll", SetLastError = true)]
+ static extern IntPtr GetClipboardData(uint uFormat);
+
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GlobalLock(IntPtr hMem);
@@ -83,4 +152,7 @@ static class WindowsClipboard
[DllImport("user32.dll")]
static extern bool EmptyClipboard();
+
+ [DllImport("Kernel32.dll", SetLastError = true)]
+ static extern int GlobalSize(IntPtr hMem);
}
\ No newline at end of file

add get text