www.pudn.com > 数控仿真与网络控制系统.rar > MainFrm.cpp


// MainFrm->cpp : implementation of the CMainFrame class 
// 
 
#include "stdafx.h" 
#include "NoteXpad.h" 
#include "FilePropertiesDlg.h" 
#include "ImageStatusBar.h" 
#include "InsertTimeDateDialog.h" 
#include "NoteXpadReplaceDlg.h" 
#include "MainFrm.h" 
#include "FindDialog.h" 
#include "NoteXpadView.h" 
#include "OptionsDlg.h" 
#include "Compiledialog.h" 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
extern CString CURRENT_FILE; 
static TCHAR BASED_CODE szZOrder[] = _T("ZOrder"); 
#define STAYONTOP_KEY "ZOrder" 
#define MY_REG_KEY "NoteXpad" 
#define SETTINGS_KEY "Settings" 
#define WINDOWPLACEMENT_KEY "Window Placement" 
extern BOOL CHANGED; 
extern BOOL TOP_AND_LEFT; 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame 
 
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd) 
 
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd) 
	//{{AFX_MSG_MAP(CMainFrame) 
	ON_WM_CREATE() 
	ON_COMMAND(ID_CHANGEFONT, OnChangefont) 
	ON_COMMAND(ID_FIND, OnFind) 
	ON_WM_TIMER() 
	ON_COMMAND(ID_FILE_PROPERTIES, OnFileProperties) 
	ON_WM_CLOSE() 
	ON_WM_SHOWWINDOW() 
	ON_COMMAND(ID_STAYONTOP_YES, OnUpdateZOrder) 
	ON_WM_DROPFILES() 
	ON_COMMAND(ID_SELECT_ALL, OnSelectAll) 
	ON_COMMAND(ID_INSERTTIMEDATE, OnInserttimedate) 
	ON_UPDATE_COMMAND_UI(ID_CHANGECOLOR, OnUpdateChangecolor) 
	ON_COMMAND(ID_OPTIONS, OnOptions) 
	ON_COMMAND(ID_CHARMAP, OnCharmap) 
	ON_COMMAND(ID_NEWWINDOW, OnNewWindow) 
	ON_COMMAND(ID_REPLACE, OnReplace) 
	ON_COMMAND(ID_COMPILE_TIAOSHI, OnCompileTiaoshi) 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
static UINT indicators[] ={ID_SEPARATOR,ID_BLANKPANEL1,ID_BLANKPANEL,ID_BLANKPANEL2,};// status line indicator 
UINT bitmaps[] = { 
		ISB_NOIMAGE, 
		ISB_NOIMAGE, 
		ISB_NOIMAGE, 
}; 
int Alignments[] = { 
		ISB_NOIMAGE, 
		ISB_NOIMAGE, 
		ISB_LEFTALIGN, 
}; 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame construction/destruction 
void CMainFrame::OnTimer(UINT nIDEvent)  
{	 
	if (nIDEvent == 2) 
	{ 
		CFrameWnd* pMainWnd =(CFrameWnd*)AfxGetMainWnd(); 
		CEdit* theView = (CEdit*)pMainWnd->GetActiveView(); 
		CNoteXpadView* nView = (CNoteXpadView*)pMainWnd->GetActiveView(); 
		CString sizeofit; 
		theView->GetWindowText(sizeofit); 
 
		//Finds Line/Column position of Caret in view 
	 
		int begin; 
		int end; 
		CString LineAndColumn; 
		theView->GetSel(begin,end); 
 
		UINT linenum = theView->SendMessage(EM_LINEFROMCHAR,begin,0) +1; 
		UINT columnnum = theView->SendMessage(EM_LINEINDEX,-1,0); 
		 
		long column = begin - columnnum +1; 
		 
		if (column > 1000000) //I added this because 
		{					  //the column number is reported as 15 million+ if 
							  //print preview is currently displayed =[ 
			m_wndStatusBar.SetPaneText(1,"\t(Preview)",FALSE); 
			m_wndStatusBar.SendMessage(WM_PAINT); 
			return; 
		} 
		LineAndColumn.Format("\tLn: %d / Col: %d",linenum,column); 
		 
		int size = sizeofit.GetLength(); 
		CString selText; 
		selText = nView->GetSelectedTxt(); 
		 
		if (!selText.GetLength() ==0) 
			{ 
				if (sizeofit.GetLength() > 1024) 
					{	 
						int remainder = sizeofit.GetLength() % 1000; 
						int selKB = selText.GetLength() % 1000; 
						sizeofit.Format("\t(%d.%d/%d.%d KB)",selText.GetLength()/1000,selKB, sizeofit.GetLength() / 1000,remainder); 
						m_wndStatusBar.SetPaneText(2,sizeofit,FALSE); 
						m_wndStatusBar.SetPaneText(1,"\t(Selection)",FALSE); 
						m_wndStatusBar.SendMessage(WM_PAINT); 
						return; 
					} 
 
				CString selbytes; 
				selbytes.Format("\t(%d/%d Bytes)",selText.GetLength(),size); 
				m_wndStatusBar.SetPaneText(2,selbytes,FALSE); 
				m_wndStatusBar.SetPaneText(1,"\t(Selection)",FALSE); 
				m_wndStatusBar.SendMessage(WM_PAINT); 
				return; 
			} 
				m_wndStatusBar.SetPaneText(1,LineAndColumn,FALSE); 
 
		 if (!CHANGED) //Set status panel according to status of file; saved or otherwise 
			{ 
				m_wndStatusBar.SetPaneText(3,"\t   [SAVED]",FALSE); 
				m_wndStatusBar.SetBitmap(3,ID_DISKCHECK,ISB_LEFTALIGN); 
			} 
			else 
			{ 
				m_wndStatusBar.SetPaneText(3,"\t     [UNSAVED]",FALSE); 
				m_wndStatusBar.SetBitmap(3,ID_DISKXED,ISB_LEFTALIGN); 
			} 
	 
	 
 
	if (sizeofit.GetLength() > 1024) 
	{	 
		int remainder = sizeofit.GetLength() % 1000; 
		sizeofit.Format("\t%d.%d KBytes.", sizeofit.GetLength() / 1000,remainder); 
		m_wndStatusBar.SetPaneText(2,sizeofit,FALSE); 
		m_wndStatusBar.SendMessage(WM_PAINT); 
		return; 
	} 
	 
	if (sizeofit.GetLength() ==1) 
	{ 
		sizeofit.Format("\t%d Byte.",size); 
		m_wndStatusBar.SetPaneText(2,sizeofit,FALSE); 
		m_wndStatusBar.SendMessage(WM_PAINT); 
		return; 
	} 
	 
	sizeofit.Format(_T("\t%d Bytes."),size); 
	m_wndStatusBar.SetPaneText(2,sizeofit,FALSE); 
 
	 
	if (size == 0) 
	{ 
		//The view has no text, disable Select All menu item: 
		m_wndStatusBar.SetPaneText(2,"\t(Empty)",FALSE); 
		m_wndStatusBar.SetPaneText(3,"\tEmpty Buffer",FALSE); 
		m_wndStatusBar.SetBitmap(3,ISB_NOIMAGE,ISB_NOIMAGE); 
		m_wndStatusBar.SendMessage(WM_PAINT); 
		CFrameWnd::OnTimer(nIDEvent); 
		return; 
	} 
 
	 
	m_wndStatusBar.SendMessage(WM_PAINT); 
	CFrameWnd::OnTimer(nIDEvent); 
 
} 
} 
CMainFrame::CMainFrame() 
{ 
	 
} 
 
CMainFrame::~CMainFrame() 
{ 
} 
 
 
 
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1) 
		return -1; 
	 
	if (!m_wndToolBar.CreateEx(this) || 
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME)) 
	{ 
		TRACE0("Failed to create toolbar\n"); 
		return -1;      // fail to create 
	} 
 
 
	if (!m_wndReBar.Create(this) || 
		!m_wndReBar.AddBar(&m_wndToolBar)) 
	{ 
		TRACE0("Failed to create rebar\n"); 
		return -1;      // fail to create 
	} 
		m_wndStatusBar.SetBitmaps(bitmaps,Alignments); 
 
	if (!m_wndStatusBar.Create(this) || 
		!m_wndStatusBar.SetIndicators(indicators, 
		  sizeof(indicators)/sizeof(UINT))) 
	{ 
		TRACE0("Failed to create status bar\n"); 
		return -1;      // fail to create 
	} 
 
	// TODO: Remove this if you don't want tool tips 
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() | 
		CBRS_TOOLTIPS | CBRS_FLYBY); 
 
	//Pull value from registry that contains either 
	//a 1 or 0, this value represents wether or not 
	//the main window will be 'StayOnTop' or not. 
	LoadBarState("Control Bar States"); 
	WINDOWPLACEMENT *pWP; 
	UINT size=sizeof(WINDOWPLACEMENT); 
	CWinApp* pApp = (CWinApp*)AfxGetApp(); 
	if (pApp->GetProfileBinary(SETTINGS_KEY,WINDOWPLACEMENT_KEY,(LPBYTE*)&pWP,&size)) 
	{ 
		 
		SetWindowPlacement(pWP); 
		delete []pWP; 
 
	} 
    if (TOP_AND_LEFT) 
	{ 
	CRect rect; 
	GetWindowRect(&rect); 
 
	rect.top = pApp->GetProfileInt("Settings","Top",0); 
	rect.left = pApp->GetProfileInt("Settings","Left",0); 
	SetWindowPos(NULL,rect.left,rect.top,rect.right,rect.bottom,SWP_NOSIZE); 
	} 
	int stayontop = GetZOrderFromRegistry(); 
 
	SetZOrder(stayontop); 
 
	CheckStayOnTopMenu(stayontop); 
	 
	return 0; 
} 
 
BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) 
{ 
	 
	if( !CFrameWnd::PreCreateWindow(cs) ) 
		return FALSE; 
	// TODO: Modify the Window class or styles here by modifying 
	//  the CREATESTRUCT cs 
	 
	return TRUE; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame diagnostics 
 
#ifdef _DEBUG 
void CMainFrame::AssertValid() const 
{ 
	CFrameWnd::AssertValid(); 
} 
 
void CMainFrame::Dump(CDumpContext& dc) const 
{ 
	CFrameWnd::Dump(dc); 
} 
 
#endif //_DEBUG 
 
///////////////////////////////////////////////////////////////////////////// 
// CMainFrame message handlers 
 
 
void CMainFrame::OnChangefont()  
{ 
	 
		CNoteXpadView* theView = (CNoteXpadView*)GetActiveView(); 
		theView->OnChangeFont(); 
	 
} 
 
 
void CMainFrame::OnFind()  
{ 
	CNoteXpadFindDialog * pDlg; 
	pDlg = new CNoteXpadFindDialog; 
	pDlg->Create(IDD_FINDDIALOG); 
	pDlg->CenterWindow(); 
	pDlg->OnInitializeFields(); 
	pDlg->ShowWindow(SW_SHOW); 
 
} 
	 
void CMainFrame::SaveZOrder(int val) 
{ 
	//This function saves to the registry the value 
	//pertaining to the Stay On Top functionality 
	//of our window 
	CWinApp* pApp= (CWinApp*)AfxGetApp(); 
	pApp->WriteProfileInt(STAYONTOP_KEY,szZOrder,val); 
 
} 
 
 
void CMainFrame::CheckStayOnTopMenu(int val) 
{ 
	//This function checks the menu accordingly 
	//to the value sent to it. 
	CMenu *theMenu; 
	CMenu *subMenu; 
	MENUITEMINFO info; 
	theMenu = GetMenu(); 
	subMenu = theMenu->GetSubMenu(2); 
	info.cbSize = sizeof (MENUITEMINFO);  
	info.fMask = MIIM_STATE;              
	VERIFY(subMenu->GetMenuItemInfo(ID_STAYONTOP_YES, &info)); 
 
   if (val ==0) 
   { 
      subMenu->CheckMenuItem(ID_STAYONTOP_YES, MF_UNCHECKED | MF_BYCOMMAND); 
	  } 
   else 
   { 
      subMenu->CheckMenuItem(ID_STAYONTOP_YES, MF_CHECKED | MF_BYCOMMAND); 
	} 
 
} 
 
 
 
void CMainFrame::OnFileProperties()  
{ 
	CFileStatus status; 
	if (!CFile::GetStatus(CURRENT_FILE,status)) 
	{ 
		MessageBox("Error: No file to show properties for!","NoteXpad - Error",MB_OK|MB_ICONEXCLAMATION); 
		return; 
	} 
 
	CFile file; 
	file.Open(CURRENT_FILE,CFile::modeRead); 
	CFilePropertiesDlg* pDialog; 
	pDialog = new CFilePropertiesDlg(this); 
	CString FileSize; 
	CTime Accessed; 
	CTime Created; 
	CTime Modified; 
	CString caption; 
 
 
	FileSize.Format("%d Bytes",file.GetLength()); 
	caption.Format(" NoteXpad - Properties for: %s",file.GetFileName()); 
	pDialog->m_FileName = file.GetFileName(); 
	pDialog->m_FileSize = FileSize; 
	pDialog->m_ParentDirectory = PathFromAbsolute(file.GetFilePath());  
 
	Accessed = status.m_atime; 
	Created = status.m_ctime; 
	Modified = status.m_mtime; 
	 
 
	pDialog->m_DateAccessed =Accessed.Format("%m/%d/%y"); 
	pDialog->m_DateCreated = Created.Format("%m/%d/%y - %H:%M:%S"); 
	pDialog->m_DateModified = Modified.Format("%m/%d/%y - %H:%M:%S"); 
	pDialog->Create(IDD_FILE_PROP_DIALOG); 
	pDialog->CenterWindow(); 
	pDialog->SetWindowText(caption); 
	pDialog->ShowWindow(SW_SHOW); 
} 
 
 
CString CMainFrame::PathFromAbsolute(CString AbsolutePath) 
{ 
	//Takes an Absolute path to a file on disk in CString format 
	//and removes the filename from the string. Returns a CString  
	//containing the parent folder of the file. - Ryan Lederman - 
 
	int slash = AbsolutePath.ReverseFind('\\'); 
	return AbsolutePath.Left(++slash); 
 
} 
 
void CMainFrame::OnClose()  
{ 
 
	//Save control bar states: 
	CFrameWnd::SaveBarState("Control Bar States"); 
	WINDOWPLACEMENT windowPlacement; 
 
	GetWindowPlacement(&windowPlacement); 
 
	if (windowPlacement.showCmd != SW_SHOWMINIMIZED) 
		AfxGetApp()->WriteProfileBinary(SETTINGS_KEY,WINDOWPLACEMENT_KEY,(BYTE*)&windowPlacement,sizeof(WINDOWPLACEMENT)); 
 
	CFrameWnd::OnClose(); 
} 
 
void CMainFrame::OnShowWindow(BOOL bShow, UINT nStatus)  
{ 
	CFrameWnd::OnShowWindow(bShow, nStatus); 
	 
 
	 
} 
 
 
void CMainFrame::OnUpdateZOrder()  
{ 
	CMenu *theMenu; 
	CMenu *subMenu; 
	//UINT menuStatus; 
	MENUITEMINFO info; 
	theMenu = GetMenu(); 
	subMenu = theMenu->GetSubMenu(2); 
   info.cbSize = sizeof (MENUITEMINFO);  
   info.fMask = MIIM_STATE;              
   VERIFY(subMenu->GetMenuItemInfo(ID_STAYONTOP_YES, &info)); 
 
   if (info.fState & MF_CHECKED) 
   { 
	  SaveZOrder(0); 
	  SetZOrder(0); 
      subMenu->CheckMenuItem(ID_STAYONTOP_YES, MF_UNCHECKED | MF_BYCOMMAND); 
	  } 
   else 
   { 
	  SaveZOrder(1); 
	  SetZOrder(1); 
      subMenu->CheckMenuItem(ID_STAYONTOP_YES, MF_CHECKED | MF_BYCOMMAND); 
	  } 
} 
 
int CMainFrame::GetZOrderFromRegistry() 
{ 
	//This function pulls the values from  
	//The registry that pertain to the Stay On Top 
	//functionality of our window 
	CWinApp* pApp= (CWinApp*)AfxGetApp(); 
	return pApp->GetProfileInt(STAYONTOP_KEY,szZOrder,0); 
 
} 
	 
void CMainFrame::SetZOrder(int val) 
{ 
	//CFrameWnd* theFrame = (CFrameWnd*)AfxGetMainWnd(); 
	HWND hWnd = (HWND)GetSafeHwnd(); 
	RECT theRect; 
	GetWindowRect(&theRect); 
	//This function actually sets the Z-axis position of our window 
	if (val == 0) 
	{ 
		//Not stayontop 
		SetWindowPos(&wndNoTopMost, theRect.left , theRect.top, theRect.right , theRect.bottom,SWP_NOSIZE); 
	} 
	else 
	{ 
		//Stayontop 
		SetWindowPos(&wndTopMost,theRect.left , theRect.top, theRect.right , theRect.bottom,SWP_NOSIZE); 
	} 
 
 
} 
 
void CMainFrame::OnDropFiles(HDROP hDropInfo)  
{ 
	SetForegroundWindow(); 
	 
	UINT size=0; 
	char buff[256]; 
	DragQueryFile(hDropInfo,0,buff,size); 
	CURRENT_FILE = buff; 
	CFrameWnd::OnDropFiles(hDropInfo); 
} 
 
void CMainFrame::OnSelectAll()  
{ 
 
	CEdit* theView =(CEdit*)GetActiveView(); 
	 
	theView->SetSel(0,theView->GetWindowTextLength()); 
} 
 
void CMainFrame::OnInserttimedate()  
{ 
	CInsertTimeDateDialog tDialog; 
 
	tDialog.DoModal(); 
	 
} 
 
void CMainFrame::OnUpdateChangecolor(CCmdUI* pCmdUI)  
{ 
	pCmdUI->Enable(TRUE); 
	 
} 
 
 
 
void CMainFrame::OnOptions()  
{ 
	COptionsDlg oDialog; 
 
	oDialog.DoModal(); 
	 
} 
 
 
 
 
 
void CMainFrame::OnCharmap()  
{ 
	char windowspath[MAX_PATH]; 
	GetWindowsDirectory(windowspath,MAX_PATH); 
	CString path; 
	path.Format("%s%s", windowspath , "\\CHARMAP.EXE"); 
   if (!ShellExecute(NULL,"open",path,0,0,SW_SHOWNORMAL)) 
	   MessageBox("Failed to open CHARMAP.EXE! Cannot Continue.","NoteXpad - Error",MB_ICONEXCLAMATION); 
 
} 
 
void CMainFrame::OnNewWindow()  
{	 
	CRect rect; 
	GetWindowRect(rect); 
	rect.top += 25; 
	rect.left +=25; 
	CString cmd; 
	cmd.Format("/x%d,%d",rect.top,rect.left); 
    ShellExecute(NULL,"open",AppPath(),cmd,0,SW_SHOWNORMAL);	 
} 
 
CString CMainFrame::AppPath() 
{ 
	CString exename; 
	CWinApp* app = (CWinApp*)AfxGetApp(); 
	exename.Format("%s%s", app->m_pszExeName, ".EXE"); 
	char path[MAX_PATH]; 
	GetModuleFileName(GetModuleHandle(exename), path, MAX_PATH); 
	exename = path; 
	return exename; 
} 
 
 
void CMainFrame::OnReplace()  
{   CNoteXpadReplaceDlg  *rDlg; 
	rDlg = new CNoteXpadReplaceDlg; 
	rDlg->Create(IDD_REPLACEDIALOG); 
	rDlg->CenterWindow(); 
	rDlg->OnInitializeFields(); 
	rDlg->ShowWindow(SW_SHOW);	 
} 
 
 
void CMainFrame::OnCompileTiaoshi()  
{   CCompileDialog* cDialog; 
	cDialog = new CCompileDialog(this); 
 
	CStdioFile file; 
	file.Open(CURRENT_FILE,CFile::modeRead); 
	CString sRecord[100]; 
 
	file.ReadString(sRecord); 
	file.ReadString(sRecordd); 
		cDialog->m_fData=sRecord+sRecordd; 
		cDialog->Create(IDD_COMPILE_DIALOG); 
	cDialog->ShowWindow(SW_SHOW); 
}