From 0f00ef4fcf8399a60e5c6c5adb34eb6b214037b2 Mon Sep 17 00:00:00 2001 From: David Hall Date: Sun, 25 Dec 2022 17:05:38 -0700 Subject: [PATCH] Fixed NativeClipboard GetText and SetText methods. Also changed ctor so that if no HWND is passed, it uses the Desktop's window handle. Fixes #355. --- UnitTests/Windows.Shell/ClipboardTests.cs | 22 ++++++++++++++++++++-- Windows.Shell.Common/NativeClipboard.cs | 10 +++++----- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/UnitTests/Windows.Shell/ClipboardTests.cs b/UnitTests/Windows.Shell/ClipboardTests.cs index 598138f4..d6c4a53f 100644 --- a/UnitTests/Windows.Shell/ClipboardTests.cs +++ b/UnitTests/Windows.Shell/ClipboardTests.cs @@ -74,7 +74,7 @@ namespace Vanara.Windows.Shell.Tests [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()) { @@ -86,16 +86,34 @@ namespace Vanara.Windows.Shell.Tests [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, $"

{txt}

"); 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() { diff --git a/Windows.Shell.Common/NativeClipboard.cs b/Windows.Shell.Common/NativeClipboard.cs index 3aec8b6a..8bd2a6c6 100644 --- a/Windows.Shell.Common/NativeClipboard.cs +++ b/Windows.Shell.Common/NativeClipboard.cs @@ -69,6 +69,8 @@ namespace Vanara.Windows.Shell dontClose = true; return; } + if (hWndNewOwner == default) + hWndNewOwner = GetDesktopWindow(); if (!OpenClipboard(hWndNewOwner)) Win32Error.ThrowLastError(); open = true; @@ -362,8 +364,8 @@ namespace Vanara.Windows.Shell /// The string value or if the format is not available. 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), @@ -420,9 +422,7 @@ namespace Vanara.Windows.Shell 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); } @@ -434,7 +434,7 @@ namespace Vanara.Windows.Shell { (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)),