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

📄 changeformatdlg.cpp

📁 自己写的文件对话框操作程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ChangeFormatDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ChangeFormat.h"
#include "ChangeFormatDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChangeFormatDlg dialog

CChangeFormatDlg::CChangeFormatDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChangeFormatDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChangeFormatDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	//m_szFullPath = _T("");
	m_szFileName = _T("");
	m_szFileSrcName = _T("");
	m_szFileSaveName = _T("");
	m_szExt = _T("");
	m_bModified = FALSE;

	// 初始化 3D 数据
	m_TD_Channal.m_p3DData = NULL;
	m_TD_Channal.m_3DPointNum = NULL;
}

CChangeFormatDlg::~CChangeFormatDlg()
{
	if (m_TD_Channal.m_3DPointNum != NULL)
	{
		delete [] (m_TD_Channal.m_3DPointNum);
		m_TD_Channal.m_3DPointNum = NULL;
	}
	
	if (m_TD_Channal.m_p3DData != NULL)
	{
		for (int i = 0;i < m_TD_Channal.m_3DRealCurveNum;i++)
		{
			delete [] (m_TD_Channal.m_p3DData[i]);
			m_TD_Channal.m_p3DData[i] = NULL;
		}
		delete [] (m_TD_Channal.m_p3DData);
		m_TD_Channal.m_p3DData = NULL;
	}
}

void CChangeFormatDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChangeFormatDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChangeFormatDlg, CDialog)
	//{{AFX_MSG_MAP(CChangeFormatDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChangeFormatDlg message handlers

BOOL CChangeFormatDlg::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
	// 获得应用程序路径
// 	TCHAR path[MAX_PATH];
// 	GetModuleFileName(NULL, path, MAX_PATH);
// 	m_szFullPath = path;

	// 获得菜单指针
	m_pMenu = GetSystemMenu(FALSE);

	GetDlgItem(IDCANCEL)->EnableWindow(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CChangeFormatDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else if (nID == SC_CLOSE)
	{
		CDialog::OnCancel();
	}
	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 CChangeFormatDlg::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 CChangeFormatDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CChangeFormatDlg::OnOK() 
{
	// TODO: Add extra validation here

	CString newName = m_szFileSrcName;
	// 文件打开对话框
	if (!DoPromptFileName(newName, AFX_IDS_OPENFILE,
		OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, TRUE))
		return;

	// 记录打开的文件名(包含文件全路径)
	m_szFileSrcName = newName;

	SetDlgItemText(IDC_SRC_EDIT, newName);

	// 获得文件相关信息
	char drive[MAX_PATH];
	char dir[MAX_PATH];
	char fname[MAX_PATH];
	char ext[MAX_PATH];
	
	memset(drive, 0, MAX_PATH);
	memset(dir, 0, MAX_PATH);
	memset(fname, 0, MAX_PATH);
	memset(ext, 0, MAX_PATH);
	
	_splitpath(newName, drive, dir, fname, ext);

	// 记录文件后缀
	m_szFileName = fname;
	m_szExt = ext;

	if (!OnOpenDocument(newName))
	{
		// user has be alerted to what failed in OnOpenDocument
		TRACE0("CDocument::OnOpenDocument returned FALSE.\n");
		return;
	}

	// 设置文件路径
	//SetPathName(newName);
	
	GetDlgItem(IDCANCEL)->EnableWindow();
	// success	
	
	//CDialog::OnOK();
}

void CChangeFormatDlg::OnCancel() 
{
	// TODO: Add extra cleanup here	
	// 文件保存对话框
	CString newName = _T("");

	if (!m_szFileSaveName.IsEmpty())
	{
		newName = m_szFileSaveName;
		newName += m_szFileName;
		newName += m_szExt;
	}
	else
	{
		newName = m_szFileSrcName;
	}

	if (!DoPromptFileName(newName, AFX_IDS_SAVEFILE,
		OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT, FALSE))
		return;       // don't even attempt to save


	CWaitCursor wait;

	char drive[MAX_PATH];
	char dir[MAX_PATH];
	char fname[MAX_PATH];
	char ext[MAX_PATH];
	
	memset(drive, 0, MAX_PATH);
	memset(dir, 0, MAX_PATH);
	memset(fname, 0, MAX_PATH);
	memset(ext, 0, MAX_PATH);
	
	_splitpath(newName, drive, dir, fname, ext);

	// 记录保存文件路径
	m_szFileSaveName = dir;
	SetDlgItemText(IDC_TARGET_EDIT, newName);

	CString lpszPathName = _T("");
	if (!OnSaveDocument(newName))
	{
		if (lpszPathName == NULL)
		{
			// be sure to delete the file
			TRY
			{
				CFile::Remove(newName);
			}
			CATCH_ALL(e)
			{
				TRACE0("Warning: failed to delete file after failed SaveAs.\n");
				DELETE_EXCEPTION(e);
			}
			END_CATCH_ALL
		}
		return;
	}
	
	SetPathName(newName);
	
	// success

	// CDialog::OnCancel();
}

// 打开文件
BOOL CChangeFormatDlg::OnOpenDocument(LPCTSTR lpszPathName)
{
// 	if (IsModified())
// 		TRACE0("Warning: OnOpenDocument replaces an unsaved document.\n");
	
	CFileException fe;
	CFile* pFile = new CFile;
	pFile->Open(lpszPathName, CFile::modeRead|CFile::shareDenyWrite, &fe);
	if (pFile == NULL)
	{
		ReportSaveLoadException(lpszPathName, &fe,
			FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		return FALSE;
	}
	
	SetModifiedFlag();  // dirty during de-serialize
	
	CArchive loadArchive(pFile, CArchive::load | CArchive::bNoFlushOnDelete);
	//loadArchive.m_pDocument = NULL;
	loadArchive.m_bForceFlat = FALSE;
	TRY
	{
		CWaitCursor wait;
		if (pFile->GetLength() != 0)
			Serialize(loadArchive);     // load me
		loadArchive.Close();
		ReleaseFile(pFile, FALSE);
	}
	CATCH_ALL(e)
	{
		ReleaseFile(pFile, TRUE);
		
		TRY
		{
			ReportSaveLoadException(lpszPathName, e,
				FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
		}
		END_TRY
			DELETE_EXCEPTION(e);
		return FALSE;
	}
	END_CATCH_ALL
		
		SetModifiedFlag(FALSE);     // start off with unmodified
	
	return TRUE;
}

// 文件对话框
BOOL CChangeFormatDlg::DoPromptFileName(CString& fileName, UINT nIDSTitle, DWORD lFlags, BOOL bOpenFileDialog)
{
	CFileDialog dlgFile(bOpenFileDialog);
	
	CString title;
	VERIFY(title.LoadString(nIDSTitle));
	
	dlgFile.m_ofn.Flags |= lFlags;
	
	CString strFilter;
	CString strDefault;
	if (bOpenFileDialog == TRUE)
	{
		// 打开文件
		strFilter.LoadString(IDS_STR_ALL_FILE_EXT);
		
		if (m_szExt.IsEmpty())
		{
			dlgFile.m_ofn.lpstrDefExt = _T(".spw");
			dlgFile.m_ofn.nFilterIndex = 1;  // 1 based number
		}
		else
		{
			dlgFile.m_ofn.lpstrDefExt = m_szExt;
			if (/*_stricmp(ext, ".spw") == 0*/m_szExt.CompareNoCase(_T(".spw")) == 0)
			{
				dlgFile.m_ofn.nFilterIndex = 1;
			}
			else if (/*_stricmp(ext, ".stm") == 0*/m_szExt.CompareNoCase(_T(".stm")) == 0)
			{
				dlgFile.m_ofn.nFilterIndex = 2;
			}
			else if (/*_stricmp(ext, ".sph") == 0*/m_szExt.CompareNoCase(_T(".sph")) == 0)
			{
				dlgFile.m_ofn.nFilterIndex = 3;
			}
			else if (/*_stricmp(ext, ".spd") == 0*/m_szExt.CompareNoCase(_T(".spd")) == 0)
			{
				dlgFile.m_ofn.nFilterIndex = 4;
			}
		}
		dlgFile.m_ofn.nMaxCustFilter = 5;
	}
	else
	{
		// 保存文件
		int index = 0;

		// 比较文件类型
		if (/*_stricmp(ext, ".spw") == 0*/m_szExt.CompareNoCase(_T(".spw")) == 0)
		{
			index = 0;
		}
		else if (/*_stricmp(ext, ".stm") == 0*/m_szExt.CompareNoCase(_T(".stm")) == 0)
		{
			index = 1;
		}
		else if (/*_stricmp(ext, ".sph") == 0*/m_szExt.CompareNoCase(_T(".sph")) == 0)
		{
			index = 2;
		}
		else if (/*_stricmp(ext, ".spd") == 0*/m_szExt.CompareNoCase(_T(".spd")) == 0)
		{
			index = 3;
		}

		strFilter.LoadString(IDS_STR_WL_FILE_EXT + index);
		dlgFile.m_ofn.lpstrDefExt = strFilter;
		dlgFile.m_ofn.nFilterIndex = 1;  // 1 based number
		dlgFile.m_ofn.nMaxCustFilter = 2;
	}
	
	// append the "*.*" all files filter
	CString allFilter;
	VERIFY(allFilter.LoadString(AFX_IDS_ALLFILTER));
	strFilter += allFilter;
	strFilter += (TCHAR)'\0';   // next string please
	strFilter += _T("*.*");
	strFilter += (TCHAR)'\0';   // last string
	
	if (!strFilter.IsEmpty())
	{
		LPTSTR pch = strFilter.GetBuffer(0); // modify the buffer in place
		// MFC delimits with '|' not '\0'
		while ((pch = _tcschr(pch, '|')) != NULL)
			*pch++ = '\0';
		dlgFile.m_ofn.lpstrFilter = strFilter;
		// do not call ReleaseBuffer() since the string contains '\0' characters
	}

	dlgFile.m_ofn.lpstrTitle = title;
	dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
	
	int nResult = dlgFile.DoModal();
	fileName.ReleaseBuffer();
	return nResult == IDOK;

}

// 保存文件
BOOL CChangeFormatDlg::OnSaveDocument(LPCTSTR lpszPathName)
{
	CFileException fe;
	CFile* pFile = NULL;
	pFile = new CFile();
	pFile->Open(lpszPathName, CFile::modeCreate |
		CFile::modeReadWrite | CFile::shareExclusive, &fe);
	
	if (pFile == NULL)
	{
		ReportSaveLoadException(lpszPathName, &fe,
			TRUE, AFX_IDP_INVALID_FILENAME);
		return FALSE;
	}
	
	CArchive saveArchive(pFile, CArchive::store | CArchive::bNoFlushOnDelete);
	//saveArchive.m_pDocument = this;
	saveArchive.m_bForceFlat = FALSE;
	TRY
	{
		CWaitCursor wait;
		Serialize(saveArchive);     // save me
		saveArchive.Close();
		ReleaseFile(pFile, FALSE);
	}
	CATCH_ALL(e)
	{
		ReleaseFile(pFile, TRUE);
		
		TRY
		{
			ReportSaveLoadException(lpszPathName, e,
				TRUE, AFX_IDP_FAILED_TO_SAVE_DOC);
		}
		END_TRY
			DELETE_EXCEPTION(e);
		return FALSE;
	}
	END_CATCH_ALL
	
	return TRUE;        // success
}

⌨️ 快捷键说明

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