NetAutoGUI.Windows/ClipboardHelpers.cs
(完整代码) 1KBusing System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
namespace NetAutoGUI.Windows
{
public static class ClipboardHelpers
{
private static TResult RunInSTAThread<TResult>(Func<TResult> func)
{
//The Clipboard class can only be used in threads set to single thread apartment (STA) mode. To use this class, ensure that your Main method is marked with the STAThreadAttribute attribute.
//https://blog.csdn.net/wangyue4/article/details/39431725
TResult? result = default;
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
Thread thread = new Thread(() =>
{
result = func();
});
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
}
else
{
result = func();
}
return result;
}
public static IDataObject GetClipboardData()
{
return RunInSTAThread(() =>
{
return Clipboard.GetDataObject();
});
}
public static string GetClipboardText()
{
return RunInSTAThread(() =>
{
return Clipboard.GetText();
});
}
public static Image GetImage()
{
return RunInSTAThread(() =>
{
return Clipboard.GetImage();
});
}
}
}
24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明
AI 解读 AI
登录后可用,每次 10 算力,解读结果公开显示在下面。
还没有人解读过这个文件。