protected int CalcGroupHeight(ActionMenuGroup oGroup) { if (oGroup.Expanded == false) return 23; int nHeight = 28; foreach (ActionMenuItem oItem in oGroup.Items) { if (oItem.Text == "-") nHeight += 3; else nHeight += 20; } return nHeight; }
public ActionMenuItem AddMenuItem(ActionMenuGroup oGroup, string sItem) { ActionMenuItem oItem = new ActionMenuItem(); oItem.Text = sItem; oItem.MenuGroup = oGroup; oGroup.Items.Add(oItem); return oItem; }
public void Show(int nX, int nY) { // Decide if we need layered windows m_bLayered = (m_bSupportsLayered); CreateParams cp = new CreateParams(); // Any old title will do as it will not be shown cp.Caption = "PureComponents.ActionMenu"; RecalcLayout(); // get the max screen size. if the nX + Width or nY + Height is bigger than the area, them move it elsewhere and recalculate the // position Screen oScreen = Screen.FromHandle(this.Handle); if (nX + m_oCurrentSize.Width > oScreen.Bounds.Width) nX = oScreen.Bounds.Width - m_oCurrentSize.Width; if (nY + m_oCurrentSize.Height > oScreen.Bounds.Height) nY = oScreen.Bounds.Height - m_oCurrentSize.Height; // set the position m_oScreenPos = new Point(nX, nY); Size winSize = m_oCurrentSize; Point screenPos = m_oScreenPos; // Define the screen position/size cp.X = nX; cp.Y = nY; cp.Height = winSize.Height; cp.Width = winSize.Width; // As a top-level window it has no parent cp.Parent = IntPtr.Zero; // Appear as a top-level window cp.Style = unchecked((int)(uint)WindowStyles.WS_POPUP); // Set styles so that it does not have a caption bar and is above all other // windows in the ZOrder, i.e. TOPMOST cp.ExStyle = (int)WindowExStyles.WS_EX_TOPMOST + (int)WindowExStyles.WS_EX_TOOLWINDOW; // OS specific style if (m_bLayered) { // If not on NT then we are going to use alpha blending on the shadow border // and so we need to specify the layered window style so the OS can handle it cp.ExStyle += (int)WindowExStyles.WS_EX_LAYERED; } // Create the actual window if (this.Handle == IntPtr.Zero) this.CreateHandle(cp); // Remember the correct screen drawing details m_oCurrentSize = winSize; m_oCurrentPoint = screenPos; // Update the image for display if (m_bLayered) UpdateLayeredWindow(); // Show the window without activating it (i.e. do not take focus) User32.ShowWindow(this.Handle, (short)ShowWindowStyles.SW_SHOWNOACTIVATE); MSG msg = new MSG(); bool _exitLoop = false; // POINT lastMousePoint; while(!_exitLoop) { // Suspend thread until a windows message has arrived if (User32.WaitMessage()) { // Take a peek at the message details without removing from queue while(!_exitLoop && User32.PeekMessage(ref msg, 0, 0, 0, (int)PeekMessageFlags.PM_NOREMOVE)) { // test if the mouse up is in the area of the menu int localWidth = m_oCurrentSize.Width - m_aPosition[0, (int)PaintItem.SW]; int localHeight = m_oCurrentSize.Height - m_aPosition[0, (int)PaintItem.SH]; bool eatMessage = false; #region mouse move if (msg.message == (int)Msgs.WM_MOUSEMOVE) { POINT screenPosMsg = MousePositionToScreen(msg); // Is the POINT inside the Popup window rectangle if ((screenPosMsg.x >= m_oCurrentPoint.X) && (screenPosMsg.x <= (m_oCurrentPoint.X + localWidth)) && (screenPosMsg.y >= m_oCurrentPoint.Y) && (screenPosMsg.y <= (m_oCurrentPoint.Y + localHeight))) { eatMessage = true; OnWM_MOUSEMOVE(screenPosMsg.x, screenPosMsg.y); if (m_oHighlightedGroup != null) User32.SetCursor(User32.LoadCursor(IntPtr.Zero, (uint)Cursors.IDC_HAND)); else User32.SetCursor(User32.LoadCursor(IntPtr.Zero, (uint)Cursors.IDC_ARROW)); } else { if (m_bDrag == true) { OnWM_MOUSEMOVE(screenPosMsg.x, screenPosMsg.y); } else { // clear selection and redraw the window if (m_oSelectedItem != null) { m_oSelectedItem.Selected = false; m_oSelectedItem = null; RecalcLayout(); Invalidate(); } if (m_oHighlightedGroup != null) { m_oHighlightedGroup = null; RecalcLayout(); Invalidate(); } if (m_bExpandCollapseStrafeSelect == true) { m_bExpandCollapseStrafeSelect = false; RecalcLayout(); Invalidate(); } if (User32.GetMessage(ref msg, 0, 0, 0)) { User32.TranslateMessage(ref msg); User32.DispatchMessage(ref msg); } } } } #endregion #region left button up if (msg.message == (int)Msgs.WM_LBUTTONUP) { POINT screenPosMsg = MousePositionToScreen(msg); m_bDrag = false; // Is the POINT inside the Popup window rectangle if ((screenPosMsg.x >= m_oCurrentPoint.X) && (screenPosMsg.x <= (m_oCurrentPoint.X + localWidth)) && (screenPosMsg.y >= m_oCurrentPoint.Y) && (screenPosMsg.y <= (m_oCurrentPoint.Y + localHeight))) { eatMessage = true; POINT localPoint = MousePositionToClient(screenPosMsg); if (OnWM_LBUTTONUP(localPoint.x, localPoint.y) == true) { Hide(); return; } } else { Hide(); _exitLoop = true; if (User32.GetMessage(ref msg, 0, 0, 0)) { User32.TranslateMessage(ref msg); User32.DispatchMessage(ref msg); } } } #endregion #region mouse click events if ((msg.message == (int)Msgs.WM_MBUTTONDOWN) || (msg.message == (int)Msgs.WM_RBUTTONDOWN) || (msg.message == (int)Msgs.WM_XBUTTONDOWN) || (msg.message == (int)Msgs.WM_NCLBUTTONDOWN) || (msg.message == (int)Msgs.WM_NCMBUTTONDOWN) || (msg.message == (int)Msgs.WM_NCRBUTTONDOWN) || (msg.message == (int)Msgs.WM_NCXBUTTONDOWN)) { POINT screenPosMsg = MousePositionToScreen(msg); // Is the POINT inside the Popup window rectangle if ((screenPosMsg.x >= m_oCurrentPoint.X) && (screenPosMsg.x <= (m_oCurrentPoint.X + localWidth)) && (screenPosMsg.y >= m_oCurrentPoint.Y) && (screenPosMsg.y <= (m_oCurrentPoint.Y + localHeight))) { eatMessage = true; // if it is the left mouse then check the drag if (msg.message != (int)Msgs.WM_RBUTTONDOWN && msg.message != (int)Msgs.WM_MBUTTONDOWN) { // check if it is in the header, if yes, start the dragging mode if ((screenPosMsg.x >= m_oCurrentPoint.X) && (screenPosMsg.x <= (m_oCurrentPoint.X + localWidth)) && (screenPosMsg.y >= m_oCurrentPoint.Y) && (screenPosMsg.y <= (m_oCurrentPoint.Y + 10))) { m_bDrag = true; } else m_bDrag = false; } } else { m_bDrag = false; Hide(); _exitLoop = true; if (User32.GetMessage(ref msg, 0, 0, 0)) { User32.TranslateMessage(ref msg); User32.DispatchMessage(ref msg); } } } #endregion #region key press if (msg.message == (int)Msgs.WM_KEYDOWN) { switch((int)msg.wParam) { case (int)VirtualKeys.VK_ESCAPE: Hide(); return; } } #endregion if (eatMessage == true) { MSG eat = new MSG(); User32.GetMessage(ref eat, 0, 0, 0); eatMessage = false; } else { if (User32.GetMessage(ref msg, 0, 0, 0)) { User32.TranslateMessage(ref msg); User32.DispatchMessage(ref msg); } } } } } }
public void RemoveMenuGroup(ActionMenuGroup oGroup) { m_aGroups.Remove(oGroup); RecalcLayout(); Invalidate(); }
public ActionMenuGroup AddMenuGroup(string sGroup) { ActionMenuGroup oGroup = new ActionMenuGroup(); oGroup.Title = sGroup; m_aGroups.Add(oGroup); RecalcLayout(); Invalidate(); return oGroup; }
protected void OnWM_MOUSEMOVE(int xPos, int yPos) { // Convert from screen to client coordinates xPos -= m_oCurrentPoint.X; yPos -= m_oCurrentPoint.Y; Point pos = new Point(xPos, yPos); // Yes, we know the mouse is over window m_bMouseOver = true; if (m_bDrag == true) { m_oCurrentPoint = new Point(m_oCurrentPoint.X - (m_oLastMousePos.X - xPos), m_oCurrentPoint.Y - (m_oLastMousePos.Y - yPos)); // get the max screen size. if the nX + Width or nY + Height is bigger than the area, // them move it elsewhere and recalculate the position Screen oScreen = Screen.FromHandle(this.Handle); if (m_oCurrentPoint.X + m_oCurrentSize.Width > oScreen.Bounds.Width) m_oCurrentPoint.X = oScreen.Bounds.Width - m_oCurrentSize.Width; if (m_oCurrentPoint.Y + m_oCurrentSize.Height > oScreen.Bounds.Height) m_oCurrentPoint.Y = oScreen.Bounds.Height - m_oCurrentSize.Height; RecalcLayout(); Invalidate(); return; } int localWidth = m_oCurrentSize.Width - m_aPosition[0, (int)PaintItem.SW]; // Has mouse position really changed since last time? if (m_oLastMousePos != pos) { foreach (Rectangle oRect in m_mapRect2GroupBox.Keys) { if (oRect.Contains(pos) == true) { ActionMenuGroup oGroup = m_mapRect2GroupBox[oRect] as ActionMenuGroup; if (m_oHighlightedGroup != oGroup) { m_oHighlightedGroup = oGroup; RecalcLayout(); Invalidate(); if (Cursor.Current != System.Windows.Forms.Cursors.Hand) Cursor.Current = System.Windows.Forms.Cursors.Hand; } return; } } foreach (Rectangle oRect in m_mapRect2Item.Keys) { if (oRect.Contains(pos) == true) { ActionMenuItem oItem = m_mapRect2Item[oRect] as ActionMenuItem; if (oItem.Selected == true) return; if (m_oSelectedItem != null) m_oSelectedItem.Selected = false; oItem.Selected = true; m_oSelectedItem = oItem; m_oHighlightedGroup = null; RecalcLayout(); Invalidate(); return; } } Rectangle oCloseRect = new Rectangle(localWidth - 15, 3, 11, 10); if (oCloseRect.Contains(pos) == true) { Cursor.Current = System.Windows.Forms.Cursors.Hand; return; } if (m_oSelectedItem != null) { m_oSelectedItem.Selected = false; m_oSelectedItem = null; RecalcLayout(); Invalidate(); } if (m_oHighlightedGroup != null) { m_oHighlightedGroup = null; RecalcLayout(); Invalidate(); } if (m_bExpandCollapseStrafeSelect == true) { m_bExpandCollapseStrafeSelect = false; RecalcLayout(); Invalidate(); } } // Remember for next time around m_oLastMousePos = pos; }
private void PaintGroupExpanded(ActionMenuGroup oGroup, Graphics oGraphics, ref int nY) { Brush oBrush; Rectangle main = new Rectangle(0, 0, m_oCurrentSize.Width - 1 - m_aPosition[(int)0, (int)PaintItem.SW], m_oCurrentSize.Height - 1 - m_aPosition[(int)0, (int)PaintItem.SH]); if (m_oHighlightedGroup == oGroup) oBrush = new SolidBrush(Color.FromArgb(0x8A, 0x6E, 0x5D)); else oBrush = new SolidBrush(m_oBorderColor); oGraphics.FillRectangle(oBrush, 2, nY, main.Width - 3, 18); oGraphics.DrawString(oGroup.Title, Font, Brushes.White, 5, nY + 2); oBrush.Dispose(); Pen oPen = new Pen(m_oSelectColor, 1); oGraphics.DrawLine(oPen, main.Width - 12, nY + 5, main.Width - 11, nY + 5); oGraphics.DrawLine(oPen, main.Width - 13, nY + 6, main.Width - 12, nY + 6); oGraphics.DrawLine(oPen, main.Width - 11, nY + 6, main.Width - 10, nY + 6); oGraphics.DrawLine(oPen, main.Width - 14, nY + 7, main.Width - 13, nY + 7); oGraphics.DrawLine(oPen, main.Width - 10, nY + 7, main.Width - 9, nY + 7); oGraphics.DrawLine(oPen, main.Width - 15, nY + 8, main.Width - 14, nY + 8); oGraphics.DrawLine(oPen, main.Width - 9, nY + 8, main.Width - 8, nY + 8); oGraphics.DrawLine(oPen, main.Width - 12, nY + 8, main.Width - 11, nY + 8); oGraphics.DrawLine(oPen, main.Width - 13, nY + 9, main.Width - 12, nY + 9); oGraphics.DrawLine(oPen, main.Width - 11, nY + 9, main.Width - 10, nY + 9); oGraphics.DrawLine(oPen, main.Width - 14, nY + 10, main.Width - 13, nY + 10); oGraphics.DrawLine(oPen, main.Width - 10, nY + 10, main.Width - 9, nY + 10); oGraphics.DrawLine(oPen, main.Width - 15, nY + 11, main.Width - 14, nY + 11); oGraphics.DrawLine(oPen, main.Width - 9, nY + 11, main.Width - 8, nY + 11); oPen.Dispose(); Rectangle oRect = new Rectangle(3, nY, main.Width - 6, 18); m_mapGroupBox2Rect.Add(oGroup, oRect); m_mapRect2GroupBox.Add(oRect, oGroup); nY += 5; foreach (ActionMenuItem oItem in oGroup.Items) { if (oItem.Selected == true) { SolidBrush oInnerBrush = new SolidBrush(Color.FromArgb(0x80, 0x80, 0x80)); oGraphics.FillRectangle(oInnerBrush, 2, nY + 17, main.Width - 3, 20); oInnerBrush.Color = Color.FromArgb(0xBF, 0xBF, 0xFF); oGraphics.FillRectangle(oInnerBrush, 3, nY + 18, main.Width - 5, 18); oInnerBrush.Dispose(); } if (oItem.Text == "-") { oPen = new Pen(m_oBorderColor); oPen.DashStyle = DashStyle.Dot; oGraphics.DrawLine(oPen, 5, nY + 18, main.Width - 5, nY + 18); oPen.Dispose(); nY += 3; } else { oGraphics.DrawString(oItem.Text, Font, Brushes.Black, 5, nY + 20); oRect = new Rectangle(10, nY + 17, main.Width - 20, 20); m_mapItem2Rect.Add(oItem, oRect); m_mapRect2Item.Add(oRect, oItem); nY += 20; } } nY += 23; }
private void PaintGroupClosed(ActionMenuGroup oGroup, Graphics oGraphics, ref int nY) { Brush oBrush; Rectangle main = new Rectangle(0, 0, m_oCurrentSize.Width - 1 - m_aPosition[(int)0, (int)PaintItem.SW], m_oCurrentSize.Height - 1 - m_aPosition[(int)0, (int)PaintItem.SH]); if (m_oHighlightedGroup == oGroup) oBrush = new SolidBrush(Color.FromArgb(0x8A, 0x6E, 0x5D)); else oBrush = new SolidBrush(m_oBorderColor); oGraphics.FillRectangle(oBrush, 2, nY, main.Width - 3, 18); oGraphics.DrawString(oGroup.Title, Font, Brushes.White, 5, nY + 2); oBrush.Dispose(); Pen oPen = new Pen(m_oSelectColor, 1); oGraphics.DrawLine(oPen, main.Width - 15, nY + 5, main.Width - 14, nY + 5); oGraphics.DrawLine(oPen, main.Width - 14, nY + 6, main.Width - 13, nY + 6); oGraphics.DrawLine(oPen, main.Width - 13, nY + 7, main.Width - 12, nY + 7); oGraphics.DrawLine(oPen, main.Width - 12, nY + 8, main.Width - 11, nY + 8); oGraphics.DrawLine(oPen, main.Width - 11, nY + 7, main.Width - 10, nY + 7); oGraphics.DrawLine(oPen, main.Width - 10, nY + 6, main.Width - 9, nY + 6); oGraphics.DrawLine(oPen, main.Width - 9, nY + 5, main.Width - 8, nY + 5); oGraphics.DrawLine(oPen, main.Width - 15, nY + 8, main.Width - 14, nY + 8); oGraphics.DrawLine(oPen, main.Width - 14, nY + 9, main.Width - 13, nY + 9); oGraphics.DrawLine(oPen, main.Width - 13, nY + 10, main.Width - 12, nY + 10); oGraphics.DrawLine(oPen, main.Width - 12, nY + 11, main.Width - 11, nY + 11); oGraphics.DrawLine(oPen, main.Width - 11, nY + 10, main.Width - 10, nY + 10); oGraphics.DrawLine(oPen, main.Width - 10, nY + 9, main.Width - 9, nY + 9); oGraphics.DrawLine(oPen, main.Width - 9, nY + 8, main.Width - 8, nY + 8); oPen.Dispose(); Rectangle oRect = new Rectangle(1, nY, main.Width - 2, 18); m_mapGroupBox2Rect.Add(oGroup, oRect); m_mapRect2GroupBox.Add(oRect, oGroup); nY += 23; }