C# DataFormats.GetFormat允许我创建新的私有格式吗?
我尝试了以下方法:C# DataFormats.GetFormat允许我创建新的私有格式吗?,c#,.net,winforms,base-class-library,C#,.net,Winforms,Base Class Library,我尝试了以下方法: DataFormats.Format binaryData = DataFormats.GetFormat("BinaryData"); 返回的binaryData.Id为50151 我可以假设“BinaryData”严格地说是我的私有名称,还是一个众所周知的名称 我这样问是因为我正在与第三方应用程序(colaSoft)进行接口,它将名为BinaryData的格式(Id也为Id也为50151)推送到剪贴板。这只是巧合吗?如何确定Id?来自以下文档: 此方法也可用于注册新格式
DataFormats.Format binaryData = DataFormats.GetFormat("BinaryData");
返回的binaryData.Id为50151
我可以假设“BinaryData”严格地说是我的私有名称,还是一个众所周知的名称
我这样问是因为我正在与第三方应用程序(colaSoft)进行接口,它将名为
BinaryData
的格式(Id也为Id
也为50151)推送到剪贴板。这只是巧合吗?如何确定Id
?来自以下文档:
此方法也可用于注册新格式。如果找不到指定的格式,将自动将其注册为新的数据格式
这并不能回答您的问题:
我可以假设“BinaryData”严格地说是我的私有名称,还是一个众所周知的名称
因此,下一步是查看该方法的源代码
public static Format GetFormat(string format) {
lock(internalSyncObject) {
EnsurePredefined();
// It is much faster to do a case sensitive search here. So do
// the case sensitive compare first, then the expensive one.
//
for (int n = 0; n < formatCount; n++) {
if (formatList[n].Name.Equals(format))
return formatList[n];
}
for (int n = 0; n < formatCount; n++) {
if (String.Equals(formatList[n].Name, format, StringComparison.OrdinalIgnoreCase))
return formatList[n];
}
// need to add this format string
//
int formatId = SafeNativeMethods.RegisterClipboardFormat(format);
if (0 == formatId) {
throw new Win32Exception(Marshal.GetLastWin32Error(), SR.GetString(SR.RegisterCFFailed));
}
EnsureFormatSpace(1);
formatList[formatCount] = new Format(format, formatId);
return formatList[formatCount++];
}
}
现在,请参阅以下文档:
如果已存在具有指定名称的已注册格式,则会出现新的
未注册格式,返回值标识现有格式
格式。这使多个应用程序能够复制和粘贴数据
使用相同的注册剪贴板格式。请注意,格式名称
比较不区分大小写
注册的剪贴板格式由范围内的值标识
0xC000到0xFFFF
根据这一点以及每个会话只有一个剪贴板的事实,您应该能够推断出格式ID在给定会话中是通用的
至于ID是如何生成的,我无法回答这一部分
我没有访问该代码的权限
[DllImport(ExternDll.User32, SetLastError=true, CharSet=System.Runtime.InteropServices.CharSet.Auto)]
[ResourceExposure(ResourceScope.None)]
public static extern int RegisterClipboardFormat(string format);