(本系列文章由檸檬的(lc_mtt)原創,轉載請註明出處,謝謝~)
接上一節:(C#)Windows Shell 外殼編程系列8 - 同尾碼名不同表徵圖?
QueryInfo擴充
活動案頭引入一項新特性,當你在某些特定對象上旋停滑鼠時,工具提示將顯示它們的描述。我們可以使用 QueryInfo 擴充為Shell中的其它對象提供自訂的工具提示。如:
事實上,這個功能實現比前兩個 Shell 擴充更簡單,它僅僅是實現 IQueryInfo 介面:
IQueryInfo 介面定義
[ComImport(), ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIUnknown), GuidAttribute("00021500-0000-0000-c000-000000000046")]
public interface IQueryInfo
{
[PreserveSig]
uint GetInfoTip(uint dwFlags, out IntPtr pszInfoTip);
[PreserveSig]
uint GetInfoFlags(out uint dwFlags);
}
IQueryInfo 介面只包含兩個函數,其中 GetInfoFlags 目前還不被支援並且必須返回 0。
GetInfoTip() 讓我們返回工具提示文本 字串。其參數:
dwFlags 當前並不被使用。
pszInfoTip 是個Unicode 字串指標變數的指標,我們要將其賦值為我們所分配的字串緩衝區的指標。
還記得之前使用 IPersistFile 介面擷取單個檔案路徑儲存在 szFileName 變數中嗎?現在也可以使用:
GetInfoTip
public uint GetInfoTip(uint dwFlags, out IntPtr pszInfoTip)
{
StreamReader sr = new StreamReader(szFileName, Encoding.GetEncoding("gb2312"));
string text = sr.ReadToEnd();
sr.Close();
if (text.Length > 256)
{
text = text.Substring(0, 256) + "";
}
string tip = "------------- 內容預覽 -------------\r\n\r\n" + text;
pszInfoTip = Marshal.StringToCoTaskMemUni(tip);
return S_OK;
}
代碼:http://files.cnblogs.com/lemony/MyContextMenu.rar
這一章似乎過於簡單。下一章將簡述如何使用 IShellPropSheetExt 介面為檔案/檔案夾增加屬性頁面,如下: