C# 应用程序拖拽添加其他程序快捷图标,并能启动
想写一个C# winform程序,希望实现拖拽添加其他程序快捷图标到我写的程序的面板上,并能启动,希望指教.
------解决方案--------------------
帮顶
可以用api,下面的例子可以参考。
using System;
using System.Runtime.InteropServices;
namespace MediaClient
{
public class Win32
{
[DllImport("shell32.dll", EntryPoint = "ExtractIcon")]
public static extern int ExtractIcon(IntPtr hInst,string lpFileName,int nIndex);
[DllImport("shell32.dll", EntryPoint = "SHGetFileInfo")]
public static extern IntPtr SHGetFileInfo(string pszPath,uint dwFileAttribute,ref SHFILEINFO psfi,uint cbSizeFileInfo,uint Flags);
[DllImport("User32.dll", EntryPoint = "DestroyIcon")]
public static extern int DestroyIcon(IntPtr hIcon);
[StructLayout(LayoutKind.Sequential)]
public struct SHFILEINFO
{
public IntPtr hIcon;
public IntPtr iIcon;
public uint dwAttributes;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public string szDisplayName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 80)]
public string szTypeName;
};
}
}
调用方法:
/**//// <summary>
/// 获得指定路径下面的所有文件及文件夹
/// </summary>
/// <param name="path">路径</param>
/// <returns></returns>
public ArrayList GetListViewItem(string path,ImageList imglist)
{
Win32.SHFILEINFO shfi = new Win32.SHFILEINFO();
try
{
string[] dirs = Directory.GetDirectories(path);
string[] files = Directory.GetFiles(path);
ArrayList itemarr = new ArrayList();
for (int i = 0; i < dirs.Length; i++)
{
string[] info = new string[4];
DirectoryInfo dir = new DirectoryInfo(dirs[i]);
//获得图标
Win32.SHGetFileInfo(dirs[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(dir.Name, (Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = dir.Name;
info[1] = "";
info[2] = "文件夹";
info[3] = dir.LastWriteTime.ToString();
ListViewItem item = new ListViewItem(info,dir.Name);
itemarr.Add(item);
//销毁图标
Win32.DestroyIcon(shfi.hIcon);
}
for (int i = 0; i < files.Length; i++)
{
string[] info = new string[4];
FileInfo fi = new FileInfo(files[i]);
//获得图标
Win32.SHGetFileInfo(files[i],
(uint)0x80,
ref shfi,
(uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
(uint)(0x100 | 0x400)); //取得Icon和TypeName
//添加图标
imglist.Images.Add(fi.Name,(Icon)Icon.FromHandle(shfi.hIcon).Clone());
info[0] = fi.Name;<