Browse Source

implement window events

tags/2.3
clowwindy 11 years ago
parent
commit
e01a50c42a
4 changed files with 112 additions and 17 deletions
  1. +21
    -2
      shadowsocks-csharp/Config.cs
  2. +27
    -7
      shadowsocks-csharp/Form1.Designer.cs
  3. +63
    -7
      shadowsocks-csharp/Form1.cs
  4. +1
    -1
      shadowsocks-csharp/shadowsocks-csharp.csproj

+ 21
- 2
shadowsocks-csharp/Config.cs View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Runtime.Serialization.Json; using System.Runtime.Serialization.Json;
using System.Text; using System.Text;
using System.IO; using System.IO;
using System.Diagnostics;
namespace shadowsocks_csharp namespace shadowsocks_csharp
{ {
@@ -14,6 +15,17 @@ namespace shadowsocks_csharp
public int local_port; public int local_port;
public string password; public string password;
[NonSerialized]
public bool isDefault;
private static void assert(bool condition)
{
if(!condition)
{
throw new Exception("assertion failure");
}
}
public static Config Load() public static Config Load()
{ {
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config)); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Config));
@@ -22,17 +34,24 @@ namespace shadowsocks_csharp
using (FileStream fs = File.OpenRead(@"config.json")) using (FileStream fs = File.OpenRead(@"config.json"))
{ {
Config config = ser.ReadObject(fs) as Config; Config config = ser.ReadObject(fs) as Config;
assert(!string.IsNullOrEmpty(config.server));
assert(!string.IsNullOrEmpty(config.password));
assert(config.local_port > 0);
assert(config.server_port > 0);
config.isDefault = false;
return config; return config;
} }
} }
catch (IOException)
catch (Exception e)
{ {
Console.WriteLine(e);
return new Config return new Config
{ {
server = "127.0.0.1", server = "127.0.0.1",
server_port = 8388, server_port = 8388,
local_port = 1081, local_port = 1081,
password = "barfoo!"
password = "barfoo!",
isDefault = true
}; };
} }
} }


+ 27
- 7
shadowsocks-csharp/Form1.Designer.cs View File

@@ -45,7 +45,9 @@
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.ConfigItem = new System.Windows.Forms.ToolStripMenuItem(); this.ConfigItem = new System.Windows.Forms.ToolStripMenuItem();
this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.QuitItem = new System.Windows.Forms.ToolStripMenuItem(); this.QuitItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.tableLayoutPanel1.SuspendLayout(); this.tableLayoutPanel1.SuspendLayout();
this.panel1.SuspendLayout(); this.panel1.SuspendLayout();
this.contextMenuStrip1.SuspendLayout(); this.contextMenuStrip1.SuspendLayout();
@@ -175,7 +177,7 @@
this.button2.TabIndex = 1; this.button2.TabIndex = 1;
this.button2.Text = "Cancel"; this.button2.Text = "Cancel";
this.button2.UseVisualStyleBackColor = true; this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
this.button2.Click += new System.EventHandler(this.cancelButton_Click);
// //
// button1 // button1
// //
@@ -186,39 +188,54 @@
this.button1.TabIndex = 0; this.button1.TabIndex = 0;
this.button1.Text = "OK"; this.button1.Text = "OK";
this.button1.UseVisualStyleBackColor = true; this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.Click += new System.EventHandler(this.OKButton_Click);
// //
// notifyIcon1 // notifyIcon1
// //
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1; this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "notifyIcon1";
this.notifyIcon1.Text = "shadowsocks";
this.notifyIcon1.Visible = true; this.notifyIcon1.Visible = true;
this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick);
// //
// contextMenuStrip1 // contextMenuStrip1
// //
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.ConfigItem, this.ConfigItem,
this.aboutToolStripMenuItem,
this.toolStripSeparator2,
this.QuitItem}); this.QuitItem});
this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System; this.contextMenuStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
this.contextMenuStrip1.ShowImageMargin = false;
this.contextMenuStrip1.Size = new System.Drawing.Size(86, 48);
this.contextMenuStrip1.Size = new System.Drawing.Size(111, 76);
// //
// ConfigItem // ConfigItem
// //
this.ConfigItem.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Bold);
this.ConfigItem.Name = "ConfigItem"; this.ConfigItem.Name = "ConfigItem";
this.ConfigItem.Size = new System.Drawing.Size(85, 22);
this.ConfigItem.Size = new System.Drawing.Size(110, 22);
this.ConfigItem.Text = "Config"; this.ConfigItem.Text = "Config";
this.ConfigItem.Click += new System.EventHandler(this.Config_Click); this.ConfigItem.Click += new System.EventHandler(this.Config_Click);
// //
// aboutToolStripMenuItem
//
this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem";
this.aboutToolStripMenuItem.Size = new System.Drawing.Size(110, 22);
this.aboutToolStripMenuItem.Text = "About";
this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click);
//
// QuitItem // QuitItem
// //
this.QuitItem.Name = "QuitItem"; this.QuitItem.Name = "QuitItem";
this.QuitItem.Size = new System.Drawing.Size(85, 22);
this.QuitItem.Size = new System.Drawing.Size(110, 22);
this.QuitItem.Text = "Quit"; this.QuitItem.Text = "Quit";
this.QuitItem.Click += new System.EventHandler(this.Quit_Click); this.QuitItem.Click += new System.EventHandler(this.Quit_Click);
// //
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(107, 6);
//
// Form1 // Form1
// //
this.AcceptButton = this.button1; this.AcceptButton = this.button1;
@@ -233,6 +250,7 @@
this.Name = "Form1"; this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Shadowsocks"; this.Text = "Shadowsocks";
this.Load += new System.EventHandler(this.Form1_Load);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed); this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
this.tableLayoutPanel1.ResumeLayout(false); this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout(); this.tableLayoutPanel1.PerformLayout();
@@ -261,6 +279,8 @@
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem ConfigItem; private System.Windows.Forms.ToolStripMenuItem ConfigItem;
private System.Windows.Forms.ToolStripMenuItem QuitItem; private System.Windows.Forms.ToolStripMenuItem QuitItem;
private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator2;
} }
} }

+ 63
- 7
shadowsocks-csharp/Form1.cs View File

@@ -5,6 +5,7 @@ using System.Data;
using System.Drawing; using System.Drawing;
using System.Text; using System.Text;
using System.Windows.Forms; using System.Windows.Forms;
using System.Diagnostics;
namespace shadowsocks_csharp namespace shadowsocks_csharp
{ {
@@ -16,14 +17,36 @@ namespace shadowsocks_csharp
public Form1() public Form1()
{ {
config = Config.Load(); config = Config.Load();
reload(config);
InitializeComponent(); InitializeComponent();
configToTextBox();
}
private void showWindow()
{
this.Opacity = 1;
this.Show();
}
private void configToTextBox()
{
textBox1.Text = config.server; textBox1.Text = config.server;
textBox2.Text = config.server_port.ToString(); textBox2.Text = config.server_port.ToString();
textBox3.Text = config.password; textBox3.Text = config.password;
textBox4.Text = config.local_port.ToString(); textBox4.Text = config.local_port.ToString();
} }
private void Form1_Load(object sender, EventArgs e)
{
if (!config.isDefault)
{
this.Opacity = 0;
reload(config); BeginInvoke(new MethodInvoker(delegate
{
this.Hide();
}));
}
}
private void reload(Config config) private void reload(Config config)
{ {
if (local != null) if (local != null)
@@ -37,22 +60,45 @@ namespace shadowsocks_csharp
private void Config_Click(object sender, EventArgs e) private void Config_Click(object sender, EventArgs e)
{ {
showWindow();
} }
private void Quit_Click(object sender, EventArgs e) private void Quit_Click(object sender, EventArgs e)
{ {
this.Close();
} }
private void button1_Click(object sender, EventArgs e)
private void OKButton_Click(object sender, EventArgs e)
{ {
reload(Config.Load());
try
{
Config config = new Config
{
server = textBox1.Text,
server_port = int.Parse(textBox2.Text),
password = textBox3.Text,
local_port = int.Parse(textBox4.Text),
isDefault = false
};
Config.Save(config);
this.config = config;
reload(config);
this.Hide();
}
catch (FormatException)
{
MessageBox.Show("there is format problem");
}
catch (Exception)
{
MessageBox.Show("there is some problem with parameters");
}
} }
private void button2_Click(object sender, EventArgs e)
private void cancelButton_Click(object sender, EventArgs e)
{ {
this.Hide();
configToTextBox();
} }
private void Form1_FormClosed(object sender, FormClosedEventArgs e) private void Form1_FormClosed(object sender, FormClosedEventArgs e)
@@ -60,5 +106,15 @@ namespace shadowsocks_csharp
local.Stop(); local.Stop();
} }
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start("https://github.com/clowwindy/shadowsocks-csharp");
}
private void notifyIcon1_DoubleClick(object sender, EventArgs e)
{
showWindow();
}
} }
} }

+ 1
- 1
shadowsocks-csharp/shadowsocks-csharp.csproj View File

@@ -6,7 +6,7 @@
<ProductVersion>9.0.21022</ProductVersion> <ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion> <SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}</ProjectGuid> <ProjectGuid>{8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}</ProjectGuid>
<OutputType>Exe</OutputType>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>shadowsocks_csharp</RootNamespace> <RootNamespace>shadowsocks_csharp</RootNamespace>
<AssemblyName>shadowsocks-csharp</AssemblyName> <AssemblyName>shadowsocks-csharp</AssemblyName>


Loading…
Cancel
Save