Hopefully fixed #355

pull/363/head
David Hall 2022-12-30 13:56:19 -07:00
parent 04cb2edf66
commit 7ca924e5f7
2 changed files with 20 additions and 2 deletions

View File

@ -28,6 +28,8 @@ namespace Vanara.Windows.Shell.Tests
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
using (var cb = new Clipboard(true, User32.GetActiveWindow()))
cb.SetText(txt, txt, txt);
using (var cb = new Clipboard(false, User32.GetActiveWindow()))
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
using (var cb = new Clipboard())
Assert.That(cb.GetText(TextDataFormat.UnicodeText), Is.EqualTo(txt));
}

View File

@ -71,8 +71,7 @@ namespace Vanara.Windows.Shell
}
if (hWndNewOwner == default)
hWndNewOwner = GetDesktopWindow();
if (!OpenClipboard(hWndNewOwner))
Win32Error.ThrowLastError();
Win32Error.ThrowLastErrorIfFalse(OpenClipboard(hWndNewOwner));
open = true;
if (empty)
Empty();
@ -127,6 +126,19 @@ namespace Vanara.Windows.Shell
set => OleSetClipboard(value).ThrowIfFailed();
}
/// <summary>Retrieves the clipboard sequence number for the current window station.</summary>
/// <returns>
/// The clipboard sequence number. If you do not have <c>WINSTA_ACCESSCLIPBOARD</c> access to the window station, the function
/// returns zero.
/// </returns>
/// <remarks>
/// The system keeps a serial number for the clipboard for each window station. This number is incremented whenever the contents of
/// the clipboard change or the clipboard is emptied. You can track this value to determine whether the clipboard contents have
/// changed and optimize creating DataObjects. If clipboard rendering is delayed, the sequence number is not incremented until the
/// changes are rendered.
/// </remarks>
public static uint SequenceNumber => GetClipboardSequenceNumber();
/// <summary>Carries out the clipboard shutdown sequence. It also releases any IDataObject instances that were placed on the clipboard.</summary>
public static void Flush() => OleFlushClipboard().ThrowIfFailed();
@ -391,6 +403,7 @@ namespace Vanara.Windows.Shell
{
using var pMem = new SafeMoveableHGlobalHandle(data);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}
@ -401,6 +414,7 @@ namespace Vanara.Windows.Shell
{
using var pMem = SafeMoveableHGlobalHandle.CreateFromStructure(data);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}
@ -411,6 +425,7 @@ namespace Vanara.Windows.Shell
{
using var pMem = SafeMoveableHGlobalHandle.CreateFromList(values);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}
@ -423,6 +438,7 @@ namespace Vanara.Windows.Shell
{
using var pMem = SafeMoveableHGlobalHandle.CreateFromStringList(values, packing, charSet);
Win32Error.ThrowLastErrorIfInvalid(pMem);
pMem.Unlock();
Win32Error.ThrowLastErrorIfNull(SetClipboardData(formatId, pMem.DangerousGetHandle()));
}