在用.net做pocket pc的程序中,我们经常想使用系统的Imagelist来和listview或treeview捆绑来显示不同后缀文件的不同图标。 但在.net编程中对系统的Imagelist的读取比较麻烦,我们需要借助api来实现。主要的办法是P/Invoke.
但在.net编程中对系统的Imagelist的读取比较麻烦,我们需要借助api来实现。主要的办法是P/Invoke.
用sendMessag来改变listview或treeview的IMagelist.
用sendMessag来改变listview或treeview的IMagelist.
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);
}
用EVC对“SHgetfileinfo”API函数进行包装,代码如下:
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;
}
在需要使用系统imagelist的地方作如下的调用:
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;
窗体实现分割功能,主要是处理鼠标的down, move, up实践来完成不同部分的分割,这个对于pocket pc这样的屏幕限制系统来说相当有趣。
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) ) );
}
}
单击listview中的条目启动相应的程序来读其中的内容:
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;
}
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);
}
}