Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added snap to edge of window feature #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SpotSharp.Tests/SpotSharpTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion SpotSharp/SpotClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
65 changes: 64 additions & 1 deletion WindowSpot/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
using System.Threading.Tasks;
using System.Windows.Forms;
using SpotSharp;
using System.Runtime.InteropServices;
using Timer = System.Threading.Timer;

namespace WindowSpot
{
public partial class Main : Form
{
SpotClient _spot;
private const int SnapInDistance = 50;


public Main()
{
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion WindowSpot/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 31 additions & 5 deletions WindowSpot/Setup.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions WindowSpot/Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,21 @@ 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();
}

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();
}
}
}