📄 peerfilelistview.cpp
字号:
// PeerFileListView.cpp : implementation file
//
#include "stdafx.h"
#include "peeryouc.h"
#include "PeerFileListView.h"
#include "Createdirdlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPeerFileListView
IMPLEMENT_DYNCREATE(CPeerFileListView, CView)
CPeerFileListView::CPeerFileListView()
{
}
CPeerFileListView::~CPeerFileListView()
{
}
BEGIN_MESSAGE_MAP(CPeerFileListView, CView)
//{{AFX_MSG_MAP(CPeerFileListView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_COMMAND(ID_PEER_FILE_LOCAL_REPORT, OnPeerFileLocalReport)
ON_COMMAND(ID_PEER_FILE_LOCAL_LIST, OnPeerFileLocalList)
ON_COMMAND(ID_PEER_FILE_LOCAL_BIG, OnPeerFileLocalBig)
ON_COMMAND(ID_PEER_FILE_LOCAL_SMALL, OnPeerFileLocalSmall)
ON_CBN_SELCHANGE(ID_PEER_FILE_LOCAL_DROPLIST,OnSelectChange)
ON_NOTIFY(NM_DBLCLK,1000, OnLocalLD )
ON_NOTIFY(NM_RCLICK,1000, OnLocalRC )
ON_NOTIFY(LVN_BEGINDRAG,1000, OnBeginDrag )
ON_COMMAND(IDR_FILE_LOCAL_CREATEDIR, OnFileLocalCreatedir)
ON_COMMAND(IDR_FILE_LOCAL_DELFILE, OnFileLocalDelfile)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPeerFileListView drawing
void CPeerFileListView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
/////////////////////////////////////////////////////////////////////////////
// CPeerFileListView diagnostics
#ifdef _DEBUG
void CPeerFileListView::AssertValid() const
{
CView::AssertValid();
}
void CPeerFileListView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CPeerFileListView message handlers
int CPeerFileListView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
if(!list.Create( WS_VISIBLE|WS_CHILD,CRect(0,0,0,0),this,1000))
return -1;
list.ModifyStyle(0,LVS_REPORT);
style=LVS_REPORT;
LVCOLUMN colinfo;
memset(&colinfo,0,sizeof(colinfo));
colinfo.mask=LVCF_TEXT|LVCF_WIDTH;
colinfo.cx=100;
colinfo.cchTextMax=255;
colinfo.pszText="名称";
colinfo.iSubItem=0;
colinfo.iOrder=0;
list.InsertColumn(0,&colinfo);
colinfo.pszText="类型";
colinfo.iSubItem=1;
colinfo.iOrder=1;
list.InsertColumn(1,&colinfo);
colinfo.pszText="大小";
colinfo.iSubItem=2;
colinfo.iOrder=2;
list.InsertColumn(2,&colinfo);
colinfo.pszText="修改时间";
colinfo.iSubItem=3;
colinfo.iOrder=3;
list.InsertColumn(3,&colinfo);
imagelists.Create(16,16,ILC_MASK,16,1);
imagelistb.Create(32,32,ILC_MASK,16,1);
imagelists.Add(AfxGetApp()->LoadIcon(IDI_DIR));
imagelistb.Add(AfxGetApp()->LoadIcon(IDI_DIR));
list.SetImageList(&imagelists,LVSIL_SMALL);
list.SetImageList(&imagelistb,LVSIL_NORMAL);
list.Register();
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
|CBRS_TOOLTIPS | CBRS_FLYBY /*| CBRS_SIZE_DYNAMIC*/) ||
!m_wndToolBar.LoadToolBar(IDR_PEER_FILE_LOCAL))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
AddToolBarButton();
if(!image.Create(16,16,ILC_MASK,4,1))
return -1;
image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_A));
image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_FIX));
image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_CD));
image.Add(AfxGetApp()->LoadIcon(IDI_DRIVER_NET));
m_wndToolBar.m_driver.SetImageList(&image);
return 0;
}
void CPeerFileListView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
m_wndToolBar.MoveWindow(0,0,cx,25);
list.MoveWindow(0,25,cx,cy-25);
// TODO: Add your message handler code here
}
void CPeerFileListView::AddToolBarButton()
{
#define COMBO_BOX_WIDTH 80
int index = 0;
CRect rect;
while(m_wndToolBar.GetItemID(index)!= ID_PEER_FILE_LOCAL_STATIC)
index++;
m_wndToolBar.SetButtonInfo(index,
ID_PEER_FILE_LOCAL_STATIC,TBBS_SEPARATOR,110);
m_wndToolBar.GetItemRect(index, &rect);
rect.top+=4;
// then .Create the combo box and show it
if (!m_wndToolBar.m_title.Create( "Local Machine",
WS_CHILD|WS_VISIBLE ,
rect, &m_wndToolBar, ID_PEER_FILE_LOCAL_STATIC))
{
TRACE0("Failed to create combo-box\n");
return ;
}
index=0;
while(m_wndToolBar.GetItemID(index)!= ID_PEER_FILE_LOCAL_DROPLIST)
index++;
m_wndToolBar.SetButtonInfo(index,
ID_PEER_FILE_LOCAL_DROPLIST,TBBS_SEPARATOR,COMBO_BOX_WIDTH);
m_wndToolBar.GetItemRect(index, &rect);
rect.top+=2;
rect.bottom += 200;
// then .Create the combo box and show it
if (!m_wndToolBar.m_driver.Create(
WS_CHILD|WS_VISIBLE | CBS_AUTOHSCROLL |
CBS_DROPDOWNLIST ,
rect, &m_wndToolBar, ID_PEER_FILE_LOCAL_DROPLIST))
{
TRACE0("Failed to create combo-box\n");
return ;
}
m_wndToolBar.m_driver.ShowWindow(SW_SHOW);
m_wndToolBar.m_title.ShowWindow(SW_SHOW);
}
void CPeerFileListView::OnPeerFileLocalReport()
{
// TODO: Add your command handler code here
list.ModifyStyle(style,LVS_REPORT);
style=LVS_REPORT;
}
void CPeerFileListView::OnPeerFileLocalList()
{
// TODO: Add your command handler code here
list.ModifyStyle(style,LVS_LIST);
style=LVS_LIST;
}
void CPeerFileListView::OnPeerFileLocalBig()
{
// TODO: Add your command handler code here
list.ModifyStyle(style,LVS_ICON );
style=LVS_ICON;
}
void CPeerFileListView::OnPeerFileLocalSmall()
{
// TODO: Add your command handler code here
list.ModifyStyle(style,LVS_SMALLICON );
style=LVS_SMALLICON ;
}
void CPeerFileListView::OnSelectChange()
{
m_wndToolBar.m_driver.GetLBText(m_wndToolBar.m_driver.GetCurSel(),path);
AfxGetApp()->DoWaitCursor(1);
Retrive();
AfxGetApp()->DoWaitCursor(0);
}
void CPeerFileListView::Retrive()
{
list.DeleteAllItems();
int index=0;
////本地//////
HANDLE hlocalfile;
WIN32_FIND_DATA findd;
CString nowpath;
nowpath=path+"\\*";
memset(&findd,0,sizeof(findd));
int imagecount;
imagecount=imagelists.GetImageCount();
for(int j=1;j<imagecount;j++)
{
imagelists.Remove(j);
imagelistb.Remove(j);
}
CImageList* pSmallImage,* pBigImage;
pSmallImage=list.GetImageList(LVSIL_SMALL);
pBigImage=list.GetImageList(LVSIL_NORMAL);
if((hlocalfile=FindFirstFile( nowpath,&findd))==INVALID_HANDLE_VALUE)
return ;
SHFILEINFO shfi;
memset(&shfi,0,sizeof(shfi));
SHGetFileInfo(findd.cFileName,
FILE_ATTRIBUTE_NORMAL,
&shfi, sizeof(shfi),
SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_TYPENAME );
int indeximage;
indeximage=pSmallImage->Add(shfi.hIcon);
pBigImage->Add(shfi.hIcon);
index=list.GetItemCount();
list.InsertItem(index,".",0);
list.SetItem(index,1,LVIF_TEXT,"文件夹",0,0,0,NULL);
index=list.GetItemCount();
list.InsertItem(index,"..",0);
list.SetItem(index,1,LVIF_TEXT,"文件夹",0,0,0,NULL);
index=list.GetItemCount();
DWORD len;
char slen[30];
CTime time;
CString stime;
if(strcmp(findd.cFileName,".")!=0&&
strcmp(findd.cFileName,"..")!=0)
{
list.InsertItem(index,findd.cFileName,
findd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY?0:indeximage);
list.SetItem(index,1,LVIF_TEXT,findd.dwFileAttributes&
FILE_ATTRIBUTE_DIRECTORY?"文件夹":shfi.szTypeName,0,0,0,NULL);
len=(findd.nFileSizeHigh*MAXDWORD+findd.nFileSizeLow)/1024;
sprintf(slen,"%dK",len);
list.SetItem(index,2,LVIF_TEXT,findd.dwFileAttributes&
FILE_ATTRIBUTE_DIRECTORY?"":slen,0,0,0,NULL);
time=findd.ftLastWriteTime;
stime=time.Format("%Y-%m-%d %H:%M:%S");
list.SetItem(index,3,LVIF_TEXT,stime,0,0,0,NULL);
}
while(hlocalfile)
{
if(FindNextFile(hlocalfile,&findd))
{
memset(&shfi,0,sizeof(shfi));
SHGetFileInfo(findd.cFileName,
FILE_ATTRIBUTE_NORMAL,
&shfi, sizeof(shfi),
SHGFI_ICON|SHGFI_USEFILEATTRIBUTES|SHGFI_TYPENAME );
indeximage=pSmallImage->Add(shfi.hIcon);
pBigImage->Add(shfi.hIcon);
index=list.GetItemCount();
if(strcmp(findd.cFileName,".")!=0&&
strcmp(findd.cFileName,"..")!=0)
{
list.InsertItem(index,findd.cFileName,
findd.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY?0:indeximage);
list.SetItem(index,1,LVIF_TEXT,findd.dwFileAttributes&
FILE_ATTRIBUTE_DIRECTORY?"文件夹":shfi.szTypeName,0,0,0,NULL);
len=(findd.nFileSizeHigh*MAXDWORD+findd.nFileSizeLow)/1024;
sprintf(slen,"%dK",len);
list.SetItem(index,2,LVIF_TEXT,findd.dwFileAttributes&
FILE_ATTRIBUTE_DIRECTORY?"":slen,0,0,0,NULL);
time=findd.ftLastWriteTime;
stime=time.Format( "%Y-%m-%d %H:%M:%S");
list.SetItem(index,3,LVIF_TEXT,stime,0,0,0,NULL);
}
}
else
{
if(GetLastError()==ERROR_NO_MORE_FILES)
break;
}
}
FindClose(hlocalfile);
}
void CPeerFileListView::OnLocalLD(NMHDR* pNotifyStruct,LRESULT* result)
{
if(list.GetItemCount()<=0)
return;
LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)pNotifyStruct;
if(lpnmlv->iItem>=0)
{
CString filename,type;
filename=list.GetItemText(lpnmlv->iItem,0);
type=list.GetItemText(lpnmlv->iItem,1);
if(type=="文件夹")
{
if(filename==".")
goto there;
else if(filename=="..")
{
int findc=path.ReverseFind('\\');
if(findc==-1)
goto there;
path=path.Left(findc);
}
else
{
path=path+"\\"+filename;
}
AfxGetApp()->DoWaitCursor(1);
Retrive();
AfxGetApp()->DoWaitCursor(0);
}
else
{
ShellExecute(NULL, _T("open"), path+"\\"+filename, NULL,NULL, SW_SHOWNORMAL);
}
}
there:
*result=0;
}
void CPeerFileListView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
}
void CPeerFileListView::OnBeginDrag(NMHDR* pNotifyStruct,LRESULT* result)
{
if(list.GetItemCount()<=2)
return;
//确信要拖动,而不是随便点击
//按下左键250ms则认为要拖动
Sleep(250);
MSG msg;
::PeekMessage(
&msg,
GetSafeHwnd(),
WM_LBUTTONUP,
WM_LBUTTONUP,
PM_NOREMOVE
);
//随意点击而已,返回
if( msg.message==WM_LBUTTONUP )
return;
DRAG Contens;
Contens.flag=0;
Contens.wnd=(DWORD)this;
HGLOBAL hData = GlobalAlloc(GHND|GMEM_SHARE,sizeof(DRAG) );
char *pS = (char *)GlobalLock( hData );
memcpy( pS, (char*)&Contens,sizeof(DRAG));
GlobalUnlock( hData );
list.m_bDraging = TRUE; //开始拖动
COleDataSource source;
source.CacheGlobalData( RegisterClipboardFormat("ListCtrl")/*CF_TEXT*/, hData );
source.DoDragDrop( DROPEFFECT_COPY );
GlobalFree( hData );
list.m_bDraging = FALSE; //完成拖动
*result=0;
}
void CPeerFileListView::OnLocalRC(NMHDR* pNotifyStruct,LRESULT* result)
{
if(list.GetItemCount()<=0)
return;
LPNMLISTVIEW lpnmlv = (LPNMLISTVIEW)pNotifyStruct;
CPoint point;
::GetCursorPos(&point);
// ScreenToClient(&point);
CMenu menu;
VERIFY(menu.LoadMenu(IDR_FILE_LOCAL));
CMenu* pPopup = menu.GetSubMenu(0);
ASSERT(pPopup != NULL);
if(lpnmlv->iItem<0)
pPopup->EnableMenuItem(IDR_FILE_LOCAL_DELFILE,MF_GRAYED);
CWnd* pWndPopupOwner = this;
while (pWndPopupOwner->GetStyle() & WS_CHILD)
pWndPopupOwner = pWndPopupOwner->GetParent();
pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
pWndPopupOwner);
}
void CPeerFileListView::OnFileLocalCreatedir()
{
// TODO: Add your command handler code here
CCreateDirDlg dir(1,this);
dir.DoModal();
}
void CPeerFileListView::OnFileLocalDelfile()
{
if(MessageBox( "确定要删除吗?","提示",MB_YESNO)!=IDYES)
return;
// TODO: Add your command handler code here
POSITION pos = list.GetFirstSelectedItemPosition();
int nItem;
CString filename,attrib;
while (pos)
{
nItem = list.GetNextSelectedItem(pos);
filename=list.GetItemText(nItem,0);
attrib=list.GetItemText(nItem,1);
if(filename=="." || filename=="..")
continue;
if(attrib=="文件夹")
{
if(!RemoveDirectory(path+"\\"+filename))
MessageBox("删除文件夹:"+path+"\\"+filename+" 出错!,可能目录不为空");
}
else
{
if(!DeleteFile(path+"\\"+filename))
MessageBox("删除文件夹:"+path+"\\"+filename+" 出错!");
}
}
Retrive();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -