/// <summary> /// 引发 System.Windows.Forms.Form.MouseDown。 /// </summary> /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param> protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); var point = e.Location; if (e.Button == MouseButtons.Right) { if (this.TitleBarRect.Contains(point) && !SysBtnRect.Contains(point)) { var menu = NativeMethods.GetSystemMenu(this.Handle, false); RECT rect = new RECT(); var result = NativeMethods.TrackPopupMenu(menu, (int)WindowsMessage.WM_KEYDOWN, MousePosition.X, MousePosition.Y, 0, this.Handle, ref rect); MenuClick((MenuType)result); } return; } if (CloseContains(point)) { CloseState = TMouseState.Down; } else if (MiniRect.Contains(point)) { MinState = TMouseState.Down; } else if (MaxRect.Contains(point)) { MaxState = TMouseState.Down; } if ((_sysButton == TSysButton.Normal || _sysButton == TSysButton.Close_Max) && e.Clicks == 2) { WindowMax(); } }
/// <summary> /// 引发 System.Windows.Forms.Form.MouseUp。 /// </summary> /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param> protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); if (e.Button != MouseButtons.Left) { return; } var point = e.Location; if (CloseContains(point) && CloseState == TMouseState.Down) { CloseState = TMouseState.Move; Close(); } else { CloseState = TMouseState.Normal; } if (MiniRect.Contains(point) && MinState == TMouseState.Down) { MinState = TMouseState.Move; WindowState = FormWindowState.Minimized; } else { MinState = TMouseState.Normal; } if (MaxRect.Contains(point) && MaxState == TMouseState.Down) { MaxState = TMouseState.Move; WindowMax(); } else { MaxState = TMouseState.Normal; } }
/// <summary> /// 引发 System.Windows.Forms.Form.MouseMove。 /// </summary> /// <param name="e">包含事件数据的 System.Windows.Forms.MouseEventArgs。</param> protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { return; } var flag = true; string tipText = null; var point = e.Location; if (CloseContains(point)) { flag = false; if (CloseState != TMouseState.Move) { CloseState = TMouseState.Move; tipText = "关闭"; } } else { CloseState = TMouseState.Normal; } if (MiniRect.Contains(point)) { flag = false; if (MinState != TMouseState.Move) { MinState = TMouseState.Move; tipText = "最小化"; } } else { MinState = TMouseState.Normal; } if (MaxRect.Contains(point)) { flag = false; if (MaxState != TMouseState.Move) { MaxState = TMouseState.Move; tipText = WindowState == FormWindowState.Maximized ? "还原" : "最大化"; } } else { MaxState = TMouseState.Normal; } if (TitleBarMenuRect.Contains(e.Location)) { flag = false; if (TitleBarMenuState != TMouseState.Move) { TitleBarMenuState = TMouseState.Move; tipText = "菜单栏"; } } else { TitleBarMenuState = TMouseState.Normal; } if (tipText != null) { ShowTooTip(tipText); } if (flag) { HideToolTip(); } }
/// <summary> /// 拖动窗口大小。 /// </summary> /// <param name="m"></param> protected virtual void DragFormSize(ref Message m) { bool isR = false; int param = m.LParam.ToInt32(); Point point = new Point(param & 0xFFFF, param >> 16); point = PointToClient(point); if (this.WindowState != FormWindowState.Maximized) { if (IsResize) { if (point.X <= 3) { if (point.Y <= 3) { m.Result = (IntPtr)WM_NCHITTEST.HTTOPLEFT; isR = true; } else if (point.Y > Height - 3) { m.Result = (IntPtr)WM_NCHITTEST.HTBOTTOMLEFT; isR = true; } else { m.Result = (IntPtr)WM_NCHITTEST.HTLEFT; isR = true; } } else if (point.X >= Width - 3) { if (point.Y <= 3) { m.Result = (IntPtr)WM_NCHITTEST.HTTOPRIGHT; isR = true; } else if (point.Y >= Height - 3) { m.Result = (IntPtr)WM_NCHITTEST.HTBOTTOMRIGHT; isR = true; } else { m.Result = (IntPtr)WM_NCHITTEST.HTRIGHT; isR = true; } } else if (point.Y <= 3) { m.Result = (IntPtr)WM_NCHITTEST.HTTOP; isR = true; } else if (point.Y >= Height - 3) { m.Result = (IntPtr)WM_NCHITTEST.HTBOTTOM; isR = true; } } } if (!isR) { if (TitleRect.Contains(point)) { if (!this.MaximizeBox || CloseRect.Contains(point) || MaxRect.Contains(point) || MiniRect.Contains(point) || SkinRect.Contains(point)) { return; } m.Result = (IntPtr)2; } } }