From 3356c41f170e188b0d7a6fc04d396e224821ab86 Mon Sep 17 00:00:00 2001 From: Mark Sylvia Date: Mon, 16 Dec 2013 15:55:33 -0500 Subject: [PATCH] Added snap to edge of window feature --- SpotSharp.Tests/SpotSharpTests.cs | 2 +- SpotSharp/SpotClient.cs | 3 +- WindowSpot/Main.cs | 65 +++++++++++++++++++++- WindowSpot/Properties/Settings.Designer.cs | 16 +++++- WindowSpot/Setup.Designer.cs | 36 ++++++++++-- WindowSpot/Setup.cs | 8 +++ 6 files changed, 121 insertions(+), 9 deletions(-) diff --git a/SpotSharp.Tests/SpotSharpTests.cs b/SpotSharp.Tests/SpotSharpTests.cs index 7172d28..d4e0549 100644 --- a/SpotSharp.Tests/SpotSharpTests.cs +++ b/SpotSharp.Tests/SpotSharpTests.cs @@ -6,7 +6,7 @@ namespace SpotSharp.Tests [TestClass] public class SpotSharpTests { - const string Host = "http://192.168.3.174:5051"; + const string Host = "http://192.168.3.175:5051"; readonly SpotClient _spot = new SpotClient(Host); [TestMethod] diff --git a/SpotSharp/SpotClient.cs b/SpotSharp/SpotClient.cs index 2c08953..4b44307 100644 --- a/SpotSharp/SpotClient.cs +++ b/SpotSharp/SpotClient.cs @@ -27,7 +27,8 @@ public string Playing() public Image AlbumArt() { var bits = Get("/playing.png").RawBytes; - if (bits == null) return ServerDownImage(); + if (bits == null || bits.Length<10) + return ServerDownImage(); return Image.FromStream(new MemoryStream(bits)); } diff --git a/WindowSpot/Main.cs b/WindowSpot/Main.cs index f06832c..4d552c3 100644 --- a/WindowSpot/Main.cs +++ b/WindowSpot/Main.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using SpotSharp; +using System.Runtime.InteropServices; using Timer = System.Threading.Timer; namespace WindowSpot @@ -10,6 +11,8 @@ namespace WindowSpot public partial class Main : Form { SpotClient _spot; + private const int SnapInDistance = 50; + public Main() { @@ -74,7 +77,7 @@ private void NextClicked(object sender, EventArgs e) private void BackClicked(object sender, EventArgs e) { - _spot.Back(); + _spot.Back(); } private void SayIt(object sender, KeyEventArgs e) @@ -111,6 +114,66 @@ private void Poll(object sender, EventArgs e) { Sync(); } + + + public struct WindowPos + { + public IntPtr hwnd; + public IntPtr hwndInsertAfter; + public int x; + public int y; + public int cx; + public int cy; + public uint flags; + } + + private const int WM_WINDOWPOSCHANGING = 0x46; + + protected override void WndProc(ref Message m) + { + bool Snapped = false; + if ((m.Msg == WM_WINDOWPOSCHANGING) && (Properties.Settings.Default.SnapToEdge)) + { + Screen scn = Screen.FromPoint(this.Location); + WindowPos mwp; + mwp = (WindowPos)Marshal.PtrToStructure(m.LParam, typeof(WindowPos)); + if (mwp.x != 0) + { + // Left + if (mwp.x <= (scn.WorkingArea.Left + SnapInDistance)) + { + mwp.x = scn.WorkingArea.Left; + Snapped = true; + } + // Right + if ((mwp.x + mwp.cx) >= (scn.WorkingArea.Right - SnapInDistance)) + { + mwp.x = scn.WorkingArea.Right - mwp.cx; + Snapped = true; + } + // Top + if (mwp.y <= (scn.WorkingArea.Top + SnapInDistance)) + { + mwp.y = scn.WorkingArea.Top; + Snapped = true; + } + // Bottom + if ((mwp.y + mwp.cy) >= (scn.WorkingArea.Bottom - SnapInDistance)) + { + mwp.y = scn.WorkingArea.Bottom - mwp.cy; + Snapped = true; + } + // Keep from moving off the screen + if (mwp.x < scn.WorkingArea.Left) mwp.x = scn.WorkingArea.Left; + if ((mwp.x + mwp.cx) > scn.WorkingArea.Right) mwp.x = scn.WorkingArea.Right - mwp.cx; + if (mwp.y < scn.WorkingArea.Top) mwp.y = scn.WorkingArea.Top; + if ((mwp.y + mwp.cy) > scn.WorkingArea.Bottom) mwp.y = scn.WorkingArea.Bottom - mwp.cy; + Marshal.StructureToPtr(mwp, (IntPtr)m.LParam,false); + } + m.Result = (IntPtr)0; + } + base.WndProc(ref m); + } } internal class SpotState diff --git a/WindowSpot/Properties/Settings.Designer.cs b/WindowSpot/Properties/Settings.Designer.cs index ce6253a..0fc4fcb 100644 --- a/WindowSpot/Properties/Settings.Designer.cs +++ b/WindowSpot/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.18033 +// Runtime Version:4.0.30319.18052 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -34,5 +34,19 @@ public string Host { this["Host"] = value; } } + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("false")] + public bool SnapToEdge { + get { + return ((bool)(this["SnapToEdge"])); + } + set + { + this["SnapToEdge"] = value; + } + } + + } } diff --git a/WindowSpot/Setup.Designer.cs b/WindowSpot/Setup.Designer.cs index 2f8f25a..c5b4b60 100644 --- a/WindowSpot/Setup.Designer.cs +++ b/WindowSpot/Setup.Designer.cs @@ -32,6 +32,8 @@ private void InitializeComponent() this.txtHost = new System.Windows.Forms.TextBox(); this.btnTest = new System.Windows.Forms.Button(); this.btnSave = new System.Windows.Forms.Button(); + this.button1 = new System.Windows.Forms.Button(); + this.cboxSnapToEdge = new System.Windows.Forms.CheckBox(); this.SuspendLayout(); // // label1 @@ -50,12 +52,12 @@ private void InitializeComponent() this.txtHost.Location = new System.Drawing.Point(24, 45); this.txtHost.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.txtHost.Name = "txtHost"; - this.txtHost.Size = new System.Drawing.Size(498, 22); + this.txtHost.Size = new System.Drawing.Size(298, 22); this.txtHost.TabIndex = 0; // // btnTest // - this.btnTest.Location = new System.Drawing.Point(529, 39); + this.btnTest.Location = new System.Drawing.Point(340, 34); this.btnTest.Name = "btnTest"; this.btnTest.Size = new System.Drawing.Size(75, 33); this.btnTest.TabIndex = 1; @@ -65,19 +67,41 @@ private void InitializeComponent() // // btnSave // - this.btnSave.Location = new System.Drawing.Point(610, 39); + this.btnSave.Location = new System.Drawing.Point(258, 244); this.btnSave.Name = "btnSave"; this.btnSave.Size = new System.Drawing.Size(75, 33); this.btnSave.TabIndex = 2; - this.btnSave.Text = "Save"; + this.btnSave.Text = "OK"; this.btnSave.UseVisualStyleBackColor = true; this.btnSave.Click += new System.EventHandler(this.SaveClicked); // + // button1 + // + this.button1.Location = new System.Drawing.Point(340, 244); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 33); + this.button1.TabIndex = 3; + this.button1.Text = "Cancel"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // cboxSnapToEdge + // + this.cboxSnapToEdge.AutoSize = true; + this.cboxSnapToEdge.Location = new System.Drawing.Point(24, 84); + this.cboxSnapToEdge.Name = "cboxSnapToEdge"; + this.cboxSnapToEdge.Size = new System.Drawing.Size(208, 24); + this.cboxSnapToEdge.TabIndex = 4; + this.cboxSnapToEdge.Text = "Snap to the Screen Edge"; + this.cboxSnapToEdge.UseVisualStyleBackColor = true; + // // Setup // this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; - this.ClientSize = new System.Drawing.Size(717, 101); + this.ClientSize = new System.Drawing.Size(436, 296); + this.Controls.Add(this.cboxSnapToEdge); + this.Controls.Add(this.button1); this.Controls.Add(this.btnSave); this.Controls.Add(this.btnTest); this.Controls.Add(this.txtHost); @@ -98,5 +122,7 @@ private void InitializeComponent() private System.Windows.Forms.TextBox txtHost; private System.Windows.Forms.Button btnTest; private System.Windows.Forms.Button btnSave; + private System.Windows.Forms.Button button1; + private System.Windows.Forms.CheckBox cboxSnapToEdge; } } \ No newline at end of file diff --git a/WindowSpot/Setup.cs b/WindowSpot/Setup.cs index 5c5fa39..240eea3 100644 --- a/WindowSpot/Setup.cs +++ b/WindowSpot/Setup.cs @@ -21,6 +21,7 @@ private void SaveClicked(object sender, EventArgs e) { DialogResult = DialogResult.OK; Properties.Settings.Default.Host = txtHost.Text; + Properties.Settings.Default.SnapToEdge = cboxSnapToEdge.Checked; Properties.Settings.Default.Save(); Close(); } @@ -28,6 +29,13 @@ private void SaveClicked(object sender, EventArgs e) private void Setup_Load(object sender, EventArgs e) { txtHost.Text = Properties.Settings.Default.Host; + cboxSnapToEdge.Checked = Properties.Settings.Default.SnapToEdge; + } + + private void button1_Click(object sender, EventArgs e) + { + DialogResult = DialogResult.Cancel; + Close(); } } }