Permalink
Browse files

win app: prelim wrapper gui work

  • Loading branch information...
1 parent 308dfc6 commit 4cc1e1edf06a3dd8fb926496f704c44f4f642643 @todbot committed Jan 6, 2013
@@ -41,6 +41,7 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
+ <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
@@ -59,6 +60,7 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Blink1Event.cs" />
+ <Compile Include="MinimizeToTray.cs" />
<Compile Include="MySettings.cs" />
<Page Include="MainWindow.xaml">
<Generator>MSBuild:Compile</Generator>
@@ -210,17 +210,24 @@ public static string getContentsOfUrl(string url)
// stolen from: http://stackoverflow.com/questions/9961220/webclient-read-content-of-error-page
Console.WriteLine("getContentsOfUrl:" + url); // FIXME: how to do debug logging better?
string content = null;
+ if (url == null) return null;
WebClient webclient = new WebClient();
try {
content = webclient.DownloadString(url);
}
catch (WebException we) {
// WebException.Status holds useful information
- Console.WriteLine(we.Message + "\n" + we.Status.ToString()); // FIXME:
+ Console.WriteLine(we.Message); // + "\n" + we.Status.ToString()); // FIXME:
+ /*
Stream receiveStream = we.Response.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader readStream = new StreamReader(receiveStream, encode);
content = readStream.ReadToEnd();
+ Console.WriteLine("content: " + content);
+ */
+ content = null;
+ }
+ catch (System.ArgumentException sae) {
}
catch (NotSupportedException ne) {
Console.WriteLine(ne.Message); // other errors
@@ -1,7 +1,7 @@
<Window x:Class="Blink1Control.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="MainWindow" Height="350" Width="525">
+ Title="Blink1Control" Height="350" Width="525">
<Grid>
</Grid>
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@@ -24,9 +25,31 @@ public partial class MainWindow : Window
{
Blink1Server blink1Server = new Blink1Server();
+ public bool disposeTime = false;
+
public MainWindow()
{
InitializeComponent();
+
+ MinimizeToTray.Enable(this);
+
+ }
+
+ protected override void OnClosing(CancelEventArgs e)
+ {
+ if (disposeTime) {
+ //close it
+ Console.WriteLine("disposeTime!");
+ blink1Server.shutdown();
+ // FIXME: is this the right thing to do?
+ System.Windows.Forms.Application.Exit();
+ System.Diagnostics.Process.GetCurrentProcess().Kill();
+ }
+ else {
+ base.OnClosing(e);
+ e.Cancel = true;
+ this.WindowState = WindowState.Minimized;
+ }
}
}
}
@@ -0,0 +1,115 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+using System.Drawing;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Forms;
+
+namespace Blink1Control
+{
+ // this is from justin's work
+ // should also see: http://www.codeproject.com/Articles/27599/Minimize-window-to-system-tray
+ // and: http://stackoverflow.com/questions/2708371/how-can-i-have-my-windows-form-application-minimize-to-system-taskbar
+
+ /// <summary>
+ /// Class implementing support for "minimize to tray" functionality.
+ /// </summary>
+ public static class MinimizeToTray
+ {
+ /// <summary>
+ /// Enables "minimize to tray" behavior for the specified Window.
+ /// </summary>
+ /// <param name="window">Window to enable the behavior for.</param>
+ public static void Enable(MainWindow window)
+ {
+ // No need to track this instance; its event handlers will keep it alive
+ new MinimizeToTrayInstance(window);
+ }
+
+ /// <summary>
+ /// Class implementing "minimize to tray" functionality for a Window instance.
+ /// </summary>
+ private class MinimizeToTrayInstance
+ {
+ private MainWindow _window;
+ private NotifyIcon _notifyIcon;
+ private System.Windows.Forms.MenuItem menuItem1;
+
+ private System.Windows.Forms.MenuItem menuItem2;
+ /// <summary>
+ /// Initializes a new instance of the MinimizeToTrayInstance class.
+ /// </summary>
+ /// <param name="window">Window instance to attach to.</param>
+ public MinimizeToTrayInstance(MainWindow window)
+ {
+ Debug.Assert(window != null, "window parameter is null.");
+ _window = window;
+ _window.StateChanged += new EventHandler(HandleStateChanged);
+ // Initialize menuItem1 this.menuItem1.Index = 0;
+ this.menuItem1 = new MenuItem();
+ this.menuItem2 = new MenuItem();
+ this.menuItem1.Text = "E&xit";
+ this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
+ this.menuItem2.Text = "Settings";
+ this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
+ }
+
+ /// <summary>
+ /// Handles the Window's StateChanged event.
+ /// </summary>
+ /// <param name="sender">Event source.</param>
+ /// <param name="e">Event arguments.</param>
+ private void HandleStateChanged(object sender, EventArgs e)
+ {
+ if (_notifyIcon == null) {
+ // Initialize NotifyIcon instance "on demand"
+ _notifyIcon = new NotifyIcon();
+ _notifyIcon.Icon = Icon.ExtractAssociatedIcon(Assembly.GetEntryAssembly().Location);
+ _notifyIcon.MouseDoubleClick += new MouseEventHandler(HandleNotifyIconOrBalloonClicked);
+ _notifyIcon.BalloonTipClicked += new EventHandler(HandleNotifyIconOrBalloonClicked);
+ _notifyIcon.ContextMenu = new ContextMenu();
+ _notifyIcon.ContextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { menuItem1, menuItem2 });
+ _notifyIcon.Visible = true;
+ }
+ // Update copy of Window Title in case it has changed
+ _notifyIcon.Text = _window.Title;
+
+ // Show/hide Window and NotifyIcon
+ var minimized = (_window.WindowState == WindowState.Minimized);
+ //_window.ShowInTaskbar = true;
+ _window.ShowInTaskbar = !minimized;
+ //_notifyIcon.Visible = minimized;
+ if (minimized) {
+ _notifyIcon.ShowBalloonTip(1000, null, _window.Title, ToolTipIcon.None);
+ }
+ }
+
+
+ /// <summary>
+ /// Handles a click on the notify icon or its balloon.
+ /// </summary>
+ /// <param name="sender">Event source.</param>
+ /// <param name="e">Event arguments.</param>
+ private void HandleNotifyIconOrBalloonClicked(object sender, EventArgs e)
+ {
+ // Restore the Window
+ _window.WindowState = WindowState.Normal;
+ }
+ private void menuItem1_Click(object Sender, EventArgs e)
+ {
+ // Close the form, which closes the application.
+ _window.disposeTime = true;
+ _window.Close();
+ }
+ private void menuItem2_Click(object Sender, EventArgs e)
+ {
+ // Restore the Window and reload
+ _window.WindowState = WindowState.Normal;
+ //
+ }
+ }
+ }
+}
@@ -1,106 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using System.Xml.Serialization;
-using System.IO;
-
-namespace Blink1Control
-{
- /*
- using System.Collections.Generic;
- using System.Configuration;
-
- /// <summary>
- /// Persistent store for my parameters.
- /// </summary>
- public class MySettings : ApplicationSettingsBase
- {
- /// <summary>
- /// The instance lock.
- /// </summary>
- private static readonly object InstanceLock = new object();
-
- /// <summary>
- /// The instance.
- /// </summary>
- private static MySettings instance;
-
- /// <summary>
- /// Prevents a default instance of the <see cref="MySettings"/> class
- /// from being created.
- /// </summary>
- private MySettings()
- {
- // don't need to do anything
- }
-
- /// <summary>
- /// Gets the singleton.
- /// </summary>
- public static MySettings Instance
- {
- get
- {
- lock (InstanceLock)
- {
- if (instance == null)
- {
- instance = new MySettings();
- }
- }
-
- return instance;
- }
- }
-
- /// <summary>
- /// Gets or sets the parameters.
- /// </summary>
- [UserScopedSetting]
- [SettingsSerializeAs(SettingsSerializeAs.Binary)]
- public Dictionary<string, string> Parameters
- {
- get
- {
- return (Dictionary<string, string>)this["Parameters"];
- }
-
- set
- {
- this["Parameters"] = value;
- }
- }
- }
- */
-
- public class MySettings
- {
- public Dictionary<string, string> Parameters { get; set; }
-
- public MySettings()
- {
- Parameters = new Dictionary<string,string>();
- }
- public static void saveSettings(MySettings settings)
- {
- Console.WriteLine("saveSettings!");
- XmlSerializer mySerializer = new XmlSerializer(typeof(MySettings));
- StreamWriter myWriter = new StreamWriter("c:/prefs.xml");
- mySerializer.Serialize(myWriter, settings);
- myWriter.Close();
- }
-
- public static MySettings loadSettings()
- {
- Console.WriteLine("loadSettings!");
- XmlSerializer mySerializer = new XmlSerializer(typeof(MySettings));
- FileStream myFileStream = new FileStream("c:/prefs.xml", FileMode.Open);
- MySettings settings = (MySettings)mySerializer.Deserialize(myFileStream);
- return settings;
- }
- }
-
-}
-
Oops, something went wrong.

0 comments on commit 4cc1e1e

Please sign in to comment.