Skip to content

Instantly share code, notes, and snippets.

@amenayach
Created August 11, 2016 07:34
Show Gist options
  • Save amenayach/6ceafb934e7f45b237eeddc11708ca4e to your computer and use it in GitHub Desktop.
Save amenayach/6ceafb934e7f45b237eeddc11708ca4e to your computer and use it in GitHub Desktop.
Mac.cs is winforms class that makes a Form opens with sliding motion from one of four directions selected by the user. Usage: Mac.Slide(form);
//By Amen Ayach
using System;
using System.Windows.Forms;
using System.Drawing;
public class Mac
{
public enum eDirection
{
Up,
Down,
Right,
Left
}
eDirection dir = eDirection.Down;
Form frm = null;
Point centerScreen;
public Mac(Form _frm, eDirection _Direction)
{
frm = _frm;
dir = _Direction;
if (frm != null && !frm.IsDisposed)
{
//Initializing points
centerScreen = new Point((Screen.PrimaryScreen.Bounds.Width - frm.Width) / 2, (Screen.PrimaryScreen.Bounds.Height - frm.Height) / 2);
//Initializing form location
frm.StartPosition = FormStartPosition.Manual;
switch (dir)
{
case eDirection.Up:
frm.Top = Screen.PrimaryScreen.Bounds.Height + frm.Height + 10;
frm.Left = centerScreen.X;
break;
case eDirection.Down:
frm.Top = -(frm.Height + 10);
frm.Left = centerScreen.X;
break;
case eDirection.Left:
frm.Left = Screen.PrimaryScreen.Bounds.Width + frm.Width + 10;
frm.Top = centerScreen.Y;
break;
case eDirection.Right:
frm.Left = -(frm.Width + 10);
frm.Top = centerScreen.Y;
break;
}
//Attaching event
frm.Load += FromLoad;
}
}
private void FromLoad(object sender, EventArgs e)
{
System.Windows.Forms.Timer tm = new System.Windows.Forms.Timer
{
Interval = 1,
Enabled = true
};
tm.Tick += TmTick;
tm.Start();
}
private void TmTick(object sender, EventArgs e)
{
Timer tm = (System.Windows.Forms.Timer)sender;
try
{
tm.Stop();
const int increm = 50;
switch (dir)
{
case eDirection.Up:
if (frm.Top > centerScreen.Y)
{
frm.Top -= increm;
}
else
{
StopTimer(ref tm);
}
break;
case eDirection.Down:
if (frm.Top < centerScreen.Y)
{
frm.Top += increm;
}
else
{
StopTimer(ref tm);
}
break;
case eDirection.Right:
if (frm.Left < centerScreen.X)
{
frm.Left += increm;
}
else
{
StopTimer(ref tm);
}
break;
case eDirection.Left:
if (frm.Left > centerScreen.X)
{
frm.Left -= increm;
}
else
{
StopTimer(ref tm);
}
break;
}
if (tm != null)
{
tm.Start();
}
else
{
frm.Location = centerScreen;
}
}
catch
{
//Ignored
}
}
private void StopTimer(ref System.Windows.Forms.Timer aTimer)
{
if (aTimer != null)
{
aTimer.Stop();
aTimer.Dispose();
aTimer = null;
}
}
public static void Slide(Form frm, eDirection _Go = eDirection.Down)
{
Mac c = new Mac(frm, _Go);
}
}
@amenayach
Copy link
Author

Usage:

Mac.Slide(form);
// Or
Mac.Slide(form, Mac.eDirection.Up);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment