www.pudn.com > FileGuard.zip > FileGuardView.cpp


// FileGuardView.cpp : implementation of the CFileGuardView class 
// 
 
#include "stdafx.h" 
#include "FileGuard.h" 
#include "FGDevice.h" 
#include "FileGuardApp.h" 
 
#include "FileGuardDoc.h" 
#include "FileGuardView.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileGuardView 
 
IMPLEMENT_DYNCREATE(CFileGuardView, CFormView) 
 
BEGIN_MESSAGE_MAP(CFileGuardView, CFormView) 
	//{{AFX_MSG_MAP(CFileGuardView) 
	ON_BN_CLICKED(IDC_DELETE_PROTECTION, OnProtectionTypeChange) 
	ON_MESSAGE(WM_SHOW_DATA, OnShowData) 
	ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnToolTip )		//debug 
	ON_BN_CLICKED(IDC_WRITE_PROTECTION, OnProtectionTypeChange) 
	ON_BN_CLICKED(IDC_READ_PROTECTION, OnProtectionTypeChange) 
	ON_BN_CLICKED(IDC_HIDE, OnProtectionTypeChange) 
	//}}AFX_MSG_MAP 
	// Standard printing commands 
	ON_COMMAND(ID_FILE_PRINT, CFormView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CFormView::OnFilePrint) 
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CFormView::OnFilePrintPreview) 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileGuardView construction/destruction 
 
CFileGuardView::CFileGuardView() 
	: CFormView(CFileGuardView::IDD) 
{ 
	//{{AFX_DATA_INIT(CFileGuardView) 
	m_bFileHide = FALSE; 
	m_bFileReadProt = FALSE; 
	m_bFileWriteProt = FALSE; 
	m_bFileDeleteProt = FALSE; 
	//}}AFX_DATA_INIT 
	// TODO: add construction code here 
 
} 
 
CFileGuardView::~CFileGuardView() 
{ 
} 
 
void CFileGuardView::DoDataExchange(CDataExchange* pDX) 
{ 
	CFormView::DoDataExchange(pDX); 
	//{{AFX_DATA_MAP(CFileGuardView) 
	DDX_Control(pDX, IDC_FILE_TREE, m_dirTree); 
	DDX_Control(pDX, IDC_PROTECTED_FILE_LIST, m_protFileListCtrl); 
	DDX_Check(pDX, IDC_HIDE, m_bFileHide); 
	DDX_Check(pDX, IDC_READ_PROTECTION, m_bFileReadProt); 
	DDX_Check(pDX, IDC_WRITE_PROTECTION, m_bFileWriteProt); 
	DDX_Check(pDX, IDC_DELETE_PROTECTION, m_bFileDeleteProt); 
	//}}AFX_DATA_MAP 
} 
 
BOOL CFileGuardView::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
 
	return CFormView::PreCreateWindow(cs); 
} 
 
void CFileGuardView::OnInitialUpdate() 
{ 
	CFormView::OnInitialUpdate(); 
	GetParentFrame()->RecalcLayout(); 
	ResizeParentToFit(); 
 
	//Set controls to a approperiate size. 
	RECT clientRect, listRect; 
	GetClientRect(&clientRect); 
	m_protFileListCtrl.GetWindowRect(&listRect); 
	ScreenToClient(&listRect); 
	m_protFileListCtrl.SetWindowPos(NULL,listRect.left,listRect.top,clientRect.right-clientRect.left,clientRect.bottom-listRect.top,SWP_NOMOVE); 
 
	//Add a column 
	LVCOLUMN newColumn; 
	newColumn.mask=LVCF_FMT|LVCF_ORDER|LVCF_TEXT|LVCF_WIDTH; 
	newColumn.fmt=LVCFMT_LEFT; 
 
	newColumn.cx=351; 
	newColumn.iOrder=0; 
	newColumn.pszText="路径"; 
	m_protFileListCtrl.InsertColumn(1,&newColumn); 
 
	newColumn.cx=150; 
	newColumn.iOrder=1; 
	newColumn.pszText="保护方式"; 
	m_protFileListCtrl.InsertColumn(1,&newColumn); 
 
	//Initialize tool tips 
	EnableToolTips(); 
/*		//debug 
	m_toolTip.Create(this); 
	m_toolTip.AddTool(GetDlgItem(IDC_FILE_TREE), IDC_FILE_TREE); 
	TOOLINFO toolInfo; 
	toolInfo.cbSize=sizeof(toolInfo); 
	toolInfo.uFlags=0; //??? 
	toolInfo.hwnd=m_hWnd; 
	toolInfo.uId=IDC_FILE_TREE; 
	toolInfo.hinst=AfxGetResourceHandle(); 
	toolInfo.lpszText=(LPTSTR)IDC_FILE_TREE; 
	toolInfo.rect.left=10; 
	toolInfo.rect.top=10; 
	toolInfo.rect.bottom=30; 
	toolInfo.rect.right=30; 
	m_toolTip.SetToolInfo(&toolInfo); 
	m_toolTip.Activate(TRUE); 
*/ 
 
	//show protected file information. 
	OnShowData(0); 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileGuardView printing 
 
BOOL CFileGuardView::OnPreparePrinting(CPrintInfo* pInfo) 
{ 
	// default preparation 
	return DoPreparePrinting(pInfo); 
} 
 
void CFileGuardView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add extra initialization before printing 
} 
 
void CFileGuardView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add cleanup after printing 
} 
 
void CFileGuardView::OnPrint(CDC* pDC, CPrintInfo* /*pInfo*/) 
{ 
	// TODO: add customized printing code here 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileGuardView diagnostics 
 
#ifdef _DEBUG 
void CFileGuardView::AssertValid() const 
{ 
	CFormView::AssertValid(); 
} 
 
void CFileGuardView::Dump(CDumpContext& dc) const 
{ 
	CFormView::Dump(dc); 
} 
 
CFileGuardDoc* CFileGuardView::GetDocument() // non-debug version is inline 
{ 
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CFileGuardDoc))); 
	return (CFileGuardDoc*)m_pDocument; 
} 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CFileGuardView message handlers 
 
BOOL CFileGuardView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)  
{ 
	// TODO: Add your specialized code here and/or call the base class 
	 
	BOOL bRet =  CFormView::Create(lpszClassName, lpszWindowName, dwStyle, rect, pParentWnd, nID, pContext); 
 
	// this part creates the TreeCtrl and use the CLeftView 
	// as his parent Window 
 
/*	bRet = m_dirTree.Create( WS_CHILD | TVS_LINESATROOT | TVS_HASBUTTONS | WS_VISIBLE | TVS_HASLINES,  
			                     CRect(0, 0, 0, 0), this, IDC_FILE_TREE ); 
 
	if ( bRet ) 
	{ 
		AfxMessageBox("Display tree."); 
		m_dirTree.DisplayTree( NULL, TRUE ); 
	} 
*/ 
	return bRet; 
} 
 
void CFileGuardView::ShowProtectionType(PROTECTED_FILE *pProtFile) 
{ 
	if(pProtFile==NULL) 
		return; 
 
	m_bFileHide=pProtFile->PF_type & PT_HIDE ? 1: 0; 
	m_bFileReadProt=pProtFile->PF_type & PT_READ ? 1:0; 
	m_bFileWriteProt=pProtFile->PF_type & PT_WRITE ? 1:0; 
	m_bFileDeleteProt=pProtFile->PF_type & PT_DELETE ? 1:0; 
	 
	UpdateData(FALSE); 
} 
 
WORD CFileGuardView::GetCurProtectionType() 
{ 
	UpdateData(); 
	 
	return m_bFileHide*PT_HIDE | m_bFileReadProt*PT_READ | m_bFileWriteProt*PT_WRITE | m_bFileDeleteProt*PT_DELETE; 
} 
 
void CFileGuardView::ClearProtectionType() 
{ 
	m_bFileWriteProt=m_bFileReadProt=m_bFileHide=m_bFileDeleteProt=FALSE; 
	UpdateData(FALSE); 
} 
 
LRESULT CFileGuardView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)  
{ 
	LRESULT res=CFormView::WindowProc(message, wParam, lParam); 
 
	static BOOL bPostClick=FALSE; 
	//Todo: make it smarter. 
	if(message==WM_NOTIFY && wParam==IDC_FILE_TREE) 
	{ 
		if(/*((NMHDR *)lParam)->code==TVN_GETINFOTIP ||*/bPostClick==TRUE) 
		{ 
			m_dirTree.UpdateData(TRUE); 
			m_dirTree.ShowItemType(m_dirTree.GetSelectedItem()); 
			bPostClick=FALSE; 
		} 
		else if(((NMHDR *)lParam)->code==NM_CLICK) 
			bPostClick=TRUE; 
	} 
 
	return res; 
} 
 
//Call when user click checkbox on the right side to change protection type. 
void CFileGuardView::OnProtectionTypeChange()  
{ 
	HTREEITEM hItem=m_dirTree.GetSelectedItem(); 
	if( !hItem || !m_dirTree.GetCheck(hItem) ) 
		return; 
	CString strPath=m_dirTree.GetFullPath(hItem); 
	if(strPath.IsEmpty()) 
		return; 
	FGFormatPath(strPath); 
	protFileList.Add(strPath, GetCurProtectionType()); 
 
	//Update data of view. 
	OnShowData(3); 
} 
 
 
void CFileGuardView::OnShowData(UINT id)  
{ 
	AddSpecialProtectedFile(); 
 
	//Display the tree. 
	if(id==0 || id==1) 
		m_dirTree.DisplayTree( NULL, TRUE ); 
 
	//Display protected file in list. 
	if(id==0 || id==2) 
		m_protFileListCtrl.DisplayItem(); 
 
	//Update the visual part of tree. 
	if(id==3) 
	{ 
		m_dirTree.UpdateData(FALSE); 
		m_protFileListCtrl.DisplayItem(); 
	} 
} 
 
//Give toop tip. 
BOOL CFileGuardView::OnToolTip( UINT id, NMHDR * pNMHDR, LRESULT * pResult ) 
{	 
    TOOLTIPTEXT *pTTT = (TOOLTIPTEXT *)pNMHDR; 
 
    UINT nID =pNMHDR->idFrom; 
    if(pTTT->uFlags & TTF_IDISHWND) 
	{ 
		nID = ::GetDlgCtrlID((HWND)nID); 
		 
        if(nID) 
        { 
            pTTT->lpszText = MAKEINTRESOURCE(nID); 
            pTTT->hinst = AfxGetResourceHandle(); 
            return(TRUE); 
        } 
	} 
	 
    return(FALSE); 
}