📄 textedit.cs
字号:
// Read the three fields and create the font.
// Close the file.
textInput.Font =
new Font(brdrFile.ReadString(),
brdrFile.ReadSingle(),
(FontStyle)brdrFile.ReadInt32());
brdrFile.Close();
}
#endregion
#region Font Settings Structure
// A structure containing font information and
// the routines to write / restore that info
// to / from a file.
private struct ourFontInfo
{
public string strName;
public float sglSize;
public FontStyle fsStyle;
public void WriteToFile(string strDirName,
string strFileName,
Encoding encodeFile)
{
// Create the directory if it does not
// already exist, and make it the
// current directory.
if (! Directory.Exists(strDirName) )
{
Directory.CreateDirectory(strDirName);
}
Directory.SetCurrentDirectory(strDirName);
// Create the file if it does not
// already exist.
if (! File.Exists(strFileName) )
{
File.Create(strFileName).Close();
}
// Create a BinaryWriter (wrapped around a
// FileStream wrapped around the file)
// for output using the user specified
// encoding.
BinaryWriter bwrtFile =
new BinaryWriter(
new FileStream(
strFileName,
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.None),
encodeFile);
// Write the three fields, each of a
// different data type.
// Close the file.
bwrtFile.Write(this.strName);
bwrtFile.Write(this.sglSize);
bwrtFile.Write((int)this.fsStyle);
bwrtFile.Close();
}
public void ReadFromFile(string strDirName,
string strFileName,
Encoding encodeFile)
{
// Set the current directory
Directory.SetCurrentDirectory(strDirName);
// Create a BinaryReader (wrapped around a
// FileStream wrapped around the file)
// for input using the user specified
// encoding.
BinaryReader brdrFile =
new BinaryReader(
new FileStream(
strFileName,
FileMode.OpenOrCreate,
FileAccess.Read,
FileShare.None),
encodeFile);
// Read the three fields.
// Close the file.
try
{
this.strName = brdrFile.ReadString();
this.sglSize = brdrFile.ReadSingle();
this.fsStyle = (FontStyle)brdrFile.ReadInt32();
}
catch
{
this.strName = "Tahoma";
this.sglSize = 9;
this.fsStyle = FontStyle.Regular;
}
finally
{
brdrFile.Close();
}
}
}
#endregion
#region Save/Restore to Registry
// Variables for registry access.
UtilRegistry urNotepad = new UtilRegistry();
string strNotepadCE = "NotepadCe";
string strFont = "Font";
string strName = "Name";
string strSize = "Size";
string strStyle = "Style";
string strOptions = "Options";
string strMenu = "Menu";
string strToolBar = "ToolBar";
string strScrollBars = "ScrollBars";
string strTextAlign = "TextAlign";
string strWordWrap = "WordWrap";
private void mitemSettingsSave_Click(object sender,
EventArgs e)
{
// Use the UtilRegistry object to save the
// font settings.
// UtilRegistry has three overloads for
// SetValue; string, integer, and boolean.
// Font Size and Style are data types that
// derive from integer; they have to
// be converted to integer for this call.
// Font.Style data type, because it has
// the [Flags] attribute, requires an
// unchecked conversion.
urNotepad.SetValue(
strNotepadCE + @"\" + strFont,
strName,
textInput.Font.Name);
urNotepad.SetValue(
strNotepadCE + @"\" + strFont,
strSize,
Convert.ToInt32(textInput.Font.Size));
urNotepad.SetValue(
strNotepadCE + @"\" + strFont,
strStyle,
unchecked((System.Int32)textInput.Font.Style));
// Use the UtilRegistry object to save the
// Textbox settings; three of which are
// boolean, two of which are [Flags]
// attributed unsigned integers.
urNotepad.SetValue(
strNotepadCE + @"\" + strOptions,
strMenu,
this.Menu == this.menuMain);
urNotepad.SetValue(
strNotepadCE + @"\" + strOptions,
strToolBar,
this.Controls.Contains(tbarCommands));
urNotepad.SetValue(
strNotepadCE + @"\" + strOptions,
strScrollBars,
unchecked((System.Int32)textInput.ScrollBars));
urNotepad.SetValue(strNotepadCE + @"\" + strOptions,
strTextAlign,
unchecked((System.Int32)textInput.TextAlign));
urNotepad.SetValue(strNotepadCE + @"\" + strOptions,
strWordWrap,
textInput.WordWrap);
}
// Version that uses the ourFontInfo structure
// private void mitemSettingsSave_Click(object sender,
// EventArgs e)
// {
// string strDirName = @"My Documents\NotepadCE";
// string strFileName = "Settings.dat";
//
// // Create an ourFontInfo structure, load it from
// // TextInput's font, have the structure save
// // its contents to the file.
//
// ourFontInfo fiFont= new ourFontInfo();
// fiFont.strName = textInput.Font.Name;
// fiFont.sglSize = textInput.Font.Size;
// fiFont.fsStyle = textInput.Font.Style;
// fiFont.WriteToFile(
// strDirName, strFileName, Encoding.Default);
// }
private void mitemSettingsRestore_Click(object sender,
EventArgs e)
{
// Read Font info, if any, from the registry,
// Create a font from that info, or use default
// values if no info is in the registry.
// Set textInput.Font = that font.
string strFontName;
int intFontSize;
int intFontStyle;
strFontName = "Tahoma";
urNotepad.GetValue(strNotepadCE + @"\" + strFont,
strName,
ref strFontName);
intFontSize = 9;
urNotepad.GetValue(strNotepadCE + @"\" + strFont,
strSize,
ref intFontSize);
intFontStyle = (int)FontStyle.Regular;
urNotepad.GetValue(strNotepadCE + @"\" + strFont,
strStyle,
ref intFontStyle);
textInput.Font =
new Font(strFontName,
intFontSize,
(FontStyle)intFontStyle);
// Read Option info, if any, from the registry.
// Set the properties from that info, or use default
// values if no info is in the registry.
bool boolTemp;
int intTemp;
// .Menu is either menuMain or null
boolTemp = true;
urNotepad.GetValue(strNotepadCE + @"\" + strOptions,
strMenu,
ref boolTemp);
this.Menu = boolTemp ? this.menuMain : null;
// .Controls either contains
// tbarCommands or it doesn't
boolTemp = true;
urNotepad.GetValue(strNotepadCE + @"\" + strOptions,
strToolBar,
ref boolTemp);
if ( boolTemp )
{
if (! this.Controls.Contains(this.tbarCommands) )
{
this.Controls.Add(this.tbarCommands);
}
}
else
{
if ( this.Controls.Contains(this.tbarCommands) )
{
this.Controls.Remove(this.tbarCommands);
}
}
// .ScrollBars
intTemp = (int)ScrollBars.Both;
urNotepad.GetValue(strNotepadCE + @"\" + strOptions,
strScrollBars,
ref intTemp);
textInput.ScrollBars = (ScrollBars)intTemp;
// .TextAlign
intTemp = (int)HorizontalAlignment.Left;
urNotepad.GetValue(strNotepadCE + @"\" + strOptions,
strTextAlign,
ref intTemp);
textInput.TextAlign = (HorizontalAlignment)intTemp;
// .WordWrap
boolTemp = true;
urNotepad.GetValue(strNotepadCE + @"\" + strOptions,
strWordWrap,
ref boolTemp);
textInput.WordWrap = boolTemp;
}
// Version that uses the ourFontInfo structure
// private void mitemSettingsRestore_Click(object sender,
// EventArgs e)
// {
// string strDirName = @"My Documents\NotepadCE";
// string strFileName = "Settings.dat";
//
// // Create an ourFontInfo structure, have the
// // structure load itself from the file,
// // create the font from the loaded info.
// ourFontInfo fiFont = new ourFontInfo();
// fiFont.ReadFromFile(
// strDirName, strFileName, Encoding.Default);
// textInput.Font =
// new Font(
// fiFont.strName, fiFont.sglSize, fiFont.fsStyle);
// }
private void mitemSettingsInit_Click(object sender,
EventArgs e)
{
// Initialize the settings by deleting the
// registry key where they are stored and
// then restoring from the non existant
// key to reinstate all default values.
urNotepad.DeleteKey(strNotepadCE);
mitemSettingsRestore_Click(this, EventArgs.Empty);
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -