⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 attributesdlg.cpp

📁 这些源代码
💻 CPP
字号:
// AttributesDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Attributes.h"
#include "AttributesDlg.h"
#include "about.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define 	LB_DRIVEITEM	0x4000
#define 	LB_DIRITEM		0x0010
#define 	LB_FILEITEM 	0x002f
#define 	LB_EXCLUSIVE	0x8000
const char	*LB_WILDCARD = "*.*";

/////////////////////////////////////////////////////////////////////////////
// CAttributesDlg dialog

CAttributesDlg::CAttributesDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAttributesDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAttributesDlg)
	m_bArchive = FALSE;
	m_bHidden = FALSE;
	m_bReadOnly = FALSE;
	m_bSystem = FALSE;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_strCurrentFile = _T("");
}

void CAttributesDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAttributesDlg)
	DDX_Control(pDX, IDC_CURRENT_FILE, m_ctlCurrentFile);
	DDX_Control(pDX, IDC_LIST_DIRECTORY, m_listDirectory);
	DDX_Check(pDX, IDC_CHECK_ARCHIVE, m_bArchive);
	DDX_Check(pDX, IDC_CHECK_HIDDEN, m_bHidden);
	DDX_Check(pDX, IDC_CHECK_READONLY, m_bReadOnly);
	DDX_Check(pDX, IDC_CHECK_SYSTEM, m_bSystem);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAttributesDlg, CDialog)
	//{{AFX_MSG_MAP(CAttributesDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK_ARCHIVE, OnCheckArchive)
	ON_BN_CLICKED(IDC_CHECK_HIDDEN, OnCheckHidden)
	ON_BN_CLICKED(IDC_CHECK_READONLY, OnCheckReadonly)
	ON_BN_CLICKED(IDC_CHECK_SYSTEM, OnCheckSystem)
	ON_LBN_SELCHANGE(IDC_LIST_DIRECTORY,
                      OnSelchangeListDirectory)
	ON_LBN_DBLCLK(IDC_LIST_DIRECTORY, OnDblclkListDirectory)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAttributesDlg message handlers

BOOL CAttributesDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//	when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE); 		// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here

	int nBytes = GetCurrentDirectory (0, NULL);
	char *szDir = new char [nBytes + 1];
	GetCurrentDirectory (nBytes + 1, szDir);
	m_strCurrentPath = szDir;
	delete [] szDir;

	RECT rc = {0, 0, 0, 0};
	CWnd *cwStaticEx;
	if ((cwStaticEx = GetDlgItem(IDC_CURRENT_FILE)) != NULL)
	{
		cwStaticEx->GetWindowRect(&rc);
		rc.right = rc.right - rc.left;
		rc.bottom = rc.bottom - rc.top;
		rc.top = 0;
		rc.left = 0;
		m_ctlCurrentFile.m_ctlRect = rc;
	}

	LoadList ();
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CAttributesDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//	to draw the icon.  For MFC applications using the document/view model,
//	this is automatically done for you by the framework.

void CAttributesDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//	the minimized window.
HCURSOR CAttributesDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CAttributesDlg::OnCheckArchive() 
{
	m_bArchive = m_bArchive ? FALSE : TRUE;
}
void CAttributesDlg::OnCheckHidden() 
{
	m_bHidden = m_bHidden ? FALSE : TRUE;
}

void CAttributesDlg::OnCheckReadonly() 
{
	m_bReadOnly = m_bReadOnly ? FALSE : TRUE;
}

void CAttributesDlg::OnCheckSystem() 
{
	m_bSystem = m_bSystem ? FALSE : TRUE;
}

void CAttributesDlg::OnSelchangeListDirectory()
{
CString strDir;
CFileStatus fs;

	int nIndex = m_listDirectory.GetCurSel ();
	m_listDirectory.GetText (nIndex, strDir);
	DWORD dwData = m_listDirectory.GetItemData (nIndex);
	switch (dwData)
	{
		case LB_DRIVEITEM:
			m_strCurrentFile.Empty ();
			m_ctlCurrentFile.SetWindowText (_T(""));
			fs.m_attribute = 0;
			break;
		case LB_DIRITEM:
			strDir.Delete (0);
			strDir.Delete (strDir.GetLength() - 1);
		default:
			CFile::GetStatus ((LPCTSTR) strDir, fs);
			m_strCurrentFile = fs.m_szFullName;
			m_ctlCurrentFile.SetWindowText ((LPCSTR) m_strCurrentFile);
			break;
	}
	m_ctlCurrentFile.Invalidate (TRUE);
	m_bHidden = fs.m_attribute & FILE_ATTRIBUTE_HIDDEN ? TRUE : FALSE;
	m_bArchive = fs.m_attribute & FILE_ATTRIBUTE_ARCHIVE ? TRUE : FALSE;
	m_bSystem = fs.m_attribute & FILE_ATTRIBUTE_SYSTEM ? TRUE : FALSE;
	m_bReadOnly = fs.m_attribute & FILE_ATTRIBUTE_READONLY ? TRUE : FALSE;
	UpdateData (FALSE);
}

void CAttributesDlg::LoadList()
{
int nCount;
int i;

	nCount = m_listDirectory.GetCount();
	while (nCount > 0)
	{
		nCount = m_listDirectory.DeleteString (nCount-1);
	}
	m_listDirectory.Dir (LB_DRIVEITEM | LB_EXCLUSIVE, LB_WILDCARD);
	m_listDirectory.Dir (LB_DIRITEM | LB_EXCLUSIVE, LB_WILDCARD);
	m_listDirectory.Dir (LB_FILEITEM | LB_EXCLUSIVE, LB_WILDCARD);
	nCount = m_listDirectory.GetCount ();
	for (i = 0; i < nCount; ++i)
	{
	CString strItem;

		m_listDirectory.GetText (i, strItem);
		if (strItem.GetAt(0) == '[')
		{
			if (strItem.GetAt(1) == '-')	// It's a drive
			{
				m_listDirectory.SetItemData (i, LB_DRIVEITEM);
			}
			else
			{
				m_listDirectory.SetItemData (i, LB_DIRITEM);
			}
		}
		else
		{
			m_listDirectory.SetItemData (i, 0x0000);
		}
	}
}

void CAttributesDlg::OnOK() 
{
CString strFileName;

	UpdateData (TRUE);
	int nIndex = m_listDirectory.GetCurSel ();
	if (nIndex < 0)
		return;
	DWORD dwData = m_listDirectory.GetItemData (nIndex);
	m_listDirectory.GetText (nIndex, strFileName);
	switch (dwData)
	{
		case LB_DRIVEITEM:
			AfxMessageBox (_T("Cannot change drive attributes"), MB_ICONSTOP);
			break;
		case LB_DIRITEM:
			strFileName.Delete (0);
			strFileName.Delete (strFileName.GetLength() - 1);
			dwData = 0;
			dwData |= m_bHidden ? FILE_ATTRIBUTE_HIDDEN : 0;
			dwData |= m_bArchive ? FILE_ATTRIBUTE_ARCHIVE : 0;
			dwData |= m_bSystem ? FILE_ATTRIBUTE_SYSTEM : 0;
			dwData |= m_bReadOnly ? FILE_ATTRIBUTE_READONLY : 0;
			if (!SetFileAttributes (strFileName, dwData))
				AfxMessageBox (_T("Directory attribute change failed"), MB_ICONSTOP);
			break;
		default:
			CFileStatus fs;
			CFile::GetStatus ((LPCTSTR) strFileName, fs);
			fs.m_attribute &= ~(FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_READONLY);
			fs.m_attribute |= m_bHidden ? FILE_ATTRIBUTE_HIDDEN : 0;
			fs.m_attribute |= m_bArchive ? FILE_ATTRIBUTE_ARCHIVE : 0;
			fs.m_attribute |= m_bSystem ? FILE_ATTRIBUTE_SYSTEM : 0;
			fs.m_attribute |= m_bReadOnly ? FILE_ATTRIBUTE_READONLY : 0;
			TRY
			{
				CFile::SetStatus (fs.m_szFullName, fs);
			}
			CATCH (CFileException, e)
			{
				char errmes[_MAX_PATH];
				memset (errmes, '\0', _MAX_PATH);
				e->GetErrorMessage (errmes, _MAX_PATH);
				CString Message = _T("Set attribute call failed\n");
				Message += errmes;
				AfxMessageBox (Message, MB_ICONSTOP);
			}
			END_CATCH
			break;
	}
}

void CAttributesDlg::OnDblclkListDirectory() 
{
CString strDir;

	int nIndex = m_listDirectory.GetCurSel ();
	DWORD dwData = m_listDirectory.GetItemData (nIndex);
	m_listDirectory.GetText (nIndex, strDir);
	switch (dwData)
	{
		case LB_DRIVEITEM:
			strDir.Delete (0, 2);
			strDir.Delete (strDir.GetLength() - 2, 2);
			strDir += ':';
			if (!SetCurrentDirectory ((LPCSTR) strDir))
			{
				AfxMessageBox (_T("Drive change failed"), MB_ICONSTOP);
				return;
			}
			break;
		case LB_DIRITEM:
			strDir.Delete (0);
			strDir.Delete (strDir.GetLength() - 1);
			if (!SetCurrentDirectory ((LPCSTR) strDir))
			{
				AfxMessageBox (_T("Directory change failed"), MB_ICONSTOP);
				return;
			}
			break;
		default:
			return;
	}
	LoadList ();
	int nBytes = GetCurrentDirectory (0, NULL);
	char *szDir = new char [nBytes + 1];
	GetCurrentDirectory (nBytes + 1, szDir);
	m_strCurrentPath = szDir;
	delete [] szDir;
	m_listDirectory.SetCurSel (-1);
	m_bArchive = FALSE;
	m_bHidden = FALSE;
	m_bReadOnly = FALSE;
	m_bSystem = FALSE;
	UpdateData (FALSE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -