?? mainfrm.cpp
字號:
// MainFrm.cpp : implementation of the CMainFrame class
#include "stdafx.h"
#include "SS.h"
#include "SourceEditView.h"
#include "MainFrm.h"
#include "SetDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMainFrame
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)
// No message handlers
//}}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()
// App command to run the dialog
IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
//{{AFX_MSG_MAP(CMainFrame)
ON_WM_CREATE()
ON_COMMAND(ID_CIFA, OnCifa)
ON_COMMAND(ID_YUFA, OnYufa)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_SET, OnSet)
ON_COMMAND(ID_TEST, OnTest)
ON_COMMAND(ID_SELECT_ALL, OnSelectAll)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction
CMainFrame::CMainFrame()
{
// TODO: add member initialization code here
}
CMainFrame::~CMainFrame()
{
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
{
TRACE0("Failed to create toolbar\n");
return -1; // fail to create
}
m_wndToolBar.SetButtonText(0,"新建"); //你所需要做的工作只需要添加這些:)
m_wndToolBar.SetButtonText(1,"打開"); //第一個參數是圖標索引, 第2個參數是想要顯示的文本
m_wndToolBar.SetButtonText(2,"保存");
m_wndToolBar.SetButtonText(4,"剪切");
m_wndToolBar.SetButtonText(5,"復制");
m_wndToolBar.SetButtonText(6,"粘貼");
m_wndToolBar.SetButtonText(8,"打印");
m_wndToolBar.SetButtonText(10,"關于");
m_wndToolBar.SetButtonText(11,"詞法");
m_wndToolBar.SetButtonText(13,"語法");
m_wndToolBar.SetButtonText(14,"設置");
m_wndToolBar.SetButtonText(15,"測試");
m_wndToolBar.SetButtonText(16,"退出");
m_wndToolBar.SetSizes(CSize(45,45),CSize(24,24));
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: Delete these three lines if you don't want the toolbar to
// be dockable
m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
EnableDocking(CBRS_ALIGN_ANY);
DockControlBar(&m_wndToolBar);
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
cs.x=0;
cs.y=0;
cs.cx=::GetSystemMetrics(SM_CXSCREEN);
cs.cy=::GetSystemMetrics(SM_CYSCREEN)-30;
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
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext)
{
if (!m_wndSplitter.CreateStatic(this, 1, 2)) //創建1行2列的切分窗口
{
TRACE0("Failed to CreateStaticSplitter\n");
return FALSE;
}
CRect rc; //獲得客戶區大小
GetClientRect(rc);
int x=rc.Width()/2;
int y=rc.Height()/2;
if (!m_wndSplitter.CreateView(0, 0, RUNTIME_CLASS(CSourceEditView), CSize(x,y), pContext))
{
TRACE0("Failed to create second pane\n");
return FALSE;
} //創建第1個視圖
if (!m_wndSplitter.CreateView(0, 1, pContext->m_pNewViewClass, CSize(x,y), pContext))
{
TRACE0("Failed to create first pane\n");
return FALSE;
} //創建第2個視圖
m_pEditSrc=&((CEditView*)m_wndSplitter.GetPane(0,0))->GetEditCtrl(); //獲得CEditView視圖中的編輯框控件的指針
m_pEditOut=&((CEditView*)m_wndSplitter.GetPane(0,1))->GetEditCtrl();
/*--------------------------------設置字體--------------------------*/
m_font.CreateFont(14, 0,0,0,FW_NORMAL, 0,0,0,
DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Verdana");
m_font2.CreateFont(12, 0,0,0,FW_NORMAL, 0,0,0,
DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "fixedsys");
m_pEditOut->SetFont(&m_font);
m_pEditSrc->SetFont(&m_font2);
/*--------------------------------設置字體--------------------------*/
::Sleep(500);
if ( ((CSSApp*)AfxGetApp())->m_pLogoThread != NULL)
((CSSApp*)AfxGetApp())->m_pLogoThread->HideSplash(); //在創建了主對話框 并初始化了各個子對話框以后,隱藏啟動畫面
return true;
}
void CMainFrame::OnCifa()
{
m_pEditOut->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\t正在進行詞法分析,稍等....");
CWaitCursor cur;
CString str, strTemp;
m_pEditSrc->GetWindowText(strTemp);
if(strTemp.IsEmpty())
{
m_pEditOut->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n請在左邊編輯源程序或打開一個源程序文件!\r\n\r\n然后再進行詞法分析");
return;
}
char buf[10000];
int sz=m_pEditSrc->GetWindowText(buf,10000); //獲得源代碼
::strcpy(m_fenxi.m_str,buf);
m_fenxi.CiFaFenXi(); //進行詞法分析
CIFA * * p=m_fenxi.m_cifa;
int s=0;
int a=p[s]->nType;
while ((p[s]->nType)>=0)
s++;
char (*ep)[100];
ep=m_fenxi.m_szErrMsg;
//result to : p,s,ep
int ErrorCount=0;
str.Format("\r\n\t\t詞法分析結果\r\n\r\n\r\n輸出格式:二元式序列(類型,值)單詞\r\n\r\n");
for (int i=0;i<s;i++)
{
if (p[i]->nType>0)
{
strTemp.Format("(%d,%d)\t%s\r\n",p[i]->nType,p[i]->nValue,p[i]->szText);
str+=strTemp;
}
else
{
ErrorCount++;
if (p[i]->nValue==2 && (p[i]->szText[0]>126 || p[i]->szText[0]<32))
{
strTemp.Format("錯誤號:%d %s 【 無法顯示該字符! 】\r\n", p[i]->nValue,ep[p[i]->nValue]);
str+=strTemp;
}
else
{
strTemp.Format("錯誤號:%d %s 【 %s 】\r\n",p[i]->nValue,ep[p[i]->nValue],p[i]->szText);
str+=strTemp;
}
}
}
strTemp.Format("\r\n 發現 %d 個錯誤!\r\n",ErrorCount);
str+=strTemp;
m_pEditOut->SetWindowText(str);
}
void CMainFrame::OnYufa()
{
CWaitCursor cur;
CString str,strTemp;
m_pEditSrc->GetWindowText(strTemp);
if(strTemp.IsEmpty())
{
m_pEditOut->SetWindowText("\r\n\r\n\r\n\r\n\r\n\r\n\r\n請在左邊編輯源程序或打開一個源程序文件!\r\n\r\n然后再進行語法分析");
return;
}
char buf[30003]; //源代碼緩沖區
int sz=m_pEditSrc->GetWindowText(buf,30000);
buf[sz]=0;
::strcpy(m_fenxi.m_str,buf);
m_fenxi.CiFaFenXi(); //詞法分析
CIFA * * cp=m_fenxi.m_cifa;
int s=0;
while ((cp[s]->nType)>=0)
s++;
char (*ep)[100];
ep=m_fenxi.m_szErrMsg;
for (int i=0;i<s;i++)
if (cp[i]->nType==0)
break;
m_fenxi.YuFaFenXi(); //語法分析
//result to : cp,s,ep
m_pEditOut->SetWindowText("");
str.Format("\r\n\t\t語法分析結果\r\n\r\n\r\n");
if (m_fenxi.m_nErrNo==0)
{
strTemp.Format("源程序語法正確!\r\n");
str+=strTemp;
}
else
{
strTemp.Format("語法上有錯誤!!\r\n");
str+=strTemp;
m_pEditSrc->SetSel(cp[m_fenxi.m_nErrAddr]->nAddr,
cp[m_fenxi.m_nErrAddr]->nAddr
+::strlen(cp[m_fenxi.m_nErrAddr]->szText));
strTemp.Format("\r\n錯誤號:%d %s\r\n",
m_fenxi.m_nErrNo,
ep[m_fenxi.m_nErrNo]);
str+=strTemp;
}
m_pEditOut->SetWindowText(str);
}
void CMainFrame::AppendText(CEdit *pEdit, char *pText)
{
int nLen=pEdit->GetWindowTextLength();
if (nLen>29000)
return;
pEdit->SetSel(nLen,nLen);
pEdit->ReplaceSel(pText);
}
void CMainFrame::OnAppAbout()
{
this->m_pEditOut->SetWindowText("");
AppendText(m_pEditOut,"\r\n\r\n\r\n\r\n\tPL/0語言的詞法和語法分析程序\r\n");
AppendText(m_pEditOut,"---------------------------------------------------------------------\r\n");
AppendText(m_pEditOut,"\r\n\r\n程序作者:2002112105 程紅秀");
AppendText(m_pEditOut,"\r\n\r\n完成日期:2005-06-13");
AppendText(m_pEditOut,"\r\n\r\n使用說明:PL/0語言是Pascal語言的一個子集,");
AppendText(m_pEditOut,"\r\n\t這里只是演示了詞法和語法分析");
AppendText(m_pEditOut,"\r\n\r\n我的網站:http://www.59hao.com/chx");
}
void CMainFrame::OnSet()
{
CSetDlg sd;
if(sd.DoModal()==IDOK)
{
((CSourceEditView*)this->m_wndSplitter.GetPane(0,0))->SetTextColor(sd.m_crTextColor);
((CSourceEditView*)this->m_wndSplitter.GetPane(0,0))->SetBackColor(sd.m_crBackColor);
}
// m_wndSplitter.GetPane(0,1)->SetTextColor(sd.m_crTextColor2);
// m_wndSplitter.GetPane(0,1)->SetBackColor(sd.m_crBackColor2);
}
void CMainFrame::OnTest()
{
CStdioFile sf;
if(sf.Open("Test.pl0",CFile::modeRead))
{
CString strTemp,strOut;
while(sf.ReadString(strTemp))
{
strOut+=strTemp;
strOut+="\r\n";
}
m_pEditSrc->SetWindowText(strOut);
sf.Close();
m_pEditOut->SetWindowText("");
AppendText(m_pEditOut,"\r\n\r\n\r\n\r\n\r\n\r\n左邊是一個正確PL/0程序,請進行詞法和語法分析看看!");
}
}
void CMainFrame::OnSelectAll()
{
m_pEditSrc->SetSel(0,-1);
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -