📄 termform.cs
字号:
this.KeyPreview = true;
this.MinimumSize = new System.Drawing.Size(434, 180);
this.Name = "TermForm";
this.Text = "BaseTerm: Offline";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form_KeyDown);
this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.Form_KeyPress);
this.Load += new System.EventHandler(this.BaseTermForm_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void Form_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
byte c;
c = (byte)e.KeyChar;
if ((c == 13) && (!inEscape))
{
textBoxS.AppendText("<CR>");
BaseTerm.term.SendCtrl("CR");
return;
}
if ((c < 0x20) || (c > 0x7E)) return;
if (inEscape)
{
if (c == (byte)'<')
{
BaseTerm.term.SendChar(c);
textBoxS.Text = mark;
inEscape = false;
textBoxS.BackColor = Color.White;
}
else
{
if (c == (byte)'>')
{
if (!BaseTerm.term.SendCtrl(esc))
{
textBoxS.Text = mark;
c = 0;
}
inEscape = false;
textBoxS.BackColor = Color.White;
}
else
{
esc += ((char)c).ToString();
}
}
}
else
{
if (c == (byte)'<')
{
inEscape = true;
esc = "";
mark = textBoxS.Text;
textBoxS.BackColor = Color.Yellow;
}
else
{
BaseTerm.term.SendChar(c);
}
}
if (c >= (byte)' ') textBoxS.AppendText(e.KeyChar.ToString());
e.Handled = true;
}
private void buttonO_Click(object sender, System.EventArgs e)
{
if (buttonO.Tag.ToString() == "1")
{
if (info != null) info.Close();
info = null;
BaseTerm.term.Close();
buttonO.Tag = "0";
buttonO.Text = "Offline";
}
else
{
try
{
if (BaseTerm.term.Open())
{
ShowMsg(">>>> ONLINE");
buttonO.Tag = "1";
buttonO.Text = "Online";
}
else
{
ShowMsg(">>>> PORT UNAVAILABLE");
}
}
catch (CommPortException ex)
{
ShowException(ex);
}
}
textBoxS.Focus();
}
public void ShowException(CommPortException e)
{
Color c = textBoxR.SelectionColor;
textBoxR.SelectionColor = Color.Red;
textBoxR.AppendText("\r\n>>>> EXCEPTION\r\n");
textBoxR.SelectionColor = Color.Red;
textBoxR.AppendText(e.Message);
if (e.InnerException != null)
{
textBoxR.AppendText("\r\n");
textBoxR.SelectionColor = Color.Red;
textBoxR.AppendText(e.InnerException.Message);
}
textBoxR.SelectionColor = Color.Red;
textBoxR.AppendText("\r\n>>>> END EXCEPTION\r\n");
textBoxR.SelectionColor = c;
}
// JH 1.3: Follow the rules by marshalling cross-thread calls using BeginInvoke. No problems reported
// with not doing this, but I should follow the rules and set a good example!
private delegate void InvokeDelegate(string s, int t);
public void ShowChar(string s, bool nl)
{
object[] p = new object[2];
int t = nl? 1: 2;
p[0] = s;
p[1] = t;
if (this.InvokeRequired)
this.BeginInvoke(new InvokeDelegate(InvokeFunction), p);
else
InvokeFunction(s, t);
}
public void ShowMsg(string s)
{
object[] p = new object[2];
int t = 3;
p[0] = s;
p[1] = t;
if (this.InvokeRequired)
this.BeginInvoke(new InvokeDelegate(InvokeFunction), p);
else
InvokeFunction(s, t);
}
public void SetIndics(bool CTS, bool DSR, bool RLSD, bool Rng)
{
object[] p = new object[2];
string s = (CTS? "1":"0") + (DSR? "1":"0") + (RLSD? "1":"0") + (Rng? "1":"0");
int t = 4;
p[0] = s;
p[1] = t;
if (this.InvokeRequired)
this.BeginInvoke(new InvokeDelegate(InvokeFunction), p);
else
InvokeFunction(s, t);
}
private void InvokeFunction(string s, int t)
{
switch (t)
{
case 1:
textBoxR.AppendText(s);
textBoxR.AppendText("\r\n");
break;
case 2:
textBoxR.AppendText(s);
break;
case 3:
Color c = textBoxR.SelectionColor;
textBoxR.SelectionColor = Color.Green;
textBoxR.AppendText("\r\n" + s + "\r\n");
textBoxR.SelectionColor = c;
break;
case 4:
radioButtonCTS.Checked = s.Substring(0, 1) == "1";
radioButtonDSR.Checked = s.Substring(1, 1) == "1";
radioButtonRLSD.Checked = s.Substring(2, 1) == "1";
radioButtonRng.Checked = s.Substring(3, 1) == "1";
break;
}
}
//========
private void Form_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
if (e.KeyCode == Keys.Back)
{
textBoxS.Text = "";
e.Handled = true;
}
if (e.KeyCode == Keys.Delete)
{
textBoxS.Text = "";
textBoxR.Text = "";
e.Handled = true;
}
}
private void checkBox_CheckedChanged(object sender, System.EventArgs e)
{
BaseTerm.term.OPClick((CheckBox)sender);
}
public void OnClose()
{
textBoxR.BackColor = Color.LightGray;
buttonStatus.Enabled = false;
buttonSet.Enabled = true;
radioButtonCTS.Checked = false;
radioButtonDSR.Checked = false;
radioButtonRLSD.Checked = false;
radioButtonRng.Checked = false;
checkBoxRTS.Checked = false;
checkBoxDTR.Checked = false;
checkBoxBK.Checked = false;
radioButtonCTS.Enabled = false;
radioButtonDSR.Enabled = false;
radioButtonRLSD.Enabled = false;
radioButtonRng.Enabled = false;
checkBoxRTS.Enabled = false;
checkBoxDTR.Enabled = false;
checkBoxBK.Enabled = false;
buttonO.Tag = "0";
buttonO.Text = "Offline";
if (BaseTerm.settingsFileName == "")
this.Text = "BaseTerm: Offline";
else
this.Text = "BaseTerm: Offline [" + BaseTerm.settingsFileName + "]";
}
public void OnOpen()
{
buttonO.Tag = "1";
buttonO.Text = "Online";
radioButtonCTS.Enabled = true;
radioButtonDSR.Enabled = true;
radioButtonRLSD.Enabled = true;
radioButtonRng.Enabled = true;
BaseTerm.term.setOPTicks(checkBoxDTR);
BaseTerm.term.setOPTicks(checkBoxRTS);
BaseTerm.term.setOPTicks(checkBoxBK);
buttonStatus.Enabled = true;
buttonSet.Enabled = false;
textBoxR.BackColor = Color.White;
this.Text = "BaseTerm Online: " + BaseTerm.settings.port;
}
private void buttonStatus_Click(object sender, System.EventArgs e)
{
if (info == null) info = new InfoForm();
if (info.IsDisposed) info = new InfoForm();
info.Show();
textBoxS.Focus();
}
private void buttonSet_Click(object sender, System.EventArgs e)
{
BaseTerm.term.Settings();
textBoxS.Focus();
}
private void buttonClr_Click(object sender, System.EventArgs e)
{
textBoxS.Text = "";
textBoxR.Text = "";
inEscape = false;
esc = "";
mark = "";
textBoxS.BackColor = Color.White;
textBoxS.Focus();
}
private void BaseTermForm_Load(object sender, System.EventArgs e)
{
textBoxS.Focus();
}
private void buttonImm_Click(object sender, System.EventArgs e)
{
if (buttonImm.Tag.ToString() == "0")
{
buttonImm.Tag = "1";
buttonImm.Text = "Immediate";
BaseTerm.term.Immediate = true;
}
else
{
buttonImm.Tag = "0";
buttonImm.Text = "Queued";
BaseTerm.term.Immediate = false;
}
textBoxS.Focus();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -