private void OnMouseMove(Object s, MouseEventArgs e) { // What item are we over? int newMouseItem = ItemFromPoint(new Point(e.X, e.Y)); // Dont worry if the mouse is still on the same item. if (newMouseItem != currentMouseItem) { // Draw the menu by un-highlighting the old and highlighting the new. using (Graphics g = popupControl.CreateGraphics()) { if (currentMouseItem != -1) { DrawMenuItem(g, currentMouseItem, false); } if (newMouseItem != -1) { DrawMenuItem(g, newMouseItem, true); } } currentMouseItem = newMouseItem; StartTimer(newMouseItem); } // Do we need to pass the mouse move along? if (MouseMove != null) { // Convert the mouse co-ordinates relative to the associated control (form). MouseMove(this, CreateParentMouseArgs(e)); } }
// Show this context menu at the specified control-relative co-ordinates. public void Show(Control control, Point pos) { associatedControl = control; currentMouseItem = -1; // Not over anything popupControl = new PopupControl(); // We need the following events from popupControl. popupControl.MouseMove +=new MouseEventHandler(OnMouseMove); popupControl.MouseDown +=new MouseEventHandler(OnMouseDown); popupControl.Paint +=new PaintEventHandler(popupControl_Paint); OnPopup(EventArgs.Empty); // Figure out where we need to put the popup and its size. Point pt = control.PointToScreen(new Point(0,0)); Rectangle rcWork = Screen.PrimaryScreen.WorkingArea; using (Graphics g = popupControl.CreateGraphics()) { Size size = MeasureItemBounds(g); size.Height -= 1; //align it to control if (pt.X < rcWork.Left) { pt.X += size.Width; } if (pt.X > rcWork.Right - size.Width) { pt.X -= size.Width; } if (pt.Y < rcWork.Top) { pt.Y += size.Height; } if (pt.Y > rcWork.Bottom - size.Height) { pt.Y -= size.Height; } //add offset pos pt.X += pos.X; pt.Y += pos.Y; //ensure that it is completely visible on screen if (pt.X < rcWork.Left) { pt.X = rcWork.Left; } if (pt.X > rcWork.Right - size.Width) { pt.X = rcWork.Right - size.Width; } if (pt.Y < rcWork.Top) { pt.Y = rcWork.Top; } if (pt.Y > rcWork.Bottom + size.Height) { pt.Y = rcWork.Bottom - size.Height; } popupControl.Bounds = new Rectangle( pt, size); } popupControl.Show(); }
// Show this context menu at the specified control-relative co-ordinates. public void Show(Control control, Point pos) { associatedControl = control; currentMouseItem = -1; // Not over anything popupControl = new PopupControl(); // We need the following events from popupControl. popupControl.MouseMove += new MouseEventHandler(OnMouseMove); popupControl.MouseDown += new MouseEventHandler(OnMouseDown); popupControl.Paint += new PaintEventHandler(popupControl_Paint); OnPopup(EventArgs.Empty); // Figure out where we need to put the popup and its size. Point pt = control.PointToScreen(new Point(0, 0)); Rectangle rcWork = Screen.PrimaryScreen.WorkingArea; using (Graphics g = popupControl.CreateGraphics()) { Size size = MeasureItemBounds(g); size.Height -= 1; //align it to control if (pt.X < rcWork.Left) { pt.X += size.Width; } if (pt.X > rcWork.Right - size.Width) { pt.X -= size.Width; } if (pt.Y < rcWork.Top) { pt.Y += size.Height; } if (pt.Y > rcWork.Bottom - size.Height) { pt.Y -= size.Height; } //add offset pos pt.X += pos.X; pt.Y += pos.Y; //ensure that it is completely visible on screen if (pt.X < rcWork.Left) { pt.X = rcWork.Left; } if (pt.X > rcWork.Right - size.Width) { pt.X = rcWork.Right - size.Width; } if (pt.Y < rcWork.Top) { pt.Y = rcWork.Top; } if (pt.Y > rcWork.Bottom + size.Height) { pt.Y = rcWork.Bottom - size.Height; } popupControl.Bounds = new Rectangle(pt, size); } popupControl.Show(); }