简体   繁体   中英

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.

Can I assume that "BinaryData" is strictly a name private to me or it is a well known name?

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? How is the Id determined?

From the documentation for the DataFormats.GetDataFormat Method :

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?

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.

    [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 :

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.

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.

As far as how the ID is generated, I would can not answer that part as I do not have access to that code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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