如果剪贴板真的是空的并且没有任何格式,请使用C#进行检查 [英] Check with C# if the clipboard is really empty and no any format is there

查看:261
本文介绍了如果剪贴板真的是空的并且没有任何格式,请使用C#进行检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的ClipBoard真的没有任何东西,我想用自定义的C#方法检查IsClipBoardEmpty。该方法应该是纯C#,不需要任何STA_THREAD或外部API或其他任何东西。我尝试过不同的解决方案,但对我来说并不适用。我有一个菜单项清除剪贴板。我想在剪贴板为空时禁用此菜单项。空的我的意思是:它很清楚,里面没有文字,没有图像,没有文件提示,没有图形或任何其他自定义对象。我已经看到了需要STA_THREAD的不同方法,但是我需要一个不需要STA_THREAD的函数。



这里我的自定义代码尝试不起作用:< br $> b $ b

  //   1  
DataObject retrieveData =(DataObject)Clipboard.GetDataObject();
if (retrieveData == null
{
toolStripMenuItem1。 enabled = false ;
}

// 2
object retrieveData = ClipBoard.GetText();
if (retrieveData == null
{
toolStripMenuItem1。 enabled = false ;
}

// 3
if ((Clipboard.GetText()。toString()!=
{
toolStripMenuItem1.enabled = true ;
}
else
{
toolStripMenuItem1.enabled = false ;
}





我的尝试:



我发布的功能是我试过了。没什么用。



这也行不通。函数返回总是假,

即使剪贴板已经空的。



  public   static   bool  IsClipboardEmpty(){
return (Clipboard.GetDataObject() == );
}





我需要的是像Clipboard.IsEmpty()这样的扩展方法。

我已经尝试为C#添加所有现有的using语句和引用,并将我的应用程序的大小增加到300 MB。无论如何,扩展方法保留所有对系统中所有兼容DLL的引用仍然未知。 DLL可以包含这样的函数的任何想法吗?

解决方案

根据 Clipboard.GetDataObject Method(System.Windows) [ ^ ]它应该是 NULL 当没有数据时:

Quote:

启用的数据对象访问系统剪贴板的全部内容,如果剪贴板上没有数据,则返回null。



如果这不是 NULL ,您可以使用 DataObject.GetFormats Method(布尔)查看可用格式(System.Windows) [ ^ ]。



我还没有测试过,但返回的字符串当剪贴板上没有数据时,数组应为空。



如果这也不起作用(并且有返回格式的数据),则可能无法实现不使用STA线程。


而不是使用剪贴板。获取 xxxx 方法使用 Clipboard.ContainsData 方法。



您需要传递一个参数,该参数是<$ c $的文字说明c> DataFormat 您正在寻找。

此函数首先枚举所有 DataFormats ,然后检查剪贴板该格式的数据。

 使用 System.Linq; 
...
public static bool IsClipboardEmpty()
{
var dataFormats = typeof (DataFormats).GetFields(BindingFlags.Public | BindingFlags.Static)
.Select(f = > f.Name)
.ToList( );
var containsSomething = dataFormats.Aggregate( false ,(current,x)= > current || Clipboard.ContainsData(x));

return (!containsSomething);
}



我通过使用简单的计时器

  private   void  timer1_Tick( object  sender,EventArgs e)
{
menuStrip1.Items [ 0 ]。Enabled =!IsClipboardEmpty();
}

并通过复制各种项目(图片,文本,文件)和菜单在启用和禁用之间切换。





 使用 System.Collections.Generic ; 
使用 System.Reflection;
...
public static bool IsClipboardEmpty()
{
var dataFormats = new 列表与LT;串GT;();
foreach var x in typeof (DataFormats).GetFields(BindingFlags.Public | BindingFlags.Static))
dataFormats.Add(x.Name);

var containsSomething = false ;
foreach var y in dataFormats)
containsSomething = containsSomething || Clipboard.ContainsData(Y);

return (!containsSomething);
}





请参阅Matt T Heffron的解决方案5,以提高此解决方案的效率,即使用<$一旦发现某些内容,Linq版本和.NET2版本中的c $ c> .Any 而不是 .Aggregate 会突破循环例如

  foreach  var  y < span class =code-keyword> in  dataFormats)
{
containsSomething = containsSomething || Clipboard.ContainsData(Y);
if (containsSomething) break ;
}


@ CHill60对解决方案3的轻微改进

看起来像。聚合(...)可能更有效,因为 .Any(...),如:

< pre lang =c#> 使用 System.Linq;
...
public static bool IsClipboardEmpty()
{
var dataFormats = typeof (DataFormats).GetFields(BindingFlags.Public | BindingFlags.Static)
。选择(f = > f.Name);
var containsSomething = dataFormats.Any(x = > Clipboard.ContainsData(x)) ;

return (!containsSomething);
}



一旦找到, .Any(...)将立即停止。 .Aggregate(...)将继续完成 dataFormats 的整个集合。

同样,一旦 Clipboard.ContainsData(y)返回true,.NET 2.0版本就可以提前退出。


I would like to check with a custom C# method alike "IsClipBoardEmpty" if my ClipBoard is really empty of anything. The method shall be in pure C# and not require any STA_THREAD or external API or whatever. I have tried different solutions which weren't working for me. I have a menu item "Clear Clipboard". This menu item I want to disable when the clipboard is empty. With empty I mean: it is clear and has no text, no images, no filedrops, no graphics or any other custom objects inside. I have seen different methods with requiring a "STA_THREAD", but I need a function not requiring a STA_THREAD.

Here my custom code attempts which weren't working:

//1
DataObject retrievedData = (DataObject)Clipboard.GetDataObject();
if(retrievedData==null)
{
toolStripMenuItem1.enabled=false;
}

//2
object retrievedData = ClipBoard.GetText();
if(retrievedData==null)
{
toolStripMenuItem1.enabled=false;
}

//3
if((Clipboard.GetText().toString()!="")
{
toolStripMenuItem1.enabled=true;
}
else
{
toolStripMenuItem1.enabled=false;
}



What I have tried:

I posted the functions which I have tried. Nothing worked.

This also doesn't work. the function returns always false,
even if the Clipboard is already empty.

public static bool IsClipboardEmpty() {
   return (Clipboard.GetDataObject() == null);
}



And what I need would be an extension method like "Clipboard.IsEmpty()".
I already tried to add all existing using statements and references for C# and increased the size of my application to 300 MB. Anyway the extension method stays with all the references to all compatible DLL's in the system still unknown. Any ideas which DLL could contain such function?

解决方案

According to the documentation at Clipboard.GetDataObject Method (System.Windows)[^] it should be NULL when there are no data:

Quote:

A data object that enables access to the entire contents of the system Clipboard, or null if there is no data on the Clipboard.


If this is not NULL, you may check the available formats using DataObject.GetFormats Method (Boolean) (System.Windows)[^].

I have not tested it but the returned string array should be empty when there are no data on the clipboard.

If this does not work too (and there are data for the returned formats), it might be impossible without using a STA thread.


Instead of using the Clipboard.Getxxxx methods use the Clipboard.ContainsData method.

You need to pass this a parameter which is the textual description of the DataFormat you are looking for.
This function first enumerates all of the DataFormats and then checks the clipboard for data in that format.

using System.Linq;
...
public static bool IsClipboardEmpty()
{
    var dataFormats = typeof(DataFormats).GetFields(BindingFlags.Public | BindingFlags.Static)
                       .Select(f => f.Name)
                       .ToList();
    var containsSomething = dataFormats.Aggregate(false, (current, x) => current || Clipboard.ContainsData(x));

    return (!containsSomething);
}


I tested this by using a simple timer

private void timer1_Tick(object sender, EventArgs e)
{
    menuStrip1.Items[0].Enabled = !IsClipboardEmpty();
}

and by copying various items (pictures, text, files) and the menu toggled between enabled and disabled.

[EDIT - for completeness here is the same thing targeting .NET 2.0]

using System.Collections.Generic;
using System.Reflection;
...
public static bool IsClipboardEmpty()
{
    var dataFormats = new List<string>();
    foreach (var x in typeof (DataFormats).GetFields(BindingFlags.Public | BindingFlags.Static))
        dataFormats.Add(x.Name);

    var containsSomething = false;
    foreach(var y in dataFormats)
        containsSomething = containsSomething || Clipboard.ContainsData(y);

    return (!containsSomething);
}



[EDIT] See Solution 5 below from Matt T Heffron for an efficiency improvement to this solution i.e. use .Any instead of .Aggregate in the Linq version and in the .NET2 version break out of the loop once something is found, e.g.

foreach (var y in dataFormats)
{
    containsSomething = containsSomething || Clipboard.ContainsData(y);
    if (containsSomething) break;
}


Slight improvement on Solution 3 by @CHill60
It looks like the .Aggregate(...) could be more efficient as .Any(...), like:

using System.Linq;
...
public static bool IsClipboardEmpty()
{
    var dataFormats = typeof(DataFormats).GetFields(BindingFlags.Public | BindingFlags.Static)
                       .Select(f => f.Name);
    var containsSomething = dataFormats.Any(x => Clipboard.ContainsData(x));
 
    return (!containsSomething);
}


The .Any(...) will stop as soon as one is found. .Aggregate(...) will proceed through the whole collection of dataFormats.
The .NET 2.0 version could, likewise, early exit once the Clipboard.ContainsData(y) returns true.


这篇关于如果剪贴板真的是空的并且没有任何格式,请使用C#进行检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆