📄 main.cs
字号:
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new MainForm());
}
private void mainForm_Load(object sender, System.EventArgs e)
{
// Set the caption for a new session
Text = _appName + " - " + "[empty]";
}
private void fileOpenMenuItem_Click(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = _fileFilter;
dlg.InitialDirectory = Application.CommonAppDataPath;
dlg.CheckFileExists = false;
if(dlg.ShowDialog() == DialogResult.OK)
{
string path = dlg.FileName;
if(File.Exists(path) != true)
{
StreamWriter writer = File.CreateText(path);
writer.Close();
}
_fileName = Path.GetFileName(path);
_pathName = path;
Text = _appName + " - " + _fileName;
StreamReader reader = new StreamReader(path);
textBox.Text = reader.ReadToEnd();
reader.Close();
}
}
private void editClearMenuItem_Click(object sender, System.EventArgs e)
{
_fileName = "";
_pathName = "";
Text = _appName + " - " + "[Empty]";
textBox.Clear();
}
private void editWordWrapMenuItem_Click(object sender, System.EventArgs e)
{
textBox.WordWrap = !textBox.WordWrap;
}
private void fileExitMenuItem_Click(object sender, System.EventArgs e)
{
Close();
}
private void SaveTextToPath(string path)
{
StreamWriter writer = new StreamWriter(path);
writer.Write(textBox.Text);
writer.Close();
textBox.Modified = false;
}
private void SaveTextToNewPath()
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = _fileFilter;
dlg.InitialDirectory = Application.CommonAppDataPath;
if(dlg.ShowDialog() == DialogResult.OK)
{
string path = dlg.FileName;
if(File.Exists(path) == true)
{
DialogResult result = MessageBox.Show(_overwriteWarning,
_appName,
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question);
if(result == DialogResult.Cancel)
return;
}
_fileName = Path.GetFileName(path);
_pathName = path;
Text = _appName + " - " + _fileName;
SaveTextToPath(path);
}
}
private void fileSaveMenuItem_Click(object sender, System.EventArgs e)
{
if(_fileName != null && _fileName != "")
SaveTextToPath(_pathName);
else
SaveTextToNewPath();
}
private void fileSaveAsMenuItem_Click(object sender, System.EventArgs e)
{
SaveTextToNewPath();
}
private void hScrollMenuItem_Click(object sender, System.EventArgs e)
{
if(hScrollMenuItem.Checked)
{
// Clear horizontal scroll bars
if(textBox.ScrollBars == ScrollBars.Both)
textBox.ScrollBars = ScrollBars.Vertical;
else
textBox.ScrollBars = ScrollBars.None;
}
else
{
// Set horizontal scroll bars
if(textBox.ScrollBars == ScrollBars.Vertical)
textBox.ScrollBars = ScrollBars.Both;
else
textBox.ScrollBars = ScrollBars.Horizontal;
}
}
private void vScrollMenuItem_Click(object sender, System.EventArgs e)
{
if(vScrollMenuItem.Checked)
{
// Clear vertical scroll bars
if(textBox.ScrollBars == ScrollBars.Both)
textBox.ScrollBars = ScrollBars.Horizontal;
else
textBox.ScrollBars = ScrollBars.None;
}
else
{
// Set vertical scroll bars
if(textBox.ScrollBars == ScrollBars.Horizontal)
textBox.ScrollBars = ScrollBars.Both;
else
textBox.ScrollBars = ScrollBars.Vertical;
}
}
// --- Menu Update Handlers --//
private void editMenuItem_Popup(object sender, System.EventArgs e)
{
editWordWrapMenuItem.Checked = textBox.WordWrap;
editBoldMenuItem.Checked = Font.Bold;
editItalicMenuItem.Checked = Font.Italic;
editUnderlineMenuItem.Checked = Font.Underline;
}
private void viewScrollBarMenuItem_Popup(object sender, System.EventArgs e)
{
// Horizontal scrolling is n/a if the text box control
// has word-wrapping enabled.
hScrollMenuItem.Enabled = !textBox.WordWrap;
switch(textBox.ScrollBars)
{
case ScrollBars.Both:
hScrollMenuItem.Checked = true;
vScrollMenuItem.Checked = true;
break;
case ScrollBars.Vertical:
hScrollMenuItem.Checked = false;
vScrollMenuItem.Checked = true;
break;
case ScrollBars.Horizontal:
hScrollMenuItem.Checked = true;
vScrollMenuItem.Checked = false;
break;
default:
hScrollMenuItem.Checked = false;
vScrollMenuItem.Checked = false;
break;
}
}
private void mainForm_MenuComplete(object sender, System.EventArgs e)
{
Trace.WriteLine("MenuComplete event");
}
private void mainForm_MenuStart(object sender, System.EventArgs e)
{
Trace.WriteLine("MenuStart event");
}
private void fileMenuItem_Popup(object sender, System.EventArgs e)
{
fileSaveMenuItem.Enabled = textBox.Modified;
}
private void editFontMenuItem_Click(object sender, System.EventArgs e)
{
FontDialog dialog = new FontDialog();
dialog.Font = Font;
if(dialog.ShowDialog() == DialogResult.OK)
{
Font = dialog.Font;
Invalidate();
}
}
private void FlipFontStyleBit(FontStyle style)
{
// Get the current font and style.
Font oldFont = Font;
// Flip the Bold style flag.
FontStyle newStyle = Font.Style ^ style;
// Use the old font and the new style to create a new Font object.
Font = new Font(oldFont, newStyle);
// Clean up.
oldFont.Dispose();
Invalidate();
}
private void editBoldMenuItem_Click(object sender, System.EventArgs e)
{
// Get the current font and style.
Font oldFont = Font;
// Flip the Bold style flag.
FontStyle newStyle = Font.Style ^ FontStyle.Bold;
// Use the old font and the new style to create a new Font object.
Font = new Font(oldFont, newStyle);
// Clean up.
oldFont.Dispose();
Invalidate();
}
private void editItalicMenuItem_Click(object sender, System.EventArgs e)
{
// Get the current font and style.
Font oldFont = Font;
// Flip the Bold style flag.
FontStyle newStyle = Font.Style ^ FontStyle.Italic;
// Use the old font and the new style to create a new Font object.
Font = new Font(oldFont, newStyle);
// Clean up.
oldFont.Dispose();
Invalidate();
}
private void editUnderlineMenuItem_Click(object sender, System.EventArgs e)
{
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -