Skip to content

Commit

Permalink
Fixed NativeClipboard GetText and SetText methods. Also changed ctor …
Browse files Browse the repository at this point in the history
…so that if no HWND is passed, it uses the Desktop's window handle. Fixes #355.
  • Loading branch information
dahall committed Dec 26, 2022
1 parent 5d6cf1b commit 0f00ef4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
22 changes: 20 additions & 2 deletions UnitTests/Windows.Shell/ClipboardTests.cs
Expand Up @@ -74,7 +74,7 @@ public void GetSetShellItems()
[Test]
public void SetNativeTextHtmlTest()
{
using (var cb = new Clipboard())
using (var cb = new Clipboard(true))
cb.SetText(html, TextDataFormat.Html);
using (var cb = new Clipboard())
{
Expand All @@ -86,16 +86,34 @@ public void SetNativeTextHtmlTest()
[Test]
public void SetNativeTextMultTest()
{
const string txt = @"“We’ve been here”";
const string stxt = "112233";
using (var cb = new Clipboard(true))
cb.SetText(stxt);
using (var cb = new Clipboard())
Assert.That(cb.GetText(TextDataFormat.Text), Is.EqualTo(stxt));

const string txt = @"“0’0©0è0”";
using (var cb = new Clipboard(true))
cb.SetText(txt, $"<p>{txt}</p>");
using (var cb = new Clipboard())
{
Assert.That(cb.GetText(TextDataFormat.Text), Is.EqualTo(txt));
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
Assert.That(cb.GetText(TextDataFormat.Html), Contains.Substring(txt));
TestContext.WriteLine(cb.GetText(TextDataFormat.Html));
}
}

[Test]
public void SetNativeTextUnicodeTest()
{
const string txt = @"“0’0©0è0”";
using (var cb = new Clipboard(true))
cb.SetText(txt, TextDataFormat.UnicodeText);
using (var cb = new Clipboard())
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
}

//[Test]
public void ChangeEventTest()
{
Expand Down
10 changes: 5 additions & 5 deletions Windows.Shell.Common/NativeClipboard.cs
Expand Up @@ -69,6 +69,8 @@ public NativeClipboard(bool empty = false, HWND hWndNewOwner = default)
dontClose = true;
return;
}
if (hWndNewOwner == default)
hWndNewOwner = GetDesktopWindow();
if (!OpenClipboard(hWndNewOwner))
Win32Error.ThrowLastError();
open = true;
Expand Down Expand Up @@ -362,8 +364,8 @@ public void Dispose()
/// <returns>The string value or <see langword="null"/> if the format is not available.</returns>
public string GetText(TextDataFormat formatId) => formatId switch
{
TextDataFormat.Text => StringHelper.GetString(GetClipboardData(CLIPFORMAT.CF_TEXT), CharSet.Ansi),
TextDataFormat.UnicodeText => StringHelper.GetString(GetClipboardData(CLIPFORMAT.CF_UNICODETEXT), CharSet.Unicode),
TextDataFormat.Text => Marshal.PtrToStringAnsi(GetClipboardData(CLIPFORMAT.CF_TEXT)),
TextDataFormat.UnicodeText => Marshal.PtrToStringUni(GetClipboardData(CLIPFORMAT.CF_UNICODETEXT)),
TextDataFormat.Rtf => StringHelper.GetString(GetClipboardData(RegisterFormat(ShellClipboardFormat.CF_RTF)), CharSet.Ansi),
TextDataFormat.Html => Utils.GetHtml(GetClipboardData(RegisterFormat(ShellClipboardFormat.CF_HTML))),
TextDataFormat.CommaSeparatedValue => StringHelper.GetString(GetClipboardData(RegisterFormat(ShellClipboardFormat.CF_CSV)), CharSet.Ansi),
Expand Down Expand Up @@ -420,9 +422,7 @@ public void SetData(uint formatId, IEnumerable<string> values, StringListPackMet
public void SetText(string text, string htmlText = null, string rtfText = null)
{
if (text is null && htmlText is null && rtfText is null) return;
SetText(text, TextDataFormat.Text);
SetText(text, TextDataFormat.UnicodeText);
SetBinaryData(RegisterFormat("Locale"), BitConverter.GetBytes(CultureInfo.CurrentCulture.LCID));
if (htmlText != null) SetText(htmlText, TextDataFormat.Html);
if (rtfText != null) SetText(rtfText, TextDataFormat.Rtf);
}
Expand All @@ -434,7 +434,7 @@ public void SetText(string value, TextDataFormat format)
{
(byte[] bytes, uint fmt) = format switch
{
TextDataFormat.Text => (Encoding.ASCII.GetBytes(value + '\0'), (uint)CLIPFORMAT.CF_TEXT),
TextDataFormat.Text => (UnicodeToAnsiBytes(value), (uint)CLIPFORMAT.CF_TEXT),
TextDataFormat.UnicodeText => (Encoding.Unicode.GetBytes(value + '\0'), (uint)CLIPFORMAT.CF_UNICODETEXT),
TextDataFormat.Rtf => (Encoding.ASCII.GetBytes(value + '\0'), RegisterFormat(ShellClipboardFormat.CF_RTF)),
TextDataFormat.Html => (FormatHtmlForClipboard(value), RegisterFormat(ShellClipboardFormat.CF_HTML)),
Expand Down

0 comments on commit 0f00ef4

Please sign in to comment.