📄 shelllink.cs
字号:
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using APLib.Native;
namespace APLib.Shell
{
public class ShellLink
{
#region HotkeyObject
public class HotkeyObject
{
Hotkey hkey;
VirtualKeys vkey;
public Hotkey HKey { get { return hkey; } }
public VirtualKeys VKey { get { return vkey; } }
public HotkeyObject(ushort key)
{
Parse(key);
}
public void Parse(ushort key)
{
if (key == 0)
{
hkey = Hotkey.HOTKEYF_NONE;
vkey = VirtualKeys.VK_NONE;
}
else
{
try
{
hkey = (Hotkey)(key >> 8);
}
catch
{
hkey = Hotkey.HOTKEYF_NONE;
}
try
{
vkey = (VirtualKeys)(key & 0xff);
}
catch
{
vkey = VirtualKeys.VK_NONE;
}
}
}
}
#endregion
protected IShellLink axShellLink;
protected IPersistFile axPersistFile;
[ComImport(), Guid("00021401-0000-0000-C000-000000000046")]
class AxShellLink
{
}
private string iconLocation;
private int iconIndex;
public string IconLocation { get { return iconLocation; } }
public int IconIndex { get { return iconIndex; } }
public ShellLink()
{
AxShellLink shellLink = new AxShellLink();
axShellLink = (IShellLink)shellLink;
axPersistFile = (IPersistFile)shellLink;
}
#region IShellLink
/// <summary>
/// Retrieves the command-line arguments associated with a Shell link object.
/// </summary>
/// <returns>command-line arguments</returns>
public string GetArguments()
{
StringBuilder sb = new StringBuilder(Win32.MAX_PATH);
axShellLink.GetArguments(sb, Win32.MAX_PATH);
return sb.ToString();
}
/// <summary>
/// Retrieves the description string for a Shell link object.
/// </summary>
/// <returns>description</returns>
public string GetDescription()
{
StringBuilder sb = new StringBuilder(Win32.MAX_PATH);
axShellLink.GetDescription(sb, Win32.MAX_PATH);
return sb.ToString();
}
/// <summary>
/// Retrieves the hot key for a Shell link object.
/// </summary>
/// <returns>hotkey</returns>
public HotkeyObject GetHotkey()
{
short key;
axShellLink.GetHotkey(out key);
return new HotkeyObject((ushort)key);
}
/// <summary>
/// Retrieves the location (path and index) of the icon for a Shell link object.
/// check IconLocation and IconIndex
/// </summary>
/// <returns>icon file path</returns>
public string GetIconLocation()
{
StringBuilder pszIconPath = new StringBuilder(Win32.MAX_PATH);
int piIcon;
axShellLink.GetIconLocation(pszIconPath, Win32.MAX_PATH, out piIcon);
iconLocation = pszIconPath.ToString();
iconIndex = piIcon;
return iconLocation;
}
/// <summary>
/// Retrieves the list of item identifiers for a Shell link object.
/// </summary>
/// <returns>list of item identifiers</returns>
public IntPtr GetIDList()
{
IntPtr ppidl;
axShellLink.GetIDList(out ppidl);
return ppidl;
}
/// <summary>
/// Retrieves the path and file name of a Shell link object.
/// </summary>
/// <param name="fFlags">Flags that specify the type of path information to retrieve.</param>
/// <returns>file path of Shell link object</returns>
public string GetPath(SLGP_FLAGS fFlags)
{
StringBuilder pszFile = new StringBuilder(Win32.MAX_PATH);
WIN32_FIND_DATA pfd;
axShellLink.GetPath(pszFile, Win32.MAX_PATH, out pfd, fFlags);
return pszFile.ToString();
}
/// <summary>
/// Retrieves the extra information of file of a Shell link object.
/// </summary>
/// <param name="fFlags">Flags that specify the type of path information to retrieve.</param>
/// <returns>extra information of file</returns>
public WIN32_FIND_DATA GetPathEx(SLGP_FLAGS fFlags)
{
StringBuilder pszFile = new StringBuilder(Win32.MAX_PATH);
WIN32_FIND_DATA pfd;
axShellLink.GetPath(pszFile, Win32.MAX_PATH, out pfd, fFlags);
return pfd;
}
/// <summary>
/// Retrieves the show command for a Shell link object.
/// </summary>
/// <returns>show command</returns>
public ShowWindowCommand GetShowCmd()
{
int piShowCmd;
axShellLink.GetShowCmd(out piShowCmd);
try
{
return (ShowWindowCommand)piShowCmd;
}
catch
{
}
return ShowWindowCommand.SW_NORMAL;
}
/// <summary>
/// Retrieves the name of the working directory for a Shell link object.
/// </summary>
/// <returns>working directory</returns>
public string GetWorkingDirectory()
{
StringBuilder pszDir = new StringBuilder(Win32.MAX_PATH);
axShellLink.GetWorkingDirectory(pszDir, Win32.MAX_PATH);
return pszDir.ToString();
}
/// <summary>
/// Attempts to find the target of a Shell link, even if it has been moved or renamed.
/// </summary>
/// <param name="hwnd">main window hwnd</param>
/// <param name="fFlags">Action flags</param>
public void Resolve(IntPtr hwnd, SLR_FLAGS fFlags)
{
axShellLink.Resolve(hwnd, fFlags);
}
/// <summary>
/// Sets the command-line arguments for a Shell link object.
/// </summary>
/// <param name="pszArgs">arguments</param>
public void SetArguments(string pszArgs)
{
axShellLink.SetArguments(pszArgs);
}
/// <summary>
/// Sets the description for a Shell link object. The description can be any application-defined string.
/// </summary>
/// <param name="pszName">a buffer containing the new description string.</param>
public void SetDescription(string pszName)
{
axShellLink.SetDescription(pszName);
}
/// <summary>
/// Sets a hot key for a Shell link object.
/// </summary>
/// <param name="hotkey">new hot key</param>
public void SetHotkey(HotkeyObject hotkey)
{
short key = (short)(((uint)hotkey.HKey << 8) | ((uint)hotkey.VKey));
axShellLink.SetHotkey(key);
}
/// <summary>
/// Sets the location (path and index) of the icon for a Shell link object.
/// </summary>
/// <param name="pszIconPath">a buffer to contain the path of the file containing the icon.</param>
/// <param name="iIcon">index of the icon</param>
public void SetIconLocation(string pszIconPath, int iIcon)
{
axShellLink.SetIconLocation(pszIconPath, iIcon);
}
/// <summary>
/// Sets the pointer to an item identifier list (PIDL) for a Shell link object.
/// </summary>
/// <param name="pidl">The object's fully-qualified PIDL.</param>
public void SetIDList(IntPtr pidl)
{
axShellLink.SetIDList(pidl);
}
/// <summary>
/// Sets the path and file name of a Shell link object.
/// </summary>
/// <param name="pszFile">a buffer that contains the new path</param>
public void SetPath(string pszFile)
{
axShellLink.SetPath(pszFile);
}
/// <summary>
/// Sets the relative path to the Shell link object.
/// </summary>
/// <param name="pszPathRel">a buffer that contains the new relative path</param>
public void SetRelativePath(string pszPathRel)
{
axShellLink.SetRelativePath(pszPathRel, 0);
}
/// <summary>
/// Sets the show command for a Shell link object. The show command sets the initial show state of the window.
/// </summary>
/// <param name="iShowCmd">command</param>
public void SetShowCmd(ShowWindowCommand iShowCmd)
{
axShellLink.SetShowCmd((int)iShowCmd);
}
/// <summary>
/// Sets the name of the working directory for a Shell link object.
/// </summary>
/// <param name="pszDir">a buffer that contains the name of the new working directory</param>
public void SetWorkingDirectory(string pszDir)
{
axShellLink.SetWorkingDirectory(pszDir);
}
#endregion
#region IPersistFile
/// <summary>
/// Retrieves either the absolute path to the object's current working file or, if there is no current working file, the object's default filename prompt.
/// </summary>
/// <returns>path to the object's current working file</returns>
public string GetCurFile()
{
StringBuilder pszFileName = new StringBuilder(Win32.MAX_PATH);
axPersistFile.GetCurFile(pszFileName);
return pszFileName.ToString();
}
/// <summary>
/// Checks an object for changes since it was last saved to its current file.
/// </summary>
/// <returns>whether it was last saved</returns>
public bool IsDirty()
{
return axPersistFile.IsDirty() == Win32.S_OK;
}
/// <summary>
/// Opens the specified file and initializes an object from the file contents.
/// </summary>
/// <param name="pszFileName">string containing the absolute path of the file to open.</param>
/// <param name="dwMode">Specifies some combination of the values from the STGM enumeration to indicate the access mode to use when opening the file.</param>
public void Load(string pszFileName, STGM dwMode)
{
axPersistFile.Load(pszFileName, (uint)dwMode);
}
/// <summary>
/// Saves a copy of the object into the specified file.
/// </summary>
/// <param name="pszFileName">string containing the absolute path of the file to which the object should be saved.</param>
/// <param name="fRemember">Indicates whether the pszFileName parameter is to be used as the current working file.</param>
public void Save(string pszFileName, bool fRemember)
{
axPersistFile.Save(pszFileName, fRemember);
}
/// <summary>
/// Notifies the object that it can write to its file.
/// </summary>
/// <param name="pszFileName">the absolute path of the file where the object was previously saved</param>
public void SaveCompleted(string pszFileName)
{
axPersistFile.SaveCompleted(pszFileName);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -