#region 控制无边框拉伸 private ResizeDirect _direct; private bool _resizeing; private Point _resizeStart; private Point _resizePos; private Size _resizeSize; const int WM_LBUTTONDOWN = 513; const int WM_MOUSEMOVE = 512; public bool PreFilterMessage(ref Message m) { if (IsDisposed) { Application.RemoveMessageFilter(this); return false; } var ctrl = FromChildHandle(m.HWnd); if (ctrl == null || ctrl.FindForm() != this) { return false; } //鼠标单击 if (m.Msg == (int)WM_LBUTTONDOWN && _direct != ResizeDirect.None) { _resizeing = true; _resizeStart = MousePosition; _resizePos = Location; _resizeSize = Size; ResizeTask(); return true; } //鼠标移动 if (m.Msg == (int)WM_MOUSEMOVE) { if (_resizeing) { switch (_direct) { case ResizeDirect.Bottom: case ResizeDirect.Top: Cursor = Cursors.SizeNS; break; case ResizeDirect.Left: case ResizeDirect.Right: Cursor = Cursors.SizeWE; break; case ResizeDirect.LeftTop: case ResizeDirect.RightBottom: Cursor = Cursors.SizeNWSE; break; case ResizeDirect.RightTop: case ResizeDirect.LeftBottom: Cursor = Cursors.SizeNESW; break; } return true; } if (NoneBorderResize && FormBorderStyle == FormBorderStyle.None && WindowState == FormWindowState.Normal) { _direct = ShowResizeCursor(PointToClient(MousePosition)); return _direct != ResizeDirect.None; } } return false; } private enum ResizeDirect { None, Left, Right, Top, Bottom, LeftTop, LeftBottom, RightTop, RightBottom } public void SafeInvoke(Action<object> callback, object state = null) { miniblinkBrowser1.SafeInvoke(callback, state); } private void ResizeTask() { var last = _resizeStart; var waiter = new SpinWait(); Task.Factory.StartNew(() => { while (_resizeing) { if (MouseButtons != MouseButtons.Left) { _resizeing = false; SafeInvoke(s => { Cursor = DefaultCursor; }); break; } var curr = MousePosition; if (curr.X != last.X || curr.Y != last.Y) { var xx = curr.X - _resizeStart.X; var xy = curr.Y - _resizeStart.Y; int nx = Left, ny = Top, nw = Width, nh = Height; switch (_direct) { case ResizeDirect.Left: nw = _resizeSize.Width - xx; nx = _resizePos.X + xx; break; case ResizeDirect.Right: nw = _resizeSize.Width + xx; break; case ResizeDirect.Top: nh = _resizeSize.Height - xy; ny = _resizePos.Y + xy; break; case ResizeDirect.Bottom: nh = _resizeSize.Height + xy; break; case ResizeDirect.LeftTop: nw = _resizeSize.Width - xx; nx = _resizePos.X + xx; nh = _resizeSize.Height - xy; ny = _resizePos.Y + xy; break; case ResizeDirect.LeftBottom: nw = _resizeSize.Width - xx; nx = _resizePos.X + xx; nh = _resizeSize.Height + xy; break; case ResizeDirect.RightTop: nw = _resizeSize.Width + xx; nh = _resizeSize.Height - xy; ny = _resizePos.Y + xy; break; case ResizeDirect.RightBottom: nw = _resizeSize.Width + xx; nh = _resizeSize.Height + xy; break; } SafeInvoke(s => { Size = new Size(nw, nh); Location = new Point(nx, ny); }); } last = curr; waiter.SpinOnce(); } }); } private ResizeDirect ShowResizeCursor(Point point) { var rect = ClientRectangle; var direct = ResizeDirect.None; if (point.X <= ResizeWidth.Left) { if (point.Y <= ResizeWidth.Top) { direct = ResizeDirect.LeftTop; } else if (point.Y + ResizeWidth.Right >= rect.Height) { direct = ResizeDirect.LeftBottom; } else { direct = ResizeDirect.Left; } } else if (point.Y <= ResizeWidth.Top) { if (point.X <= ResizeWidth.Left) { direct = ResizeDirect.LeftTop; } else if (point.X + ResizeWidth.Right >= rect.Width) { direct = ResizeDirect.RightTop; } else { direct = ResizeDirect.Top; } } else if (point.X + ResizeWidth.Right >= rect.Width) { if (point.Y <= ResizeWidth.Top) { direct = ResizeDirect.RightTop; } else if (point.Y + ResizeWidth.Bottom >= rect.Height) { direct = ResizeDirect.RightBottom; } else { direct = ResizeDirect.Right; } } else if (point.Y + ResizeWidth.Bottom >= rect.Height) { if (point.X <= ResizeWidth.Left) { direct = ResizeDirect.LeftBottom; } else if (point.X + ResizeWidth.Right >= rect.Width) { direct = ResizeDirect.RightBottom; } else { direct = ResizeDirect.Bottom; } } else if (IsMove == false) { if (Cursor != DefaultCursor) { Cursor = DefaultCursor; } } switch (direct) { case ResizeDirect.Bottom: case ResizeDirect.Top: Cursor = Cursors.SizeNS; break; case ResizeDirect.Left: case ResizeDirect.Right: Cursor = Cursors.SizeWE; break; case ResizeDirect.LeftTop: case ResizeDirect.RightBottom: Cursor = Cursors.SizeNWSE; break; case ResizeDirect.RightTop: case ResizeDirect.LeftBottom: Cursor = Cursors.SizeNESW; break; } return direct; } #endregion
文章评论