📄 mainform.cs
字号:
/*
MainForm class
--
Application's main window. Contains four sections:
1) Collects signature in custom control.
2) Displays signature info (segments and coordinates).
3) Action buttons.
4) Status bar at bottom.
User draw signature in custom control and presses the Send button
to send the signature to the server over TCP sockets. The signature
is flattened to a stream of bytes and encrypted using the crypto APIs.
*/
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text;
using Common;
namespace PocketSignature
{
/// <summary>
/// Main form.
/// </summary>
public class MainForm : System.Windows.Forms.Form
{
// socket communications
ClientSocket _client = new ClientSocket();
// custom signature control
SignatureControl _signature = new SignatureControl();
// handle updates from socket
EventHandler _processCommand;
// hold socket notification arguments
NotifyCommand _notifyCommand;
object _notifyData;
private System.Windows.Forms.Panel areaSignature;
private System.Windows.Forms.Button butNew;
private System.Windows.Forms.Button butConnect;
private System.Windows.Forms.Button butSend;
private System.Windows.Forms.TextBox textDetails;
private System.Windows.Forms.StatusBar statusBar;
private System.Windows.Forms.Button butOptions;
public MainForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
// add signature control to form
_signature.Location = areaSignature.Location;
_signature.Size = areaSignature.Size;
this.Controls.Add(_signature);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(MainForm));
this.statusBar = new System.Windows.Forms.StatusBar();
this.butConnect = new System.Windows.Forms.Button();
this.butSend = new System.Windows.Forms.Button();
this.areaSignature = new System.Windows.Forms.Panel();
this.butOptions = new System.Windows.Forms.Button();
this.butNew = new System.Windows.Forms.Button();
this.textDetails = new System.Windows.Forms.TextBox();
//
// statusBar
//
this.statusBar.Location = new System.Drawing.Point(0, 272);
this.statusBar.Size = new System.Drawing.Size(240, 24);
this.statusBar.Text = "Offline";
//
// butConnect
//
this.butConnect.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
this.butConnect.Location = new System.Drawing.Point(120, 248);
this.butConnect.Size = new System.Drawing.Size(54, 20);
this.butConnect.Text = "Connect";
this.butConnect.Click += new System.EventHandler(this.butConnect_Click);
//
// butSend
//
this.butSend.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
this.butSend.Location = new System.Drawing.Point(64, 248);
this.butSend.Size = new System.Drawing.Size(54, 20);
this.butSend.Text = "Send";
this.butSend.Click += new System.EventHandler(this.butSend_Click);
//
// areaSignature
//
this.areaSignature.BackColor = System.Drawing.Color.Gainsboro;
this.areaSignature.Location = new System.Drawing.Point(8, 8);
this.areaSignature.Size = new System.Drawing.Size(224, 120);
this.areaSignature.Visible = false;
//
// butOptions
//
this.butOptions.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
this.butOptions.Location = new System.Drawing.Point(176, 248);
this.butOptions.Size = new System.Drawing.Size(54, 20);
this.butOptions.Text = "Options";
this.butOptions.Click += new System.EventHandler(this.butOptions_Click);
//
// butNew
//
this.butNew.Font = new System.Drawing.Font("Tahoma", 8F, System.Drawing.FontStyle.Regular);
this.butNew.Location = new System.Drawing.Point(8, 248);
this.butNew.Size = new System.Drawing.Size(54, 20);
this.butNew.Text = "New";
this.butNew.Click += new System.EventHandler(this.butNew_Click);
//
// textDetails
//
this.textDetails.BackColor = System.Drawing.SystemColors.Info;
this.textDetails.Location = new System.Drawing.Point(8, 136);
this.textDetails.Multiline = true;
this.textDetails.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.textDetails.Size = new System.Drawing.Size(224, 104);
this.textDetails.Text = "";
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(240, 296);
this.Controls.Add(this.textDetails);
this.Controls.Add(this.areaSignature);
this.Controls.Add(this.butConnect);
this.Controls.Add(this.butSend);
this.Controls.Add(this.statusBar);
this.Controls.Add(this.butOptions);
this.Controls.Add(this.butNew);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MinimizeBox = false;
this.Text = "Pocket Signature";
this.Closing += new System.ComponentModel.CancelEventHandler(this.MainForm_Closing);
this.Load += new System.EventHandler(this.MainForm_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
Application.Run(new MainForm());
}
//
// events
//
private void MainForm_Load(object sender, System.EventArgs e)
{
// prompt user if have not set the server ip address
if (Global.Settings.GetString(SettingKeys.IpAddress) ==
Global.Const.EmptyAddress)
{
if (MessageBox.Show(
"Would you like to set the server IP address now?",
this.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) ==
DialogResult.Yes)
{
ShowOptionsForm();
}
}
// setup callbacks
_signature.SignatureUpdate += new EventHandler(SignatureUpdate);
_client.Notify += new ClientSocket.NotifyEventHandler(NotifyCallback);
_processCommand = new EventHandler(ProcessSocketCommand);
// enable command buttons
UpdateButtons();
// connect to server when first startup
ConnectToServer();
}
private void MainForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// disconnect from server when app closes
DisconnectFromServer();
}
private void butConnect_Click(object sender, System.EventArgs e)
{
ConnectToServer();
}
private void butNew_Click(object sender, System.EventArgs e)
{
// clear current signature
_signature.Clear();
textDetails.Text = "";
}
private void butSend_Click(object sender, System.EventArgs e)
{
try
{
statusBar.Text = "Sending signature...";
// encrypt the signature data
byte[] encryptData = Crypto.Encrypt(
Global.Settings.GetString(SettingKeys.CryptPassphrase),
_signature.SignatureBits);
UpdateSignatureDetails();
// send to server
_client.Send(encryptData);
}
catch (Exception ex)
{
statusBar.Text = "Error";
textDetails.Text = ex.Message;
}
}
private void butOptions_Click(object sender, System.EventArgs e)
{
// display the options form
ShowOptionsForm();
}
// notification from signature control
private void SignatureUpdate(object sender, EventArgs args)
{
// a new segment was added to the signature
UpdateSignatureDetails();
}
// notification from socket
private void NotifyCallback(NotifyCommand command, object data)
{
try
{
// The .Net Compact Framework does not support Control.Invoke
// that allows you to pass arguments to the delegate. So save
// arguments to class fields and then invoke the delegate.
lock(this)
{
// save arguments to class fields
_notifyCommand = command;
_notifyData = data;
// execute the method on the GUI's thread, this method
// uses the _notifyCommand and _notifyData fields
Invoke(_processCommand);
}
}
catch
{
}
}
//
// helper methods
//
private void UpdateSignatureDetails()
{
// display info on line segments
StringBuilder sb = new StringBuilder();
sb.AppendFormat(null, "Signature details\r\nNumber of segments : {0}", _signature.Lines.Count);
// loop through all line segments
for (int i=0; i < _signature.Lines.Count; i++)
{
// get points for this line segment
Point[] points = (Point[])_signature.Lines[i];
sb.AppendFormat(null, "\r\n\r\nSegment {0} ({1} points)\r\n", i+1, points.Length);
foreach (Point pt in points)
{
sb.AppendFormat(null, "({0},{1}) ", pt.X, pt.Y);
}
}
textDetails.Text = sb.ToString();
}
private void ConnectToServer()
{
// only connect if user has set the server IP address
if (Global.Settings.GetString(SettingKeys.IpAddress) == Global.Const.EmptyAddress)
{
textDetails.Text = "Need to set the server IP address.";
return;
}
statusBar.Text = "Connecting to server...";
_client.Connect(
Global.Settings.GetString(SettingKeys.IpAddress),
Global.Settings.GetInt(SettingKeys.PortNumber));
}
private void DisconnectFromServer()
{
statusBar.Text = "Offline";
_client.Disconnect();
}
private void ShowOptionsForm()
{
OptionsForm form = new OptionsForm();
form.ShowDialog();
}
// process socket notification, executes on GUI's thread
private void ProcessSocketCommand(object sender, EventArgs args)
{
string status = "";
switch (_notifyCommand)
{
case NotifyCommand.Connected:
bool connected = (bool)_notifyData;
status = connected ?
"Connected to server" :
"Offline";
break;
case NotifyCommand.SentData:
status = "Sent signature";
_client.Receive();
break;
case NotifyCommand.ReceivedData:
bool success = (bool)_notifyData;
status = success ?
"Server received signature" :
"Server did not receive signature";
break;
case NotifyCommand.Error:
textDetails.Text = "Socket error\r\n" + (string)_notifyData;
DisconnectFromServer();
break;
}
// enable action buttons
UpdateButtons();
// update the status bar
statusBar.Text = status;
}
// enable actions buttons
private void UpdateButtons()
{
butSend.Enabled = _client.Connected;
butConnect.Enabled = !butSend.Enabled;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -