namespace FlyoutMenu
{
public partial class FlyoutMenu : Form
{
private FlyoutDirection _orientation = FlyoutDirection.Down;
private Point _location;
private Size _size;
public FlyoutMenu()
{
base.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
InitializeComponent(); //from the form generator in VS
base.ControlAdded += FlyoutMenu_ControlAdded;
base.FormClosing += FlyoutMenu_FormClosing;
base.Shown += FlyoutMenu_Shown;
}
void FlyoutMenu_Shown(object sender, EventArgs e)
{
//force the form to the size and location it should be despite setting it previously
//why doesn't the value stay what it should be?
base.Size = _size;
int X = (_orientation == FlyoutDirection.Right) ? _location.X : _location.X - base.Width + base.Controls[0].Width;
int Y = (_orientation == FlyoutDirection.Down) ? _location.Y : _location.Y - base.Height + base.Controls[0].Height;
base.Location = new Point
(X, Y
); }
void FlyoutMenu_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.Hide();
}
void FlyoutMenu_ControlAdded(object sender, ControlEventArgs e)
{
//allow only certain controls … there is a better way, but this just works
if (e
.Control.GetType().Equals(typeof(FlyoutButton
))) {
}
else if (e
.Control.GetType().Equals(typeof(FlyoutLabel
))) {
}
else if (e
.Control.GetType().Equals(typeof(FlyoutSeparator
))) {
}
else
{
e.Control.Dispose();
}
}
//set the orientation and size of the control
private void setOrientation()
{
this.Width = 16;
this.Height = 16;
int wid = 0;
int hgt = 0;
int MaxHgt = 0;
int MaxWdt = 0;
int minWid = 16;
int minHgt = 16;
foreach (Control con in base.Controls)
{
if (_orientation == FlyoutDirection.Left)
{
con.Left = -wid;
con.Top = 0;
}
else if (_orientation == FlyoutDirection.Right)
{
con.Left = wid;
con.Top = 0;
}
else if (_orientation == FlyoutDirection.Up)
{
con.Left = 0;
con.Top = -hgt;
}
else if (_orientation == FlyoutDirection.Down)
{
con.Left = 0;
con.Top = hgt;
}
wid += con.Width + con.Padding.Horizontal;
hgt += con.Height + con.Padding.Vertical;
MaxHgt = Math.Max(MaxHgt, con.Height + con.Padding.Vertical);
MaxWdt = Math.Max(MaxWdt, con.Width + con.Padding.Horizontal);
}
if (base.Controls.Count > 0)
{
int leftmin = Math.Abs(Math.Min(base.Controls[0].Left, base.Controls[base.Controls.Count - 1].Left));
int topmin = Math.Abs(Math.Min(base.Controls[0].Top, base.Controls[base.Controls.Count - 1].Top));
foreach (Control con in base.Controls)
{
con.Left += leftmin;
con.Top += topmin;
}
}
if ((_orientation == FlyoutDirection.Left)||(_orientation == FlyoutDirection.Right))
{
base.Width = Math.Max(wid, minWid);
base.Height = Math.Max(MaxHgt, minHgt);
}
else
{
base.Width = Math.Max(MaxWdt, minWid);
base.Height = Math.Max(hgt, minHgt);
}
_location = base.Location;
_size = base.Size;
}
public FlyoutDirection Orientation
{
get { return _orientation; }
set
{
_orientation = value;
setOrientation();
}
}
public enum FlyoutDirection
{
Down,
Right,
Up,
Left
}
}
//these will ultimately be the controls for the flyout
public class FlyoutButton: Button
{
public FlyoutButton(){}
}
public class FlyoutLabel : Label
{
public FlyoutLabel(){}
}
public class FlyoutSeparator : ToolStripSeparator
{
public FlyoutSeparator(){}
}
}