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

📄 grammerdlg.cpp

📁 一个pascal子集编译器。输入pascal原程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// GrammerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Grammer.h"
#include "GrammerDlg.h"
#include "ExpressAnalysis.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
extern opfour four[100];
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)
//		ON_LBN_SELCHANGE(IDC_OUT, OnSelchangeOut)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGrammerDlg dialog

CGrammerDlg::CGrammerDlg(CWnd* pParent /*=NULL*/)
	: CResizableDialog(CGrammerDlg::IDD, pParent)
{
    m_hAccel=::LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1)); 
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CGrammerDlg::DoDataExchange(CDataExchange* pDX)
{
	CResizableDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CGrammerDlg)
	DDX_Control(pDX, IDC_ERROR, m_error);
	DDX_Control(pDX, IDC_RICHEDIT1, m_rich);
	DDX_Control(pDX, IDOK, m_compile);
	DDX_Control(pDX, ID_EXIT, m_exit);
	DDX_Control(pDX, IDCANCEL, m_run);
	DDX_Control(pDX, ID_OPEN, m_open);
	DDX_Control(pDX, ID_SAVE, m_save);
	DDX_Control(pDX, IDC_OUT, m_out);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CGrammerDlg, CResizableDialog)
	//{{AFX_MSG_MAP(CGrammerDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_EN_CHANGE(IDC_RICHEDIT1, OnChangeRichedit1)
	ON_COMMAND(ID_FILE_OPEN, On_File_Open)
	ON_COMMAND(ID_FILE_SAVE, On_File_Save)
	ON_WM_SIZE()
	ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
	ON_COMMAND(ID_EDIT_PASTE, OnEditPaste)
	ON_COMMAND(ID_EDIT_CUT, OnEditCut)
	ON_COMMAND(ID_EDIT_UNDO, OnEditUndo)
	ON_COMMAND(ID_DELETE, OnDelete)
	ON_COMMAND(ID_COMPILE, OnCompile)
	ON_COMMAND(ID_RUN, OnRun)
	ON_COMMAND(ID_EDIT_SELECT_ALL, OnEditSelectAll)
	ON_COMMAND(ID_FILE_EXIT, On_File_Exit)
	ON_WM_CLOSE()
	ON_COMMAND(ID_FILE_SAVEAS, OnFileSaveas)
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	ON_BN_CLICKED(ID_OPEN, On_File_Open)
	ON_BN_CLICKED(ID_SAVE, On_File_Save)
	ON_LBN_SELCHANGE(IDC_OUT, OnSelchangeOut)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGrammerDlg message handlers

BOOL CGrammerDlg::OnInitDialog()
{
	lastbegin=lastend=lastbg=lasted=-100;
	CResizableDialog::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
//	AddAnchor(IDC_RICHEDIT1, TOP_LEFT, TOP_RIGHT);
//	AddAnchor(IDC_OUT, BOTTOM_LEFT, BOTTOM_RIGHT);
	
	m_rich.SetEventMask(ENM_CHANGE);  
	CHARFORMAT cf;
    ZeroMemory(&cf, sizeof(CHARFORMAT));
    cf.cbSize = sizeof(CHARFORMAT);
    cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE |
                        CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
    cf.dwEffects = 0;
//	cf.crBackColor =clRed;       //       红色         
    cf.yHeight = 20*20;//文字高度
    cf.crTextColor = RGB(0, 0, 0); //文字颜色
    strcpy(cf.szFaceName ,"Fixedsys");  
    m_rich.SetDefaultCharFormat(cf);
	//打开文件
	m_open.SetIcon(IDI_OPEN);
	m_open.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_open.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));

	m_open.SetAlign(CButtonST::ST_ALIGN_VERT);//T_ALIGN_HORIZ_RIGHT);//将图标设置在右边
	m_open.SetTooltipText("开始分析");
	//保存文件
	m_save.SetIcon(IDI_NEW);
	m_save.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_save.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));

	m_save.SetAlign(CButtonST::ST_ALIGN_VERT);//T_ALIGN_HORIZ_RIGHT);//将图标设置在右边
	m_save.SetTooltipText("Save files");
	
	//退出系统
	m_exit.SetIcon(IDI_EXIT);
	m_exit.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_exit.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));

	m_exit.SetAlign(CButtonST::ST_ALIGN_VERT);//T_ALIGN_HORIZ_RIGHT);//将图标设置在右边
	m_exit.SetTooltipText("Exit System");


	//分析程序
	m_compile.SetIcon(IDI_ICON1);
	m_compile.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_compile.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));

	m_compile.SetAlign(CButtonST::ST_ALIGN_VERT);//T_ALIGN_HORIZ_RIGHT);//将图标设置在右边
	m_compile.SetTooltipText("开始分析");
	
	//运行程序
	m_run.SetIcon(IDI_RUN);
	m_run.OffsetColor(CButtonST::BTNST_COLOR_BK_IN, 30);
	m_run.SetColor(CButtonST::BTNST_COLOR_FG_IN, RGB(0, 128, 0));

	m_run.SetAlign(CButtonST::ST_ALIGN_VERT);//T_ALIGN_HORIZ_RIGHT);//将图标设置在右边
	m_run.SetTooltipText("Run Appliction");
	SetWindowText("无标题- PASCAL分析程序");
//	ShowWindow(SW_SHOWMAXIMIZED);//最大化  
	Save_Switch=0;
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CGrammerDlg::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 CGrammerDlg::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 CGrammerDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

BOOL CGrammerDlg::PreTranslateMessage(MSG* pMsg) 
{   
	if(pMsg->message==WM_KEYDOWN   &&   pMsg->wParam   ==   VK_TAB)   
	{   
		if(m_rich.GetSafeHwnd()==GetFocus()->GetSafeHwnd())   
		{   
			m_rich.ReplaceSel("\t");   
			return   TRUE;   
		}   
	}   
	if(m_hAccel)     
    {   
      if(::TranslateAccelerator(m_hWnd,m_hAccel,pMsg))     
      {   
          return(TRUE);   
      }   
    }   
	return   CDialog::PreTranslateMessage(pMsg);   
}

void CGrammerDlg::OnSelchangeOut() 
{
	// TODO: Add your control notification handler code here
	int beginl[500],endl[500],numl=0;
	Save_Switch=1;
    long nStart, nEnd,i,p;
    CString strTemp;
	CHARFORMAT cf,cl;
	m_rich.GetDefaultCharFormat(cl);

	CString temp_word;
	m_rich.GetWindowText(temp_word);
	m_rich.GetSel(nStart, nEnd);
	beginl[numl]=0;
	for(i=0;i<temp_word.GetLength();i++)
	{
		if(temp_word.Mid(i,1)=="\r")
		{
			endl[numl]=i;
			numl++;
			beginl[numl]=i+2;
		}
	}
	p=m_out.GetCurSel();
	if(!(lastbegin==-100 && lastend==-100))
	{
			m_rich.GetDefaultCharFormat(cl);
			m_rich.SetSel(lastbegin,lastend); //设置处理区域
			m_rich.SetSelectionCharFormat(cl);
	}
	cf.cbSize = sizeof(CHARFORMAT);
	cf.dwMask = CFM_BOLD | CFM_COLOR | CFM_FACE |
							CFM_ITALIC | CFM_SIZE | CFM_UNDERLINE;
	cf.dwEffects = 0;
	cf.yHeight = 15*15;//文字高度
	cf.crTextColor = RGB(7, 248, 7); //文字颜色
	strcpy(cf.szFaceName ,_T("Arial"));//设置字体
 
	m_rich.SetSel(beginl[code[p+10].line-1], endl[code[p+10].line-1]); //设置处理区域
	lastbegin=beginl[code[p+10].line-1];
	lastend= endl[code[p+10].line-1];
	m_rich.SetSelectionCharFormat(cf);
//	m_rich.SetParaFormat(cf);
//    m_rich.SetWordCharFormat(cl);
/*	 m_rich.SetSel(nStart, nEnd);
	 m_rich.SetWordCharFormat(cl);
*/
}
/*

void CGrammerDlg::OnChangeOut1()
{
 long nStart, nEnd;
    CString strTemp;

    m_rich.GetSel(nStart, nEnd);
    if(nStart == nEnd)
    {
        strTemp.Format(_T("光标在%d"), nStart);
        AfxMessageBox(strTemp);
    }
    else
    {
        //得到edit选中的内容     
        m_rich.GetWindowText(strTemp);
        strTemp = strTemp.Mid(nStart) - strTemp.Mid(nEnd);
        AfxMessageBox(strTemp); 
    }
}
*/
void CGrammerDlg::OnChangeRichedit1() 
{
//	CHARFORMAT cl;
//	m_rich.GetDefaultCharFormat(cl);
//	m_rich.SetWordCharFormat(cl);
/*	char strkeywords[14][20]={"program","const","var","procedure","begin","end","if",
"then","else","while","do","call","read","write"};
	int keywordsnum=14;
	Save_Switch=1;

⌨️ 快捷键说明

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