简体   繁体   English

DataFormats.GetFormat 是否允许我创建新的私有格式?

[英]Does DataFormats.GetFormat allow me to create a new private format?

I tried the following:我尝试了以下方法:

DataFormats.Format binaryData = DataFormats.GetFormat("BinaryData");

and the returned binaryData.Id is 50151.并且返回的 binaryData.Id 是 50151。

Can I assume that "BinaryData" is strictly a name private to me or it is a well known name?我可以假设“BinaryData”严格来说是我的私有名称还是众所周知的名称?

I ask because there is a third party application I am interfacing to (colaSoft) and it pushes to the clipboard a format called BinaryData also with the Id is also 50151. Is it just a coincidence?我问是因为有一个第三方应用程序与我连接(colaSoft),它向剪贴板推送一个名为BinaryData的格式, Id也是 50151。这只是巧合吗? How is the Id determined? Id是如何确定的?

From the documentation for the DataFormats.GetDataFormat Method :DataFormats.GetDataFormat 方法的文档中:

This method can also be used to register new formats.此方法还可用于注册新格式。 If the specified format cannot be found, it is automatically registered as a new data format.如果找不到指定的格式,它会自动注册为新的数据格式。

This does not answer the part of your question:这不能回答您的问题的一部分:

Can I assume that "BinaryData" is strictly a name private to me or it is a well known name?我可以假设“BinaryData”严格来说是我的私有名称还是众所周知的名称?

So the next step is to look at the source code for the method.所以下一步是查看该方法的源代码。

    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++];
        }
    }

You should notice from the code that if the format does not exist if is registed by calling SafeNativeMethods.RegisterClipboardFormat that is declared as follows.您应该从代码中注意到,如果格式不存在,则通过调用SafeNativeMethods.RegisterClipboardFormat注册,声明如下。

    [DllImport(ExternDll.User32, SetLastError=true, CharSet=System.Runtime.InteropServices.CharSet.Auto)]
    [ResourceExposure(ResourceScope.None)]
    public static extern int RegisterClipboardFormat(string format);

Now from the documentation for the RegisterClipboardFormat function :现在从RegisterClipboardFormat 函数的文档:

If a registered format with the specified name already exists, a new format is not registered and the return value identifies the existing format.如果具有指定名称的已注册格式已存在,则不会注册新格式,并且返回值标识现有格式。 This enables more than one application to copy and paste data using the same registered clipboard format.这使得多个应用程序能够使用相同的注册剪贴板格式复制和粘贴数据。 Note that the format name comparison is case-insensitive.请注意,格式名称比较不区分大小写。

Registered clipboard formats are identified by values in the range 0xC000 through 0xFFFF.注册的剪贴板格式由 0xC000 到 0xFFFF 范围内的值标识。

From this and the fact that there is only one Clipboard per session, you should be able to infer that the format ID is common in a given session.从这一点以及每个会话只有一个剪贴板这一事实,您应该能够推断出格式 ID 在给定会话中是常见的。

As far as how the ID is generated, I would can not answer that part as I do not have access to that code.至于如何生成 ID,我无法回答该部分,因为我无法访问该代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM