📄 form1.cs
字号:
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
}
// <summary>
// 关闭窗口时调用
// </summary>
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (!closeFlag)
if (!closeForm())
e.Cancel = true;
//判断是否从菜单关闭
}
private void Form1_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effect = DragDropEffects.Link;
richTextBox1.ReadOnly = true;
}
else e.Effect = DragDropEffects.None;
}
private void Form1_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
openFile(path);
richTextBox1.ReadOnly = false;
}
/* <summary>
保存为新文件
</summary>*/
private bool saveNewFile(string resultOrFile)
{
//不允许保存空文件
if (resultOrFile != "")
{
//显示文件保存对话框
saveFileDialog1.ShowDialog();
//获取新文件路径
string savePath = saveFileDialog1.FileName;
// 路径非空时保存文件
if (savePath != "")
{
try
{
//保存源文件
File.WriteAllText(savePath, resultOrFile, System.Text.Encoding.GetEncoding("GB2312"));
//产生源文件副本
fileContents = resultOrFile;
//返回文件保存成功
return true;
}
catch (Exception saveFileException)
{
MessageBox.Show("文件保存失败!");
}
}
}
return false;
}
// <summary>
// 保存文件
// </summary>
private bool saveFile()
{
// 路径非空时保存文件
if (path != "")
{
try
{
//直接写入源文件
File.WriteAllText(path, richTextBox1.Text, System.Text.Encoding.GetEncoding("GB2312"));
//产生源文件副本
fileContents = richTextBox1.Text;
//返回文件保存成功
return true;
//MessageBox.Show("文件保存成功!");
}
catch (Exception fileSaveException)
{
MessageBox.Show("源文件不存在!");
}
}
return saveNewFile(richTextBox1.Text);
}
// <summary>
// 关闭窗口
// </summary>
private bool closeForm()
{
//判断文件是否修改后没有被保存,即文件是否允许直接关闭
if (fileContents == richTextBox1.Text)
{
return true;
}
else
{
DialogResult saveOrNot;
saveOrNot = MessageBox.Show("文件已修改,是否保存?", "文件保存", MessageBoxButtons.YesNoCancel);
if (saveOrNot.Equals(DialogResult.Yes))
{
if (saveFile())
{
return true;
}
else
{
return false;
}
}
else if (saveOrNot.Equals(DialogResult.No))
{
return true;
}
}
return false;
}
// <summary>
// 文件打开函数
// </summary>
private bool checkFileExtension(string fileName)
{
String[] fileExtension = new String[] { ".txt", ".c", ".cpp", ".h", ".cs", ".css"};
FileInfo fileInfo = new FileInfo(fileName);
foreach(string name in fileExtension)
{
if(fileInfo.Extension.ToLower().EndsWith(name))
return true;
}
return false;
}
// <summary>
// 文件打开函数
// </summary>
private void openFile(string path)
{
if (checkFileExtension(path))
{
try
{
StreamReader content = new StreamReader(path, System.Text.Encoding.GetEncoding("GB2312"));
richTextBox1.Text = content.ReadToEnd();
fileContents = richTextBox1.Text;
content.Close();
}
catch (Exception pathIsError)
{
MessageBox.Show("源文件不存在或文件类型不匹配!");
}
}
else
{
MessageBox.Show("文件类型不匹配!");
}
}
// <summary>
// 点击"打开"按钮时,触发事件
// </summary>
private void open_Click(object sender, System.EventArgs e)
{
openFileDialog1.ShowDialog();
path = openFileDialog1.FileName;
if (path != "")
{
label3.Text = "您选择了从源文件读入待分析代码。";
openFile(path);
}
}
// <summary>
// 点击"保存"按钮时,触发事件
// </summary>
private void save_Click(object sender, System.EventArgs e)
{
label3.Text = "这个按钮只能保存您的代码";
saveFile();
}
// <summary>
// 点击"另存为"按钮时,触发事件
// </summary>
private void saveas_Click(object sender, System.EventArgs e)
{
saveNewFile(richTextBox1.Text);
}
// <summary>
// 在菜单栏点击"词法分析"按钮时,触发事件
// </summary>
private void analyze_Click(object sender, System.EventArgs e)
{
//当文本框非空时执行
if (richTextBox1.Text != "")
{
label3.Text = "欢迎使用C——词法分析器!";
Analyser resultString = new Analyser(richTextBox1.Text.ToString());
result.Text = resultString.printTokenList();
}
}
// <summary>
// 在菜单栏点击"保存分析结果"按钮时,触发事件
// </summary>
private void saveresult_Click(object sender, System.EventArgs e)
{
label3.Text = "这个按钮只能保存输出的分析结果";
saveNewFile(result.Text);
}
// <summary>
// 点击"词法分析"按钮时,触发事件
// </summary>
private void button1_Click(object sender, EventArgs e)
{
//当文本框非空时执行
if (richTextBox1.Text != "")
{
label3.Text = "欢迎使用C——词法分析器!";
Analyser resultString = new Analyser(richTextBox1.Text.ToString());
result.Text = resultString.printTokenList();
}
}
// <summary>
// 点击"保存分析结果"按钮时,触发事件
// </summary>
private void button2_Click(object sender, EventArgs e)
{
label3.Text = "这个按钮只能保存输出的分析结果";
saveNewFile(result.Text);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -