簡體   English   中英

DataFormats.GetFormat 是否允許我創建新的私有格式?

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

我嘗試了以下方法:

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

並且返回的 binaryData.Id 是 50151。

我可以假設“BinaryData”嚴格來說是我的私有名稱還是眾所周知的名稱?

我問是因為有一個第三方應用程序與我連接(colaSoft),它向剪貼板推送一個名為BinaryData的格式, Id也是 50151。這只是巧合嗎? Id是如何確定的?

DataFormats.GetDataFormat 方法的文檔中:

此方法還可用於注冊新格式。 如果找不到指定的格式,它會自動注冊為新的數據格式。

這不能回答您的問題的一部分:

我可以假設“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++];
        }
    }

您應該從代碼中注意到,如果格式不存在,則通過調用SafeNativeMethods.RegisterClipboardFormat注冊,聲明如下。

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

現在從RegisterClipboardFormat 函數的文檔:

如果具有指定名稱的已注冊格式已存在,則不會注冊新格式,並且返回值標識現有格式。 這使得多個應用程序能夠使用相同的注冊剪貼板格式復制和粘貼數據。 請注意,格式名稱比較不區分大小寫。

注冊的剪貼板格式由 0xC000 到 0xFFFF 范圍內的值標識。

從這一點以及每個會話只有一個剪貼板這一事實,您應該能夠推斷出格式 ID 在給定會話中是常見的。

至於如何生成 ID,我無法回答該部分,因為我無法訪問該代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM