private void InitSystemButtons() { _closeButton = new CaptionButton(); _closeButton.Key = "CloseButton"; _closeButton.Visible = true; _closeButton.HitTestCode = (int)NativeMethods.NCHITTEST.HTCLOSE; _closeButton.SystemCommand = (int)NativeMethods.SystemCommands.SC_CLOSE; _captionButtons.Add(_closeButton); _restoreButton = new CaptionButton(); _restoreButton.Key = "RestoreButton"; _restoreButton.Enabled = this.MaximizeBox; _restoreButton.HitTestCode = (int)NativeMethods.NCHITTEST.HTMAXBUTTON; _restoreButton.SystemCommand = (int)NativeMethods.SystemCommands.SC_RESTORE; _captionButtons.Add(_restoreButton); _maximizeButton = new CaptionButton(); _maximizeButton.Key = "MaximizeButton"; _maximizeButton.Enabled = this.MaximizeBox; _maximizeButton.HitTestCode = (int)NativeMethods.NCHITTEST.HTMAXBUTTON; _maximizeButton.SystemCommand = (int)NativeMethods.SystemCommands.SC_MAXIMIZE; _captionButtons.Add(_maximizeButton); _minimizeButton = new CaptionButton(); _minimizeButton.Key = "MinimizeButton"; _minimizeButton.Enabled = this.MinimizeBox; _minimizeButton.HitTestCode = (int)NativeMethods.NCHITTEST.HTMINBUTTON; _minimizeButton.SystemCommand = (int)NativeMethods.SystemCommands.SC_MINIMIZE; _captionButtons.Add(_minimizeButton); _helpButton = new CaptionButton(); _helpButton.Key = "HelpButton"; _helpButton.Visible = this.HelpButton; _helpButton.HitTestCode = (int)NativeMethods.NCHITTEST.HTHELP; _helpButton.SystemCommand = (int)NativeMethods.SystemCommands.SC_CONTEXTHELP; _captionButtons.Add(_helpButton); OnUpdateWindowState(); }
private void DrawButton(CaptionButton button) { if (IsHandleCreated) { // MSDN states that only WINDOW and INTERSECTRGN are needed, // but other sources confirm that CACHE is required on Win9x // and you need CLIPSIBLINGS to prevent painting on overlapping windows. IntPtr hDC = NativeMethods.GetDCEx(this.Handle, (IntPtr)1, (int)(NativeMethods.DCX.DCX_WINDOW | NativeMethods.DCX.DCX_INTERSECTRGN | NativeMethods.DCX.DCX_CACHE | NativeMethods.DCX.DCX_CLIPSIBLINGS)); if (hDC != IntPtr.Zero) { using (Graphics g = Graphics.FromHdc(hDC)) { button.DrawButton(g, true); } } NativeMethods.ReleaseDC(this.Handle, hDC); } }
public void Add(CaptionButton button) { this.List.Add(button); }
/// <summary> /// This method handles depressing the titlebar button. It captures the mouse and creates a message loop /// filtring only the mouse buttons until a WM_MOUSEMOVE or WM_LBUTTONUP message is received. /// </summary> /// <param name="currentButton">The button that was pressed</param> /// <returns>true if WM_LBUTTONUP occured over this button; false when mouse was moved away from this button.</returns> private bool DepressButton(CaptionButton currentButton) { try { // callect all mouse events (should do the same as SetCapture()) this.Capture = true; // draw button in pressed mode currentButton.State = CaptionButtonState.Pressed; DrawButton(currentButton); // loop until button is released bool result = false; bool done = false; while (!done) { // NOTE: This struct needs to be here. We had strange error (starting from Beta 2). // when this was defined at begining of this method. also check if types are correct (no overlap). Message m = new Message(); if (NativeMethods.PeekMessage(ref m, IntPtr.Zero, (int)NativeMethods.WindowMessages.WM_MOUSEFIRST, (int)NativeMethods.WindowMessages.WM_MOUSELAST, (int)NativeMethods.PeekMessageOptions.PM_REMOVE)) { Log(MethodInfo.GetCurrentMethod(), "Message = {0}, Button = {1}", (NativeMethods.WindowMessages)m.Msg, currentButton); switch (m.Msg) { case (int)NativeMethods.WindowMessages.WM_LBUTTONUP: { if (currentButton.State == CaptionButtonState.Pressed) { currentButton.State = CaptionButtonState.Normal; DrawButton(currentButton); } Point pt = PointToWindow(PointToScreen(new Point(m.LParam.ToInt32()))); Log(MethodInfo.GetCurrentMethod(), "### Point = ({0}, {1})", pt.X, pt.Y); result = currentButton.Bounds.Contains(pt); done = true; } break; case (int)NativeMethods.WindowMessages.WM_MOUSEMOVE: { Point clientPoint = PointToWindow(PointToScreen(new Point(m.LParam.ToInt32()))); if (currentButton.Bounds.Contains(clientPoint)) { if (currentButton.State == CaptionButtonState.Normal) { currentButton.State = CaptionButtonState.Pressed; DrawButton(currentButton); } } else { if (currentButton.State == CaptionButtonState.Pressed) { currentButton.State = CaptionButtonState.Normal; DrawButton(currentButton); } } // [1531]: These variables need to be reset here although thay aren't changed // before reaching this point result = false; done = false; } break; } } } return(result); } finally { this.Capture = false; } }