public void Show (Point position, ToolStripDropDownDirection direction)
{
this.PerformLayout ();
Point show_point = position;
Point max_screen = new Point (SystemInformation.VirtualScreen.Width, SystemInformation.VirtualScreen.Height);
if (this is ContextMenuStrip) {
// If we are going to go offscreen, adjust our direction so we don't...
// X direction
switch (direction) {
case ToolStripDropDownDirection.AboveLeft:
if (show_point.X - this.Width < 0)
direction = ToolStripDropDownDirection.AboveRight;
break;
case ToolStripDropDownDirection.BelowLeft:
if (show_point.X - this.Width < 0)
direction = ToolStripDropDownDirection.BelowRight;
break;
case ToolStripDropDownDirection.Left:
if (show_point.X - this.Width < 0)
direction = ToolStripDropDownDirection.Right;
break;
case ToolStripDropDownDirection.AboveRight:
if (show_point.X + this.Width > max_screen.X)
direction = ToolStripDropDownDirection.AboveLeft;
break;
case ToolStripDropDownDirection.BelowRight:
case ToolStripDropDownDirection.Default:
if (show_point.X + this.Width > max_screen.X)
direction = ToolStripDropDownDirection.BelowLeft;
break;
case ToolStripDropDownDirection.Right:
if (show_point.X + this.Width > max_screen.X)
direction = ToolStripDropDownDirection.Left;
break;
}
// Y direction
switch (direction) {
case ToolStripDropDownDirection.AboveLeft:
if (show_point.Y - this.Height < 0)
direction = ToolStripDropDownDirection.BelowLeft;
break;
case ToolStripDropDownDirection.AboveRight:
if (show_point.Y - this.Height < 0)
direction = ToolStripDropDownDirection.BelowRight;
break;
case ToolStripDropDownDirection.BelowLeft:
if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
direction = ToolStripDropDownDirection.AboveLeft;
break;
case ToolStripDropDownDirection.BelowRight:
case ToolStripDropDownDirection.Default:
if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
direction = ToolStripDropDownDirection.AboveRight;
break;
case ToolStripDropDownDirection.Left:
if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
direction = ToolStripDropDownDirection.AboveLeft;
break;
case ToolStripDropDownDirection.Right:
if (show_point.Y + this.Height > max_screen.Y && show_point.Y - this.Height > 0)
direction = ToolStripDropDownDirection.AboveRight;
break;
}
}
switch (direction) {
case ToolStripDropDownDirection.AboveLeft:
show_point.Y -= this.Height;
show_point.X -= this.Width;
break;
case ToolStripDropDownDirection.AboveRight:
show_point.Y -= this.Height;
break;
case ToolStripDropDownDirection.BelowLeft:
show_point.X -= this.Width;
break;
case ToolStripDropDownDirection.Left:
show_point.X -= this.Width;
break;
case ToolStripDropDownDirection.Right:
break;
}
// Fix offscreen horizontal positions
if ((show_point.X + this.Width) > max_screen.X)
show_point.X = max_screen.X - this.Width;
if (show_point.X < 0)
show_point.X = 0;
// Fix offscreen vertical positions
if ((show_point.Y + this.Height) > max_screen.Y)
show_point.Y = max_screen.Y - this.Height;
if (show_point.Y < 0)
show_point.Y = 0;
if (this.Location != show_point)
this.Location = show_point;
CancelEventArgs e = new CancelEventArgs ();
this.OnOpening (e);
if (e.Cancel)
return;
// The tracker lets us know when the form is clicked or loses focus
ToolStripManager.AppClicked += new EventHandler (ToolStripMenuTracker_AppClicked);
ToolStripManager.AppFocusChange += new EventHandler (ToolStripMenuTracker_AppFocusChange);
base.Show ();
ToolStripManager.SetActiveToolStrip (this, ToolStripManager.ActivatedByKeyboard);
this.OnOpened (EventArgs.Empty);
}