65.9K
CodeProject 正在变化。 阅读更多。
Home

在 Pocket PC 中使用 .NET Compact Framework 实现资源管理器

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.82/5 (7投票s)

2005 年 3 月 15 日

CPOL
viewsIcon

63616

downloadIcon

470

在 Pocket PC 中使用 .NET Compact Framework 实现资源管理器。

Sample Image - UseSystemimagelist.jpg

引言

有时我们希望在 ListViewTreeView 中使用系统 ImageList,就像资源管理器一样,它可以显示系统 ImageList。我们可以使用 SHgetfileinfo API 获取系统 ImageList 的句柄以及特定文件应使用的索引。

使用系统 ImageList

在这个主题中,我使用“SendMessage”API 将系统图像绑定到 TreeViewListView

public static void SetListViewImageList(
            IntPtr hwnd,ListView listView,
            SysImageList sysImageList,bool forStateImages)
{
  IntPtr wParam = (IntPtr)LVSIL_NORMAL;
  if (sysImageList.ImageListSize == SysImageListSize.smallIcons)
  {
     wParam = (IntPtr)LVSIL_SMALL;
  }
  if (forStateImages)
  {
     wParam = (IntPtr)LVSIL_STATE;
  }
  SendMessage(hwnd,LVM_SETIMAGELIST,wParam,sysImageList.Handle);
}
public static void SetTreeViewImageList(IntPtr hwnd,
            TreeView treeView, SysImageList sysImageList,
            bool forStateImages )
{   
    IntPtr wParam = (IntPtr)TVSIL_NORMAL;
    if (forStateImages)
    {
        wParam = (IntPtr)TVSIL_STATE;
    }
    SendMessage(hwnd, TVM_SETIMAGELIST,
                   wParam,  sysImageList.Handle);
}

封装 SHGetFileInfo 例程

在 .NET 中封装“SHGetFileInfo”非常困难。我使用了 eVC 来帮助封装它。

extern "C" __declspec (dllexport) DWORD SHABgetfileinfo(LPCTSTR pszFileName)
{
 SHFILEINFO ssfi;
 return SHGetFileInfo(pszFileName, 0, &ssfi, sizeof(SHFILEINFO), 
      SHGFI_USEFILEATTRIBUTES | SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
}
extern "C" __declspec (dllexport) int SHIndexfileinfo(LPCTSTR pszFileName)
{
    SHFILEINFO ssfi;
    SHGetFileInfo(pszFileName, 0, &ssfi, sizeof(SHFILEINFO),
    SHGFI_SYSICONINDEX | SHGFI_SMALLICON);
    return ssfi.iIcon;
}
extern "C" __declspec (dllexport) int SHIndexfileinfoEx(
             LPCTSTR pszFileName,DWORD dwAttr, UINT dwFlag)
{
     SHFILEINFO ssfi;
     SHGetFileInfo(pszFileName, 0, &ssfi, sizeof(SHFILEINFO),dwFlag);
     return ssfi.iIcon;
}

在窗体中调用例程

 sysilsSmall = new SysImageList(SysImageListSize.smallIcons);
 this.tvFolders.Capture = true;
 IntPtr hwnd1 = GetCapture();
 this.tvFolders.Capture = false;
 this.lvFiles.Capture = true;
 IntPtr hwnd2 = GetCapture();
 SysImageListHelper.SetTreeViewImageList(hwnd1,tvFolders,
              sysilsSmall,false);
 SysImageListHelper.SetListViewImageList(hwnd2,
                  lvFiles,sysilsSmall,false);
 this.lvFiles.Capture = false;

按文件名、大小、创建日期等排序

我使用一个 PictureBox 来分割窗口,我处理 MouseDownMouseMoveMouseUp 事件。当触控笔触碰屏幕时,MouseDown 记录 PictureBox 的位置并显示它。当我们移动它时,MouseMove 事件会更改 PictureBox 的位置,当触控笔抬起时,MouseUp 事件停止并固定 PictureBox

private void FileExplorer_MouseDown(object sender, 
                        System.Windows.Forms.MouseEventArgs e)
{
    this.picSplitter.Top=e.Y-4;
    this.picSplitter.Visible=true;
    this.Moving=true;
}
private void FileExplorer_MouseMove(object sender, 
                        System.Windows.Forms.MouseEventArgs e)
{
    if(this.Moving)
    {
        this.picSplitter.Top=e.Y;
    }
}
 
private void FileExplorer_MouseUp(object sender, 
                         System.Windows.Forms.MouseEventArgs e)
{
    this.tvFolders.Height=e.Y-5;
    this.lvFiles.Top=this.tvFolders.Top+this.tvFolders.Height+10;
    this.lvFiles.Height=this.Height-this.lvFiles.Top;
    this.picSplitter.Visible=false;
    this.Moving=false;
    this.ModeChange=false;
}

按文件名、大小、创建日期等排序

private void Sort(ref ArrayList AL,string SortBy,bool Asc)
{
  IComparer myComparer=null;
  switch(SortBy)
  {
    case "Name":
      if(Asc)
        myComparer=new SortNameAsc();
      else
        myComparer=new SortNameDesc();
        break;
    case "Size":
      if(Asc)
        myComparer=new SortSizeAsc();
      else
        myComparer=new SortSizeDesc();
        break;
    case "Type":
      if(Asc)
        myComparer=new SortTypeAsc();
      else
        myComparer=new SortTypeDesc();
        break;
    case "Modified":
      if(Asc)
        myComparer=new SortModifiedAsc();
      else
        myComparer=new SortModifiedDesc();
        break;
  }
  AL.Sort(myComparer);
}
public class SortNameDesc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
  { 
    return( (new 
      CaseInsensitiveComparer()).Compare((object)((sFile)y).Name, 
      (object)((sFile)x).Name ) );
  }
}

public class SortNameAsc : IComparer
{
  int IComparer.Compare( Object x, Object y )  
  {
    return( (new 
      CaseInsensitiveComparer()).Compare((object)((sFile)x).Name,
      (object)((sFile)y).Name ) );
  }
 
}
public class SortFullNmeAsc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
  {
    return( (new 
       CaseInsensitiveComparer()).Compare((object)((sFile)x).FullName, 
       (object)((sFile)y).FullName ) );
  }
}
public class SortFullNameDesc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
  {
    return( (new 
       CaseInsensitiveComparer()).Compare( (object)((sFile)y).FullName, 
       (object)((sFile)x).FullName ) );
  }
}
public class SortTypeDesc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
  {
    return( (new 
      CaseInsensitiveComparer()).Compare( (object)((sFile)y).Type, 
      (object)((sFile)x).Type ) );
  }
}
public class SortTypeAsc : IComparer  
  {
    int IComparer.Compare( Object x, Object y )  
    {
      return( (new 
      CaseInsensitiveComparer()).Compare( (object)((sFile)x).Type, 
      (object)((sFile)y).Type ) );
    }
}
public class SortSizeAsc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
  {
    return( int.Parse(((sFile)x).Size)-int.Parse(((sFile)y).Size) ) ;
  }
}
public class SortSizeDesc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
  {
    return( int.Parse(((sFile)y).Size)-int.Parse(((sFile)x).Size) ) ;
  }
}
public class SortModifiedAsc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
  {
    return( DateTime.Compare( DateTime.Parse(((sFile)x).Modified),
                              DateTime.Parse(((sFile)y).Modified) ) );
  }
}
public class SortModifiedDesc : IComparer  
{
  int IComparer.Compare( Object x, Object y )  
    {
    return( DateTime.Compare( DateTime.Parse(((sFile)y).Modified),
                              DateTime.Parse(((sFile)x).Modified) ) );
    }
}

Shellex 其他程序

[DllImport("coredll.dll")]
public static extern IntPtr GetCapture();
[DllImport("coredll", EntryPoint="ShellExecuteEx", SetLastError=true)]
private extern static bool ShellExecuteEx( ShellExecuteInfo ex );
private void lvFiles_SelectedIndexChanged(object sender, System.EventArgs e)
{
  if(this.lvFiles.SelectedIndices.Count>0)
  {
    string FullPath=this.lvFiles.Items[this.lvFiles.SelectedIndices[0]].Text;
    ShellExecuteInfo sei=new ShellExecuteInfo();
    GCHandle hfile = GCHandle.Alloc((FullPath + '\0').ToCharArray(), 
    GCHandleType.Pinned);
    sei.lpFile = (IntPtr)((int)hfile.AddrOfPinnedObject() + 4);
    //windowstyle
    sei.nShow = 1;
    GCHandle hverb = new GCHandle();
    hverb = GCHandle.Alloc(("Open"+'\0').ToCharArray(), GCHandleType.Pinned);
    sei.lpVerb = (IntPtr)((int)hverb.AddrOfPinnedObject() + 4);
    bool ret=ShellExecuteEx(sei);
  }   
sealed class ShellExecuteInfo
 {
  public UInt32 cbSize  = 60; 
  public UInt32 fMask   = 0x00000040; 
  public IntPtr hwnd   = IntPtr.Zero; 
  public IntPtr lpVerb  = IntPtr.Zero; 
  public IntPtr lpFile  = IntPtr.Zero; 
  public IntPtr lpParameters = IntPtr.Zero; 
  public IntPtr lpDirectory = IntPtr.Zero; 
  public int nShow   = 0; 
  public IntPtr hInstApp  = IntPtr.Zero; 
  public IntPtr lpIDList  = IntPtr.Zero; 
  public IntPtr lpClass  = IntPtr.Zero; 
  public IntPtr hkeyClass  = IntPtr.Zero; 
  public UInt32 dwHotKey  = 0; 
  public IntPtr hIcon   = IntPtr.Zero; 
  public IntPtr hProcess  = IntPtr.Zero; 
 }
© . All rights reserved.