2017-01-21 43 views
1

我試過如下:DataFormats.GetFormat是否允許我創建一個新的私有格式?

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

,並返回binaryData.Id是50151.

我可以假設「BinaryData」是嚴格保密的名稱給我,或者它是一個衆所周知的名字嗎?

我問,因爲有一個第三方應用程序,我正在連接到(colaSoft),它將BinaryDataId同樣也推送到剪貼板格式也是50151.這僅僅是巧合嗎? Id如何確定?

回答

1

從文檔爲DataFormats.GetDataFormat Method

這種方法也可以被用於註冊新的格式。如果指定的格式無法找到,它會自動註冊爲新的數據格式。

這不回答你的問題的一部分:

我可以假設「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 function文檔

現在:

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

註冊的剪貼板格式通過 0xC000到0xFFFF範圍內的值進行標識。

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

至於如何生成ID,我不能回答該部分作爲 我沒有訪問該代碼。

相關問題