?? formfunctioncoding.cs
字號:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.CodeDom.Compiler;
namespace ProgramCalculator
{
public partial class FormFunctionCoding : Form
{
private bool isTextChanged = false;//跟蹤文本是否已經(jīng)被修改
private bool isNewFunction = false;//指示目前是添加的新函數(shù),還是修改已有函數(shù)
private FunctionInfo funInfo = null;//本函數(shù)對應(yīng)的FunctionInfo對象
//自動(dòng)生成部分代碼
private void AutoGenerateCode()
{
this.textBoxCoding.Clear();
string code =
Environment.NewLine +
"//不要修改此行代碼" + Environment.NewLine +
"public static double " + this.funInfo.Name + Environment.NewLine +
"(" + Environment.NewLine +
"//TODO:在這里修改參數(shù)列表" + Environment.NewLine +
")" + Environment.NewLine +
"{" + Environment.NewLine + Environment.NewLine +
" //TODO:在這里修改函數(shù)體" + Environment.NewLine + Environment.NewLine +
" return 0.0;" + Environment.NewLine +
"}" + Environment.NewLine;
this.textBoxCoding.Text = code;
}
/// <summary>
/// 保存相關(guān)文件
/// </summary>
/// <returns></returns>
private bool Save()
{
this.isTextChanged = false;
string path = Function.DirectoryOfFunctions + this.funInfo.RelativePathOfCodeFile;
StreamWriter sw = new StreamWriter(path, false, Encoding.Default, 100);
try
{
sw.Write(this.textBoxCoding.Text);
}
catch (Exception ex)
{
MessageBox.Show("Error at save codefile:\n" + ex);
return false;
}
finally
{
sw.Close();
}
return true;
}
/// <summary>
/// 刪除相關(guān)文件
/// </summary>
/// <returns></returns>
private bool Delete()
{
//編譯未通過時(shí),刪除可能帶來錯(cuò)誤的代碼文件
//帶來錯(cuò)誤的可能是函數(shù)代碼,也可能是用戶修改了using
string path1 = Function.DirectoryOfFunctions + this.funInfo.RelativePathOfCodeFile;
string path2 = Function.GetPathOfUsingFile();
try
{
Function.CollectionOfFunctionInfo.Remove(this.funInfo);
Function.SaveFunctionsToList();
}
catch { }
try
{
File.Delete(path1);
File.Delete(path2);
}
catch (Exception ex)
{
MessageBox.Show("Error at save delete FunctionCodeFile:\n" + ex);
return false;
}
return true;
}
/// <summary>
/// 構(gòu)造函數(shù)
/// </summary>
/// <param name="fInfo">該函數(shù)對應(yīng)的FunctionInfo對象</param>
/// <param name="isNewFunction">指示目前是添加的新函數(shù),還是修改已有函數(shù)</param>
public FormFunctionCoding(FunctionInfo fInfo, bool isNewFunction)
{
this.funInfo = fInfo;
this.isNewFunction = isNewFunction;
InitializeComponent();
}
private void textBoxCoding_TextChanged(object sender, EventArgs e)
{
this.isTextChanged = true;
}
private void FormFunctionCoding_Load(object sender, EventArgs e)
{
this.Closing += new CancelEventHandler(FormFunctionCoding_Closing);
this.labelFunctionName.Text = this.funInfo.Name + "函數(shù)編輯";
//如果是新函數(shù)則自動(dòng)創(chuàng)建部分代碼
//否則讀取該函數(shù)的代碼
if (this.isNewFunction)
{
this.AutoGenerateCode();
}
else
{
StreamReader sr = null;
string code = "";
try
{
sr = new StreamReader(Function.DirectoryOfFunctions + this.funInfo.RelativePathOfCodeFile, Encoding.Default);
code = sr.ReadToEnd();
this.textBoxCoding.Text = code;
this.isTextChanged = false;
}
catch (Exception ex)
{
MessageBox.Show("在讀取代碼文件時(shí)發(fā)生錯(cuò)誤\n" + ex);
}
finally
{
if (sr != null)
{
sr.Close();
}
}
}
}
void FormFunctionCoding_Closing(object sender, CancelEventArgs e)
{
if (this.isTextChanged)
{
DialogResult dr = MessageBox.Show("\n\n內(nèi)容已改變, 保存嗎? \n\n", "保存",
MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
if (dr == DialogResult.Yes)
{
this.Save();
}
else if (dr == DialogResult.Cancel)
{
e.Cancel = true;
}
}
}
private void buttonSave_Click(object sender, EventArgs e)
{
this.Save();
}
//生成函數(shù)
private void buttonMake_Click(object sender, EventArgs e)
{
if (!this.Save())
{
MessageBox.Show("保存文件失敗");
return;
}
CompilerResults res;
if (CompilerUnit.BuildFunctionDll(out res))
{
//如果是新函數(shù),則在主窗口添加相應(yīng)按鈕等等
if (this.isNewFunction)
{
FormMain fMain = (FormMain)this.Owner;
Function.CollectionOfFunctionInfo.Add(this.funInfo);
fMain.AddFunctionButtonToForm(this.funInfo);
Function.SaveFunctionsToList();
}
this.Close();
string warning = "\n生成成功! 重新啟動(dòng)程序后修改生效. 現(xiàn)在就重新啟動(dòng)程序嗎?\n";
if (MessageBox.Show(warning, "重啟程序", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
== DialogResult.Yes)
{
//重新啟動(dòng)程序--------------------
string path = Application.StartupPath + Path.DirectorySeparatorChar +
"ProgramCalculator.exe";//請修改,不可靠的
this.Owner.Dispose();
System.Diagnostics.Process.Start(path);
//---------------------------------
}
}
else
{
MessageBox.Show("生成失敗\n " + res.Errors[0].ErrorText);
this.Delete();
Function.GetPathOfUsingFile();//重要的,這將還原using文件
}
}
//添加using語句
private void buttonAddUsing_Click(object sender, EventArgs e)
{
//重要:備份using文件,用于編譯錯(cuò)誤時(shí)還原using文件
StreamReader sr = new StreamReader(Function.GetPathOfUsingFile());
try
{
Function.UsingFileBackup = sr.ReadToEnd();
}
catch { }
finally
{
sr.Close();
}
//------
FormAddUsing fu = new FormAddUsing();
fu.ShowDialog(this);
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -