📄 optionsform.cs
字号:
/*
OptionsForm class
--
Allows user to specify settings. The settings are stored in XML .config file.
Tries to initialize the server IP address to the server that has ActiveSync
installed by looking up information from the registry.
*/
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Net;
using System.Reflection;
namespace PocketSignature
{
/// <summary>
/// Options form.
/// </summary>
public class OptionsForm : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox listAdr1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ComboBox listAdr2;
private System.Windows.Forms.ComboBox listAdr3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textPassword;
private System.Windows.Forms.MainMenu mainMenu;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label lblVersion;
private System.Windows.Forms.ComboBox listAdr4;
public OptionsForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
}
/// <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(OptionsForm));
this.listAdr1 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.listAdr2 = new System.Windows.Forms.ComboBox();
this.listAdr3 = new System.Windows.Forms.ComboBox();
this.listAdr4 = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.textPassword = new System.Windows.Forms.TextBox();
this.mainMenu = new System.Windows.Forms.MainMenu();
this.label3 = new System.Windows.Forms.Label();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label4 = new System.Windows.Forms.Label();
this.lblVersion = new System.Windows.Forms.Label();
//
// listAdr1
//
this.listAdr1.Location = new System.Drawing.Point(8, 104);
this.listAdr1.Size = new System.Drawing.Size(48, 22);
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 88);
this.label1.Size = new System.Drawing.Size(152, 16);
this.label1.Text = "Server IP address";
//
// listAdr2
//
this.listAdr2.Location = new System.Drawing.Point(66, 104);
this.listAdr2.Size = new System.Drawing.Size(48, 22);
//
// listAdr3
//
this.listAdr3.Location = new System.Drawing.Point(124, 104);
this.listAdr3.Size = new System.Drawing.Size(48, 22);
//
// listAdr4
//
this.listAdr4.Location = new System.Drawing.Point(184, 104);
this.listAdr4.Size = new System.Drawing.Size(48, 22);
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 144);
this.label2.Size = new System.Drawing.Size(152, 16);
this.label2.Text = "Encryption passphrase";
//
// textPassword
//
this.textPassword.Location = new System.Drawing.Point(8, 160);
this.textPassword.Size = new System.Drawing.Size(224, 22);
this.textPassword.Text = "";
//
// label3
//
this.label3.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Bold);
this.label3.Location = new System.Drawing.Point(8, 8);
this.label3.Size = new System.Drawing.Size(128, 20);
this.label3.Text = "Pocket Signature";
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(200, 8);
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 44);
this.label4.Size = new System.Drawing.Size(224, 14);
this.label4.Text = "Developed by Vertigo Software, Inc.";
//
// lblVersion
//
this.lblVersion.Location = new System.Drawing.Point(8, 28);
this.lblVersion.Size = new System.Drawing.Size(176, 14);
this.lblVersion.Text = "Version";
//
// OptionsForm
//
this.Controls.Add(this.lblVersion);
this.Controls.Add(this.label4);
this.Controls.Add(this.pictureBox1);
this.Controls.Add(this.label3);
this.Controls.Add(this.textPassword);
this.Controls.Add(this.listAdr1);
this.Controls.Add(this.label1);
this.Controls.Add(this.listAdr2);
this.Controls.Add(this.listAdr3);
this.Controls.Add(this.listAdr4);
this.Controls.Add(this.label2);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Menu = this.mainMenu;
this.MinimizeBox = false;
this.Text = "Options";
this.Closing += new System.ComponentModel.CancelEventHandler(this.OptionsForm_Closing);
this.Load += new System.EventHandler(this.OptionsForm_Load);
}
#endregion
//
// events
//
private void OptionsForm_Load(object sender, System.EventArgs e)
{
// display version
AssemblyName name = Assembly.GetExecutingAssembly().GetName();
lblVersion.Text = string.Format("Version {0}.{1}.{2}",
name.Version.Major,
name.Version.Minor,
name.Version.Build);
// init fields with settings from config file
LoadSettings();
}
private void OptionsForm_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// save settings to config file
SaveSettings();
}
//
// helper methods
//
// save settings to config file
private void SaveSettings()
{
// password
Global.Settings.SetValue(
SettingKeys.CryptPassphrase,
textPassword.Text);
// ip address
Global.Settings.SetValue(SettingKeys.IpAddress,
string.Format("{0}.{1}.{2}.{3}",
listAdr1.SelectedIndex,
listAdr2.SelectedIndex,
listAdr3.SelectedIndex,
listAdr4.SelectedIndex));
}
// load settings from config file
private void LoadSettings()
{
// crypt password
textPassword.Text = Global.Settings.GetString(
SettingKeys.CryptPassphrase);
// init IP droplist values
for (int i=0; i <= 255; i++)
{
listAdr1.Items.Add(i);
listAdr2.Items.Add(i);
listAdr3.Items.Add(i);
listAdr4.Items.Add(i);
}
// ip address
string adr = Global.Settings.GetString(SettingKeys.IpAddress);
// if user has not specified an address, try to determine the
// ip address of the server (system running ActiveSync)
if (adr == Global.Const.EmptyAddress)
adr = GetDefaultAddress();
try
{
// select values in drop lists
string[] parts = adr.Split('.');
if (parts.Length == 4)
{
listAdr1.SelectedIndex = Convert.ToInt32(parts[0]);
listAdr2.SelectedIndex = Convert.ToInt32(parts[1]);
listAdr3.SelectedIndex = Convert.ToInt32(parts[2]);
listAdr4.SelectedIndex = Convert.ToInt32(parts[3]);
}
}
catch
{
// invalid ip address
}
}
// returns ip address of system running ActiveSync
private string GetDefaultAddress()
{
// registry path
const string RegPath = @"Software\Microsoft\Windows CE Services\Partners";
// first get current partner setting
int curPartner = Registry.GetInt(
Registry.RootKey.LocalMachine, RegPath, "PCur");
// return empty address if info is not in the registry
if (curPartner == 0)
return Global.Const.EmptyAddress;
// next, get host name for the partner
string hostName = Registry.GetString(
Registry.RootKey.LocalMachine,
string.Format(@"{0}\P{1}", RegPath, curPartner), "PName");
// return empty address if info is not in the registry
if (hostName == "")
return Global.Const.EmptyAddress;
// finally, return the ip address of the host
return GetHostAddress(hostName);
}
// return ip address for the specified host
private string GetHostAddress(string host)
{
string result = Global.Const.EmptyAddress;
try
{
// get ip address for the host
IPHostEntry entry = Dns.GetHostByName(host);
if (entry != null)
result = entry.AddressList[0].ToString();
}
catch
{
// could not resolve host
}
return result;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -