Tab4.cpp
资源名称:OSDemo [点击查看]
上传用户:tianjwyx
上传日期:2007-01-13
资源大小:813k
文件大小:33k
源码类别:
操作系统开发
开发平台:
Visual C++
- // Tab4.cpp : implementation file
- //
- #include "stdafx.h"
- #include "OSDemo.h"
- #include "Tab4.h"
- #include "PieChartCtrl.h"
- #include "CheckerCtrl.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- FAT FileFAT;
- CFile FATIO;
- CString CurrentUser;
- int CurrentID;
- bool bLogin;
- bool bFormated;
- bool WriteFAT()
- {
- FATIO.SeekToBegin();
- FATIO.Write(&FileFAT,sizeof(FileFAT));
- return true;
- }
- bool CheckLogin()
- {
- if (!bLogin)
- {
- AfxMessageBox("请先登录文件系统!");
- return false;
- }
- else
- return true;
- }
- bool ReadFAT()
- {
- ZeroMemory(&FileFAT,sizeof(FileFAT));
- FATIO.SeekToBegin();
- if (!FATIO.Read(&FileFAT,sizeof(FileFAT))) return false;
- return true;
- }
- bool FileExist(CString FilePath)
- {
- HANDLE Handle;
- WIN32_FIND_DATA W32;
- Handle=FindFirstFile(FilePath,&W32);
- if (Handle!=INVALID_HANDLE_VALUE)
- return true;
- else
- return false;
- }
- bool Format()
- {
- CString FATFile;
- int i;
- char fname[128];
- ::GetCurrentDirectory(128,fname); //FAT表信息保存在当前目录的VDISK.DAT中
- FATFile.Format("%s",fname);
- if (FATFile.Right(1)!="\")
- FATFile+="\VDISK.DAT";
- else
- FATFile+="VDISK.DAT";
- CFile fout;
- if( !fout.Open(FATFile,CFile::modeCreate|CFile::modeWrite,NULL))
- {
- return false;
- }
- ZeroMemory(&FileFAT,sizeof(FileFAT));
- FileFAT.SuperBlock.HeadInfo.pStack=0;
- for(i=0;i<10;i++)
- {
- FileFAT.SuperBlock.HeadInfo.pBlock[i]=i+1;
- }
- fout.WriteHuge(&FileFAT,sizeof(FileFAT));
- BYTE *Buffer;
- Buffer=(BYTE*)malloc(DISKSIZE);
- ZeroMemory(Buffer,DISKSIZE);
- BLOCK newBlock;
- for (i=10;i<=990;i+=10)
- {
- ZeroMemory(&newBlock,sizeof(newBlock));
- newBlock.HeadInfo.pStack=0;
- for (int k=0;k<10;k++)
- {
- newBlock.HeadInfo.pBlock[k]=i+1+k;
- }
- memcpy(Buffer+32*(i-1),&newBlock,32);
- }
- fout.WriteHuge(Buffer,DISKSIZE);
- free(Buffer);
- fout.Close();
- AfxMessageBox("创建虚拟磁盘文件成功!请选择用户管理添加新用户。");
- return true;
- }
- void StartIO() //打开VDISK.DAT文件,把该文件句柄保存在全局变量中,如果该文件不存在,创建并初始化该文件。
- {
- CString FATFile;
- char fname[128];
- ::GetCurrentDirectory(128,fname); //FAT表信息保存在当前目录的VDISK.DAT中
- FATFile.Format("%s",fname);
- if (FATFile.Right(1)!="\")
- FATFile+="\VDISK.DAT";
- else
- FATFile+="VDISK.DAT";
- bFormated=true;
- if (!FileExist(FATFile))
- {
- AfxMessageBox("当前目录下找不到VDISK.DAT,该文件是用来模拟磁盘及文件管理表。n单击确定创建新的虚拟磁盘文件!");
- bFormated=false;
- if (!Format())
- {
- AfxMessageBox("创建文件系统出错!");
- return;
- }
- }
- if(!FATIO.Open(FATFile,CFile::modeReadWrite|CFile::shareDenyWrite,NULL))
- {
- AfxMessageBox("调入FAT表失败,文件系统可能出错!");
- bFormated=false;
- return;
- }
- }
- bool ReadBlock(int id, BLOCK* buffer) //读指定块号内容(32字节)到buffer所指向的单元
- {
- if(!FATIO) return false;
- if(!FATIO.Seek(sizeof(FAT)+(id-1)*BLOCKSIZE,CFile::begin)) return false;
- if(FATIO.Read(buffer,BLOCKSIZE)!=BLOCKSIZE) return false;
- return true;
- }
- bool WriteBlock(int id, BLOCK* buffer) //把buffer所指向的单元的内容(32字节)写到指定的物理块中
- {
- if(!FATIO) return false;
- if(!FATIO.Seek(sizeof(FAT)+(id-1)*BLOCKSIZE,CFile::begin)) return false;
- FATIO.Write(buffer,BLOCKSIZE);
- return true;
- }
- USHORT AllocBlock() //分配一物理块,成功返回逻辑块号,失败返回0;
- {
- if(!FileFAT.SuperBlock.HeadInfo.pBlock[FileFAT.SuperBlock.HeadInfo.pStack]) return 0; //没有空间可分配,失败返回
- if(FileFAT.SuperBlock.HeadInfo.pStack<9)
- {
- WriteFAT();
- return FileFAT.SuperBlock.HeadInfo.pBlock[FileFAT.SuperBlock.HeadInfo.pStack++];
- }
- else
- {
- WriteFAT();
- USHORT returnBlock=FileFAT.SuperBlock.HeadInfo.pBlock[9];
- if(!ReadBlock(returnBlock,&FileFAT.SuperBlock)) return 0;
- return returnBlock;
- }
- WriteFAT();
- return 0;
- }
- bool FreeBlock(USHORT BlockID) //回收一物理块,成功返回TRUE,失败返回FALSE
- {
- if(FileFAT.SuperBlock.HeadInfo.pStack>0)
- {
- FileFAT.SuperBlock.HeadInfo.pBlock[--FileFAT.SuperBlock.HeadInfo.pStack]=BlockID;
- BLOCK newBlock;
- if (!WriteBlock(BlockID,&newBlock)) return false;
- }
- else
- {
- if (!WriteBlock(BlockID,&FileFAT.SuperBlock)) return false;
- FileFAT.SuperBlock.HeadInfo.pStack=9;
- FileFAT.SuperBlock.HeadInfo.pBlock[9]=BlockID;
- }
- WriteFAT();
- return true;
- }
- class CDlgUser : public CDialog
- {
- // Construction
- public:
- void ShowUser();
- void AddListUser(CString Name,CString FileNum);
- void InitUserList();
- CDlgUser(CWnd* pParent = NULL); // standard constructor
- CImageList* ImageList1;
- CImageList* ImageList2;
- // Dialog Data
- //{{AFX_DATA(CDlgUser)
- enum { IDD = IDD_DLG_USER };
- CListCtrl m_UserListView;
- CString m_UserName;
- //}}AFX_DATA
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CDlgUser)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- // Generated message map functions
- //{{AFX_MSG(CDlgUser)
- virtual BOOL OnInitDialog();
- afx_msg void OnDelUser();
- afx_msg void OnAddUser();
- afx_msg void OnClickUserList(NMHDR* pNMHDR, LRESULT* pResult);
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- private:
- int ItemUser;
- };
- /////////////////////////////////////////////////////////////////////////////
- // CDlgUser dialog
- CDlgUser::CDlgUser(CWnd* pParent /*=NULL*/)
- : CDialog(CDlgUser::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDlgUser)
- m_UserName = _T("");
- //}}AFX_DATA_INIT
- }
- void CDlgUser::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgUser)
- DDX_Control(pDX, IDC_USER_LIST, m_UserListView);
- DDX_Text(pDX, IDC_USER_NAME, m_UserName);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDlgUser, CDialog)
- //{{AFX_MSG_MAP(CDlgUser)
- ON_BN_CLICKED(IDC_DEL_USER, OnDelUser)
- ON_BN_CLICKED(ID_ADD_USER, OnAddUser)
- ON_NOTIFY(NM_CLICK, IDC_USER_LIST, OnClickUserList)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgUser message handlers
- BOOL CDlgUser::OnInitDialog()
- {
- CDialog::OnInitDialog();
- InitUserList();
- ShowUser();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CDlgUser::InitUserList()
- {
- DWORD dwStyle = m_UserListView.GetExtendedStyle();
- ListView_SetExtendedListViewStyle(m_UserListView.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- m_UserListView.InsertColumn(0,"用户名",LVCFMT_LEFT,100);
- m_UserListView.InsertColumn(1,"文件数",LVCFMT_RIGHT,50);
- ImageList1 = new CImageList();
- ImageList1->Create(16, 16, ILC_COLOR8 | ILC_MASK, 9, 9);
- ImageList1->Add(AfxGetApp()->LoadIcon(IDI_USER));
- m_UserListView.SetImageList(ImageList1, LVSIL_SMALL);
- //m_UserListView.SetImageList(ImageList2, LVSIL_NORMAL);
- }
- void CDlgUser::OnDelUser()
- {
- UpdateData(true);
- int i,j,k,pos;
- for(i=0;i<MAXUSER;i++)
- {
- if(FileFAT.Mfd[i].UserName==m_UserName)
- {
- pos=i;
- if(i==CurrentID)
- {
- MessageBox("您不能删除当前已登录的用户!","警告:",MB_ICONINFORMATION);
- return;
- }
- for(j=0;j<MAXFILE;j++)
- {
- if( FileFAT.Mfd[i].ufd[j].IsExist)
- {
- for(k=0;k<FileFAT.Mfd[i].ufd[j].BlockNum;k++)
- {
- if(!FreeBlock(FileFAT.Mfd[i].ufd[j].FileLink[k]))
- {
- MessageBox("删除用户出错,被删除帐号的文件块在回收中发生错误!");
- return;
- }
- }
- }
- FileFAT.Mfd[i].ufd[j].IsExist=false;
- }
- }
- }
- ZeroMemory(&FileFAT.Mfd[pos],sizeof(MFD));
- FileFAT.UserNum--;
- WriteFAT();
- ShowUser();
- }
- void CDlgUser::AddListUser(CString Name, CString FileNum)
- {
- LVITEM lvitem;
- int iActualItem;
- CString strDate;
- for(int iSubItem=0;iSubItem<2;iSubItem++)
- {
- lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
- lvitem.iItem = (iSubItem == 0)? ++ItemUser : iActualItem;
- lvitem.iSubItem = iSubItem;
- switch(iSubItem)
- {
- case 0:
- lvitem.pszText =(LPTSTR)(LPCTSTR)Name;
- lvitem.iImage=0;
- break;
- case 1:
- lvitem.pszText = (LPTSTR)(LPCTSTR)FileNum;
- break;
- }
- if (iSubItem == 0)
- iActualItem = m_UserListView.InsertItem(&lvitem);
- else
- m_UserListView.SetItem(&lvitem);
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDlgLogin dialog
- class CDlgLogin : public CDialog
- {
- // Construction
- public:
- int UserID;
- void ShowUser();
- void AddUserList(CString Name);
- CString UserName;
- CImageList m_ImageList;
- void InitUserList();
- CDlgLogin(CWnd* pParent = NULL); // standard constructor
- // Dialog Data
- //{{AFX_DATA(CDlgLogin)
- enum { IDD = IDD_DIALOG_LOGIN };
- CListCtrl m_UserList;
- CComboBox m_UserName;
- //}}AFX_DATA
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CDlgLogin)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- // Generated message map functions
- //{{AFX_MSG(CDlgLogin)
- virtual void OnOK();
- virtual BOOL OnInitDialog();
- afx_msg void OnClickUserlist(NMHDR* pNMHDR, LRESULT* pResult);
- afx_msg void OnDblclkUserlist(NMHDR* pNMHDR, LRESULT* pResult);
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- };
- /////////////////////////////////////////////////////////////////////////////
- // CDlgLogin dialog
- CDlgLogin::CDlgLogin(CWnd* pParent /*=NULL*/)
- : CDialog(CDlgLogin::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDlgLogin)
- //}}AFX_DATA_INIT
- }
- void CDlgLogin::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgLogin)
- DDX_Control(pDX, IDC_USERLIST, m_UserList);
- DDX_Control(pDX, IDC_USERNAME, m_UserName);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDlgLogin, CDialog)
- //{{AFX_MSG_MAP(CDlgLogin)
- ON_NOTIFY(NM_CLICK, IDC_USERLIST, OnClickUserlist)
- ON_NOTIFY(NM_DBLCLK, IDC_USERLIST, OnDblclkUserlist)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgLogin message handlers
- void CDlgLogin::OnOK()
- {
- // TODO: Add extra validation here
- m_UserName.GetWindowText(UserName);
- for(int i=0;i<10;i++)
- {
- if(UserName==FileFAT.Mfd[i].UserName)
- {
- UserID=i;
- }
- }
- CDialog::OnOK();
- }
- BOOL CDlgLogin::OnInitDialog()
- {
- CDialog::OnInitDialog();
- InitUserList();
- ShowUser();
- UserID=0;
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CDlgLogin::InitUserList()
- {
- HIMAGELIST hList = ImageList_Create(32,32, ILC_COLOR8 |ILC_MASK , 15, 1);
- m_ImageList.Attach(hList);
- m_ImageList.Add(AfxGetApp()->LoadIcon(IDI_USER));
- m_UserList.SetImageList(&m_ImageList, LVSIL_NORMAL);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CTab4 dialog
- CTab4::CTab4(CWnd* pParent /*=NULL*/)
- : CDialog(CTab4::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CTab4)
- m_FileContent = _T("");
- //}}AFX_DATA_INIT
- }
- void CTab4::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CTab4)
- DDX_Control(pDX, IDC_BTN_LOGIN, m_LoginBtn);
- DDX_Control(pDX, IDC_FILETREE, m_FileTree);
- DDX_Control(pDX, IDC_FILELIST, m_FileList);
- DDX_Text(pDX, IDC_FILE_CONTENT, m_FileContent);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CTab4, CDialog)
- //{{AFX_MSG_MAP(CTab4)
- ON_BN_CLICKED(IDC_BTN_LOGIN, OnBtnLogin)
- ON_BN_CLICKED(IDC_BTN_ADD, OnBtnAdd)
- ON_BN_CLICKED(IDC_BTN_MODIFY, OnBtnModify)
- ON_BN_CLICKED(IDC_BTN_USER, OnBtnUser)
- ON_BN_CLICKED(IDC_BTN_DELETE, OnBtnDelete)
- ON_BN_CLICKED(IDC_BTN_DISK, OnBtnDisk)
- ON_BN_CLICKED(IDC_BTN_CLOSE, OnBtnClose)
- ON_BN_CLICKED(IDC_BTN_OPEN, OnBtnOpen)
- ON_NOTIFY(NM_CLICK, IDC_FILELIST, OnClickFilelist)
- ON_WM_CTLCOLOR()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CTab4 message handlers
- BOOL CTab4::OnInitDialog()
- {
- CDialog::OnInitDialog();
- bLogin=false;
- ItemCount=0;
- m_bluecolor=RGB(239,239,239); // blue
- m_textcolor=RGB(0,0,255); // white text
- m_bluebrush.CreateSolidBrush(m_bluecolor); // blue background
- InitFileList();
- InitFileTree();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CTab4::InitFileList()
- {
- DWORD dwStyle = m_FileList.GetExtendedStyle();
- ListView_SetExtendedListViewStyle(m_FileList.m_hWnd, LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- m_FileList.InsertColumn(0,"文件名",LVCFMT_LEFT,100);
- m_FileList.InsertColumn(1,"文件主",LVCFMT_LEFT,60);
- m_FileList.InsertColumn(2,"文件大小",LVCFMT_RIGHT,80);
- m_FileList.InsertColumn(3,"修改时间",LVCFMT_LEFT,120);
- m_FileList.InsertColumn(4,"状态",LVCFMT_LEFT,40);
- m_FileList.InsertColumn(5,"属性",LVCFMT_LEFT,40);
- m_pImageList = new CImageList();
- m_pImageListL = new CImageList();
- SetupImages(m_pImageList, 16);
- SetupImages(m_pImageListL, 32);
- m_FileList.SetImageList(m_pImageList, LVSIL_SMALL);
- m_FileList.SetImageList(m_pImageListL, LVSIL_NORMAL);
- }
- void CTab4::InitFileTree()
- {
- TreeImageList = new CImageList();
- TreeImageList->Create(20, 20, ILC_COLOR8 | ILC_MASK, 9, 9);
- TreeImageList->Add(AfxGetApp()->LoadIcon(IDI_COMPUTER));
- TreeImageList->Add(AfxGetApp()->LoadIcon(IDI_USER));
- TreeImageList->Add(AfxGetApp()->LoadIcon(IDI_FILE));
- TreeImageList->Add(AfxGetApp()->LoadIcon(IDI_ICON2));
- m_FileTree.SetImageList(TreeImageList,TVSIL_NORMAL);
- HTREEITEM hRoot = m_FileTree.InsertItem("请先登录文件系统",0,0);
- }
- bool CTab4::AddFile(CString Path, CString FName, int FSize,CString Attr)
- {
- return true;
- }
- void CTab4::AddListFile(CString FName,CString FileOwn,CString FSize,CString Date,CString State,CString Attr)
- {
- LVITEM lvitem;
- int iActualItem;
- for(int iSubItem=0;iSubItem<6;iSubItem++)
- {
- lvitem.mask = LVIF_TEXT|(iSubItem == 0? LVIF_IMAGE : 0);
- lvitem.iItem = (iSubItem == 0)? ++ItemCount : iActualItem;
- lvitem.iSubItem = iSubItem;
- switch(iSubItem)
- {
- case 0:
- lvitem.pszText =(LPTSTR)(LPCTSTR)FName;
- Attr.MakeUpper();
- if (Attr.Find("E")>0)
- lvitem.iImage=1;
- else
- lvitem.iImage=0;
- break;
- case 1:
- lvitem.pszText = (LPTSTR)(LPCTSTR)FileOwn;
- break;
- case 2:
- FSize+=" 字节";
- lvitem.pszText = (LPTSTR)(LPCTSTR)FSize;
- break;
- case 3:
- lvitem.pszText = (LPTSTR)(LPCTSTR)Date;
- break;
- case 4:
- lvitem.pszText = (LPTSTR)(LPCTSTR)State;
- break;
- case 5:
- lvitem.pszText = (LPTSTR)(LPCTSTR)Attr;
- break;
- }
- if (iSubItem == 0)
- iActualItem = m_FileList.InsertItem(&lvitem);
- else
- m_FileList.SetItem(&lvitem);
- }
- }
- void CTab4::SetupImages(CImageList *mImageList, int iSize)
- {
- mImageList->Create(iSize, iSize, ILC_COLOR8 | ILC_MASK, 9, 9);
- mImageList->Add(AfxGetApp()->LoadIcon(IDI_FILE));
- mImageList->Add(AfxGetApp()->LoadIcon(IDI_ICON2));
- }
- void CTab4::OnBtnAdd()
- {
- if(!CheckLogin()) return;
- CDlgNewFile DlgNewFile;
- DlgNewFile.flag=true;
- if(DlgNewFile.DoModal()==IDOK)
- {
- if(CreateNewFile(DlgNewFile.m_FileName,DlgNewFile.m_Content,true,(!DlgNewFile.m_Read),DlgNewFile.m_Execute,DlgNewFile.m_Share))
- {
- MessageBox("添加文件成功!","提示:",MB_ICONINFORMATION);
- }
- else
- {
- MessageBox("添加文件失败!可能磁盘空间不足或已超过用户允许保存的文件数!","提示:",MB_ICONEXCLAMATION);
- }
- }
- ShowUserFiles();
- }
- void CTab4::OnBtnModify()
- {
- if(!CheckLogin()) return;
- CDlgNewFile DlgNewFile;
- DlgNewFile.flag=false;
- DlgNewFile.m_FileName=SelectedFileName;
- DlgNewFile.m_Content=ReadFile(SelectedFileName);
- CString Attribute=m_FileList.GetItemText(SelectPos,5);
- DlgNewFile.m_Execute=Attribute.Find("E")>0?true:false;
- DlgNewFile.m_Read=Attribute.Find("W")>0?false:true;
- DlgNewFile.m_Share=Attribute.Find("S")>0?true:false;
- //DlgNewFile.UpdateData(false);
- if(DlgNewFile.DoModal()==IDOK)
- {
- if(ModifyFile(DlgNewFile.m_FileName,DlgNewFile.m_Content,true,(!DlgNewFile.m_Read),DlgNewFile.m_Execute,DlgNewFile.m_Share))
- {
- MessageBox("修改文件成功!","提示:",MB_ICONINFORMATION);
- }
- else
- {
- MessageBox("修改文件失败!","提示:",MB_ICONEXCLAMATION);
- }
- }
- ShowUserFiles();
- }
- void CTab4::OnBtnLogin()
- {
- FATIO.Close();
- StartIO();
- if(!bFormated)
- {
- CDlgUser DlgUser;
- DlgUser.DoModal();
- return;
- }
- CDlgLogin dlgLogin;
- ZeroMemory(&OpenList,sizeof(OpenList));
- if(dlgLogin.DoModal()==IDOK)
- {
- CurrentUser=dlgLogin.UserName;
- CurrentID=dlgLogin.UserID;
- ShowUserFiles();
- bLogin=true;
- CRect rect;
- m_FileList.GetWindowRect(&rect);
- CPoint pt = rect.CenterPoint();
- ShowBallTip(pt,"单击列表的文件项,可以选定文件查看其内容或是其它操作!");
- }
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDlgUser dialog
- void CTab4::OnBtnUser()
- {
- CDlgUser DlgUser;
- DlgUser.DoModal();
- }
- void CDlgUser::OnAddUser()
- {
- UpdateData(true);
- if (FileFAT.UserNum>=10)
- {
- AfxMessageBox("用户数量已达到最大10个,你可以删除一些无用的帐号!");
- return;
- }
- for(int i=0;i<10;i++)
- {
- if(FileFAT.Mfd[i].IsExist==false)
- {
- wsprintf(FileFAT.Mfd[i].UserName,m_UserName);
- FileFAT.Mfd[i].IsExist=true;
- FileFAT.Mfd[i].nItem=0;
- break;
- }
- }
- FileFAT.UserNum++;
- WriteFAT();
- ReadFAT();
- ShowUser();
- m_UserName.Empty();
- UpdateData(false);
- }
- void CDlgUser::ShowUser()
- {
- m_UserListView.DeleteAllItems();
- ReadFAT();
- int i;
- //CString str;
- //str.Format("%d",FileFAT.UserNum);
- //MessageBox(str);
- CString strItem;
- for (i=0;i<FileFAT.UserNum;i++)
- {
- strItem.Format("%d",FileFAT.Mfd[i].nItem);
- AddListUser(FileFAT.Mfd[i].UserName,strItem);
- }
- }
- void CDlgLogin::AddUserList(CString Name)
- {
- LVITEM lvi;
- lvi.mask = LVIF_IMAGE | LVIF_TEXT;
- lvi.iItem = 0;
- lvi.iSubItem = 0;
- lvi.iImage = 0;
- lvi.pszText = (LPTSTR)(LPCTSTR)(Name);
- m_UserList.InsertItem(&lvi);
- }
- void CDlgLogin::ShowUser()
- {
- m_UserList.DeleteAllItems();
- ReadFAT();
- for (int i=0;i<FileFAT.UserNum;i++)
- {
- AddUserList(FileFAT.Mfd[i].UserName);
- m_UserName.AddString(FileFAT.Mfd[i].UserName);
- m_UserName.SetCurSel(0);
- }
- m_UserList.SetHotItem(0);
- }
- void CTab4::OnBtnDelete()
- {
- // m_JobList.-1)
- if(!CheckLogin()) return;
- if(!m_FileList.GetFirstSelectedItemPosition())
- {
- MessageBox("请先在文件列表中选择要删除的文件!","提示:",MB_ICONSTOP);
- return;
- }
- if(KillFile(SelectedFileName))
- ShowUserFiles();
- else
- MessageBox("文件删除出错!","错误:",MB_ICONEXCLAMATION);
- }
- void CDlgLogin::OnClickUserlist(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- CString strName=m_UserList.GetItemText(pNMListView->iItem,pNMListView->iSubItem);
- CString ComboText;
- for (int i=0;i<m_UserName.GetCount();i++)
- {
- ComboText.Empty();
- m_UserName.GetLBText(i,ComboText);
- if (strName==ComboText) m_UserName.SetCurSel(i);
- }
- *pResult = 0;
- }
- void CTab4::ShowUserFiles()
- {
- int i;
- m_FileTree.DeleteAllItems();
- m_FileList.DeleteAllItems();
- HTREEITEM hRoot = m_FileTree.InsertItem("我的电脑",0,0);
- CString FileNum;
- FileNum.Format(" (%d Files)",FileFAT.Mfd[CurrentID].nItem);
- HTREEITEM hDir1 = m_FileTree.InsertItem (CurrentUser+FileNum,1,1,hRoot);
- SYSTEMTIME systm;
- ::GetLocalTime(&systm);
- CString TimeStr;
- TimeStr.Format("%d/%d/%d %d:%d",systm.wYear,systm.wMonth,systm.wDay,systm.wHour,systm.wMinute);
- CString strFileLen,strFileAttr,strFileState;
- for (i=0;i<MAXFILE;i++)
- {
- if(FileFAT.Mfd[CurrentID].ufd[i].IsExist==true)
- {
- if(FileFAT.Mfd[CurrentID].ufd[i].AttrExecute)
- m_FileTree.InsertItem ( FileFAT.Mfd[CurrentID].ufd[i].FileName,3,3,hDir1);
- else
- m_FileTree.InsertItem ( FileFAT.Mfd[CurrentID].ufd[i].FileName,2,2,hDir1);
- strFileLen.Format("%d",FileFAT.Mfd[CurrentID].ufd[i].FileLen);
- strFileAttr.Empty();
- strFileState=(OpenList[CurrentID][i])?"打开":"关闭";
- if(FileFAT.Mfd[CurrentID].ufd[i].AttrRead)
- strFileAttr+="R";
- if(FileFAT.Mfd[CurrentID].ufd[i].AttrWrite)
- strFileAttr+="W";
- if(FileFAT.Mfd[CurrentID].ufd[i].AttrExecute)
- strFileAttr+="E";
- if(FileFAT.Mfd[CurrentID].ufd[i].IsShared)
- strFileAttr+="S";
- AddListFile(FileFAT.Mfd[CurrentID].ufd[i].FileName,CurrentUser,strFileLen,FileFAT.Mfd[CurrentID].ufd[i].Time,strFileState,strFileAttr);
- }
- }
- m_FileTree.Expand(hRoot,TVE_EXPAND);
- m_FileTree.Expand(hDir1,TVE_EXPAND);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CDlgDisk dialog
- class CDlgDisk : public CDialog
- {
- // Construction
- public:
- CDlgDisk(CWnd* pParent = NULL); // standard constructor
- CPieChartCtrl m_wndChart;
- // Dialog Data
- //{{AFX_DATA(CDlgDisk)
- enum { IDD = IDD_DLGDISK };
- CStatic m_PicDisk;
- CString m_Used;
- CString m_UserNum;
- //}}AFX_DATA
- // Overrides
- // ClassWizard generated virtual function overrides
- //{{AFX_VIRTUAL(CDlgDisk)
- protected:
- virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
- //}}AFX_VIRTUAL
- // Implementation
- protected:
- // Generated message map functions
- //{{AFX_MSG(CDlgDisk)
- virtual BOOL OnInitDialog();
- afx_msg void OnExit();
- afx_msg void OnFormat();
- //}}AFX_MSG
- DECLARE_MESSAGE_MAP()
- private:
- void CalcBlock();
- void InitBlockShow();
- CCheckerCtrl m_ctrlChecker;
- void AddPiece(COLORREF colorBack, int nAngle, const CString &str);
- };
- /////////////////////////////////////////////////////////////////////////////
- // CDlgDisk dialog
- CDlgDisk::CDlgDisk(CWnd* pParent /*=NULL*/)
- : CDialog(CDlgDisk::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CDlgDisk)
- m_Used = _T("");
- m_UserNum = _T("");
- //}}AFX_DATA_INIT
- }
- void CDlgDisk::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgDisk)
- DDX_Text(pDX, IDC_STATIC_USED, m_Used);
- DDX_Text(pDX, IDC_STATIC_USERNUM, m_UserNum);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDlgDisk, CDialog)
- //{{AFX_MSG_MAP(CDlgDisk)
- ON_BN_CLICKED(IDEXIT, OnExit)
- ON_BN_CLICKED(IDOK, OnCancel)
- ON_BN_CLICKED(IDC_FORMAT, OnFormat)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgDisk message handlers
- void CTab4::OnBtnDisk()
- {
- CDlgDisk DlgDisk;
- DlgDisk.DoModal();
- }
- BOOL CDlgDisk::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_wndChart.SubclassDlgItem(IDC_PIECHART2, this);
- InitBlockShow();
- CalcBlock();
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CDlgDisk::AddPiece(COLORREF colorBack, int nAngle, const CString &str)
- {
- m_wndChart.AddPiece(colorBack,RGB(0,0,0),nAngle,str);
- }
- void CDlgDisk::OnExit()
- {
- OnCancel();
- }
- void CDlgDisk::OnFormat()
- {
- if(MessageBox("格式化虚拟磁盘可重新创建文件系统,原来系统中的数据会丢失,继续吗?","警告!",MB_YESNO|MB_ICONINFORMATION)==IDYES)
- {
- FATIO.Close();
- Format();
- }
- }
- bool CTab4::KillFile(CString FileName)
- {
- int i,FilePos;
- bool flag=false;
- for(i=0;i<MAXFILE;i++)
- {
- if(FileFAT.Mfd[CurrentID].ufd[i].FileName==FileName && FileFAT.Mfd[CurrentID].ufd[i].IsExist==true)
- {
- flag=true;
- FilePos=i;
- break;
- }
- }
- if(!flag) return false;
- if(FileFAT.Mfd[CurrentID].ufd[FilePos].AttrWrite==false)
- {
- if(MessageBox("文件 "+SelectedFileName+" 是只读文件,您确实要删除吗? ","删除警告:",MB_ICONQUESTION|MB_YESNO)==IDNO)
- return true;
- }
- for(i=0;i<FileFAT.Mfd[CurrentID].ufd[FilePos].BlockNum;i++)
- {
- if(!FreeBlock(FileFAT.Mfd[CurrentID].ufd[FilePos].FileLink[i])) return false;
- }
- FileFAT.Mfd[CurrentID].nItem--;
- ZeroMemory(&FileFAT.Mfd[CurrentID].ufd[FilePos],sizeof(UFD));
- WriteFAT();
- return true;
- }
- void CTab4::OnBtnClose()
- {
- // TODO: Add your control notification handler code here
- if(!CheckLogin()) return;
- for(int i=0;i<FileFAT.Mfd[CurrentID].nItem;i++)
- {
- if(FileFAT.Mfd[CurrentID].ufd[i].FileName==SelectedFileName)
- {
- OpenList[CurrentID][i]=false;
- }
- }
- m_FileList.SetItem(SelectPos,4,LVIF_TEXT,"关闭",0,0,0,0);
- }
- void CTab4::OnBtnOpen()
- {
- if(!CheckLogin()) return;
- for(int i=0;i<FileFAT.Mfd[CurrentID].nItem;i++)
- {
- if(FileFAT.Mfd[CurrentID].ufd[i].FileName==SelectedFileName)
- {
- OpenList[CurrentID][i]=true;
- }
- }
- m_FileList.SetItem(SelectPos,4,LVIF_TEXT,"打开",0,0,0,0);
- }
- CString CTab4::GetCurrentTime()
- {
- SYSTEMTIME systm;
- ::GetLocalTime(&systm);
- CString TimeStr;
- CString strHour,strMin;
- strHour.Format("%d",systm.wHour);
- strMin.Format("%d",systm.wMinute);
- if(strHour.GetLength()<2)
- strHour="0"+strHour;
- if(strMin.GetLength()<2)
- strMin+="0"+strMin;
- TimeStr.Format("%d/%d/%d %s:%s",systm.wYear,systm.wMonth,systm.wDay,strHour,strMin);
- return TimeStr;
- }
- bool CTab4::CreateNewFile(CString FileName, CString FileContent, bool AttrRead, bool AttrWrite, bool AttrExecute, bool AttrShare)
- {
- int i,FilePos,FileLen,BlockNum,BlockID;
- bool flag=false;
- FileContent.TrimLeft();
- FileContent.TrimRight();
- FileLen=FileContent.GetLength();
- char Content[32];
- for(i=0;i<MAXFILE;i++)
- {
- if(!FileFAT.Mfd[CurrentID].ufd[i].IsExist)
- {
- flag=true;
- FilePos=i;
- break;
- }
- }
- if(!flag) return false;
- BlockNum=(int)FileLen/(BLOCKSIZE+1)+1;
- int count;
- for(i=0;i<BlockNum;i++)
- {
- BlockID=AllocBlock();
- if(BlockID)
- {
- FileFAT.Mfd[CurrentID].ufd[FilePos].FileLink[i]=BlockID;
- count=FileContent.GetLength()-i*32;
- ZeroMemory(&Content,sizeof(Content));
- if(count>32)
- wsprintf(Content,FileContent.Mid(i*32,32));
- else
- wsprintf(Content,FileContent.Mid(i*32,count));
- if(!WriteBlock(BlockID,(BLOCK*)Content)) return false;
- }
- else
- {
- return false;
- }
- }
- wsprintf(FileFAT.Mfd[CurrentID].ufd[FilePos].FileName,FileName.Left(15));
- FileFAT.Mfd[CurrentID].ufd[FilePos].IsExist=true;
- FileFAT.Mfd[CurrentID].ufd[FilePos].BlockNum=BlockNum;
- FileFAT.Mfd[CurrentID].ufd[FilePos].AttrExecute=AttrExecute;
- FileFAT.Mfd[CurrentID].ufd[FilePos].AttrRead=AttrRead;
- FileFAT.Mfd[CurrentID].ufd[FilePos].AttrWrite=AttrWrite;
- FileFAT.Mfd[CurrentID].ufd[FilePos].IsShared=AttrShare;
- FileFAT.Mfd[CurrentID].ufd[FilePos].FileLen=FileContent.GetLength();
- //str.Format("FileName:%s",FileFAT.Mfd[CurrentID].ufd[FilePos].FileName);
- //MessageBox(str);
- wsprintf(FileFAT.Mfd[CurrentID].ufd[FilePos].Time,GetCurrentTime());
- FileFAT.Mfd[CurrentID].nItem++;
- WriteFAT();
- ShowUserFiles();
- return true;
- }
- CString CTab4::ReadFile(CString FileName)
- {
- int i,FilePos;
- bool flag=false;
- char Buffer[32];
- CString strReturn;
- for(i=0;i<MAXFILE;i++)
- {
- if(FileFAT.Mfd[CurrentID].ufd[i].FileName==FileName && FileFAT.Mfd[CurrentID].ufd[i].IsExist==true)
- {
- flag=true;
- FilePos=i;
- break;
- }
- }
- if(!flag) return "";
- for(i=0;i<FileFAT.Mfd[CurrentID].ufd[FilePos].BlockNum;i++)
- {
- ZeroMemory(Buffer,32);
- if(!ReadBlock(FileFAT.Mfd[CurrentID].ufd[FilePos].FileLink[i],(BLOCK*)Buffer)) return "";
- //MessageBox(Buffer);
- strReturn+=Buffer;
- }
- return strReturn;
- }
- void CTab4::OnClickFilelist(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- CString strFName;
- strFName=m_FileList.GetItemText(pNMListView->iItem,0);
- SelectPos=pNMListView->iItem;
- SelectedFileName=m_FileList.GetItemText(pNMListView->iItem,0);
- m_FileContent=ReadFile(strFName);
- UpdateData(false);
- *pResult = 0;
- }
- bool CTab4::ModifyFile(CString FileName, CString FileContent, bool AttrRead, bool AttrWrite, bool AttrExecute, bool AttrShare)
- {
- int i,FilePos,BlockNum,BlockID;
- bool flag=false;
- FileContent.TrimLeft();
- FileContent.TrimRight();
- char Content[32];
- for(i=0;i<MAXFILE;i++)
- {
- if(FileFAT.Mfd[CurrentID].ufd[i].FileName==SelectedFileName)
- {
- flag=true;
- FilePos=i;
- //CString str;
- //str.Format("%d",FilePos);
- //MessageBox(str);
- break;
- }
- }
- if(!flag) return false;
- for(i=0;i<FileFAT.Mfd[CurrentID].ufd[FilePos].BlockNum;i++)
- {
- if(!FreeBlock(FileFAT.Mfd[CurrentID].ufd[FilePos].FileLink[i])) return false;
- }
- ZeroMemory(&FileFAT.Mfd[CurrentID].ufd[FilePos].BlockNum,200);
- BlockNum=(int)FileContent.GetLength()/(BLOCKSIZE+1)+1;
- int count;
- for(i=0;i<BlockNum;i++)
- {
- BlockID=AllocBlock();
- if(BlockID)
- {
- FileFAT.Mfd[CurrentID].ufd[FilePos].FileLink[i]=BlockID;
- count=FileContent.GetLength()-i*32;
- ZeroMemory(&Content,sizeof(Content));
- if(count>32)
- wsprintf(Content,FileContent.Mid(i*32,32));
- else
- wsprintf(Content,FileContent.Mid(i*32,count));
- if(!WriteBlock(BlockID,(BLOCK*)Content)) return false;
- }
- else
- {
- return false;
- }
- }
- wsprintf(FileFAT.Mfd[CurrentID].ufd[FilePos].FileName,FileName.Left(15));
- FileFAT.Mfd[CurrentID].ufd[FilePos].IsExist=true;
- FileFAT.Mfd[CurrentID].ufd[FilePos].BlockNum=BlockNum;
- FileFAT.Mfd[CurrentID].ufd[FilePos].AttrExecute=AttrExecute;
- FileFAT.Mfd[CurrentID].ufd[FilePos].AttrRead=AttrRead;
- FileFAT.Mfd[CurrentID].ufd[FilePos].AttrWrite=AttrWrite;
- FileFAT.Mfd[CurrentID].ufd[FilePos].IsShared=AttrShare;
- FileFAT.Mfd[CurrentID].ufd[FilePos].FileLen=FileContent.GetLength();
- wsprintf(FileFAT.Mfd[CurrentID].ufd[FilePos].Time,GetCurrentTime());
- WriteFAT();
- ShowUserFiles();
- return true;
- }
- void CTab4::ShowBlock(int fileid)
- {
- CString str,temp;
- for (int i=0;i<FileFAT.Mfd[CurrentID].ufd[fileid].BlockNum;i++)
- {
- temp.Format(",%d",FileFAT.Mfd[CurrentID].ufd[fileid].FileLink[i]);
- str+=temp;
- }
- MessageBox(str);
- }
- void CDlgDisk::InitBlockShow()
- {
- CRect rect;
- GetDlgItem(IDC_STATIC_CHECKER)->GetWindowRect(rect);
- ScreenToClient(rect);
- m_ctrlChecker.Create(WS_VISIBLE | WS_CHILD | WS_VSCROLL | WS_TABSTOP, rect, this, IDC_STATIC_CHECKER);
- m_ctrlChecker.SetTotalBlocks(1000);
- /*for(register i = 10; i < 35; i++)
- m_ctrlChecker.SetBlock(i, RGB(0, 155, 0));
- for(i = 50; i < 120; i++)
- m_ctrlChecker.SetBlock(i, RGB(255, 0, 0));
- for(i = 150; i < 255; i++)
- m_ctrlChecker.SetBlock(i, RGB(0, 0, i));
- for(i = 256; i < 300; i++)
- m_ctrlChecker.SetBlock(i, RGB(0, 0, 0));
- m_ctrlChecker.Refresh();*/
- }
- HBRUSH CTab4::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr;
- switch (nCtlColor)
- {
- case CTLCOLOR_EDIT:
- case CTLCOLOR_MSGBOX:
- switch (pWnd->GetDlgCtrlID())
- {
- case IDC_FILE_CONTENT: // first CEdit control ID
- // put your own CONTROL ID here
- pDC->SetBkColor(m_bluecolor); // change the background color
- pDC->SetTextColor(m_textcolor); // change the text color
- hbr = (HBRUSH) m_bluebrush; // apply the brush
- break;
- // otherwise do default handling of OnCtlColor
- default:
- hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
- }
- break;
- // otherwise do default handling of OnCtlColor
- default:
- hbr=CDialog::OnCtlColor(pDC,pWnd,nCtlColor);
- }
- return hbr; // return brush
- }
- void CDlgDisk::CalcBlock()
- {
- int i,j,k,sum;
- for (i=0;i<MAXUSER;i++)
- {
- if(FileFAT.Mfd[i].IsExist)
- {
- for(j=0;j<MAXFILE;j++)
- {
- if(FileFAT.Mfd[i].ufd[j].IsExist)
- {
- for(k=0;k<FileFAT.Mfd[i].ufd[j].BlockNum;k++)
- {
- m_ctrlChecker.SetBlock(FileFAT.Mfd[i].ufd[j].FileLink[k]-1, RGB(0, 0, 255));
- sum++;
- }
- }
- }
- }
- }
- AddPiece(RGB(200,0,0),sum*360/1000,"Used");
- //CString str;
- //str.Format("%d",sum*360/1000);
- //MessageBox(str);
- m_Used.Format("已用空间:%d 字节",sum*32);
- m_UserNum.Format("用户数:%d",FileFAT.UserNum);
- UpdateData(false);
- }
- void CDlgUser::OnClickUserList(NMHDR* pNMHDR, LRESULT* pResult)
- {
- NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
- m_UserName=m_UserListView.GetItemText(pNMListView->iItem,0);
- UpdateData(false);
- *pResult = 0;
- }
- void CDlgLogin::OnDblclkUserlist(NMHDR* pNMHDR, LRESULT* pResult)
- {
- *pResult = 0;
- OnOK();
- }
- void CTab4::ShowBallTip(CPoint pt, CString msg)
- {
- LOGFONT lf;
- ::ZeroMemory (&lf, sizeof (lf));
- lf.lfHeight = 15;
- lf.lfWeight = FW_BOLD;
- lf.lfUnderline = FALSE;
- ::strcpy (lf.lfFaceName, _T("楷体_GB2312"));
- m_pBalloonTip = CBalloonTip::Show(pt, CSize(250, 100), msg, lf, 5, TRUE);
- }