| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows;using System.Windows.Input;namespace Venus_Themes.CustomControls{    public class WindowTopArea : System.Windows.Controls.Border    {        static WindowTopArea()        {            DefaultStyleKeyProperty.OverrideMetadata(typeof(WindowTopArea), new FrameworkPropertyMetadata(typeof(WindowTopArea)));        }        private new bool CaptureMouse = false;        private enum ResizePosition        { None, Left, TopLeft, Top, TopRight, Right, BottomRight, Bottom, BottomLeft }        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)        {            Window win = Window.GetWindow(this);            if (e.ClickCount == 1 && e.LeftButton == MouseButtonState.Pressed)            {                win.DragMove();            }            else if (e.ClickCount == 2)            {                win.SizeToContent = SizeToContent.Manual;                win.WindowState = win.WindowState == WindowState.Maximized ? WindowState.Normal : WindowState.Maximized;            }            base.OnMouseLeftButtonDown(e);            e.Handled = true;        }        protected override void OnInitialized(EventArgs e)        {            //SetWindowNoBorder();            //SetWindowResizer();            base.OnInitialized(e);        }        #region 窗体大小变化        public void SetWindowResizer()        {            Window win = Window.GetWindow(this);            ResizePosition ResPosition = ResizePosition.None;            int Resizer = 5;            win.MouseMove += new MouseEventHandler(                    delegate (object target, MouseEventArgs args)                    {                        try                        {                            //do resize                            if (win.WindowState == WindowState.Normal)                            {                                Point MS = args.GetPosition(win);                                if (args.LeftButton == MouseButtonState.Pressed)                                {                                    Win32.POINT pos = new Win32.POINT();                                    Win32.GetCursorPos(out pos);                                    #region 改变窗体大小                                    switch (ResPosition)                                    {                                        case ResizePosition.Left:                                            //左边                                                                      Mouse.SetCursor(Cursors.SizeWE);                                            Point transPointLeft = win.PointToScreen(new Point(0, 0));                                            win.Left += pos.X - transPointLeft.X;                                            win.Width += transPointLeft.X - pos.X;                                            break;                                        case ResizePosition.Right:                                            //右边                                            Mouse.SetCursor(Cursors.SizeWE);                                            Point transPointRight = win.PointToScreen(new Point(win.Width, 0));                                            win.Width += pos.X - transPointRight.X;                                            break;                                        case ResizePosition.Top:                                            //顶部                                            Mouse.SetCursor(Cursors.SizeNS);                                            Point transPointTop = win.PointToScreen(new Point(0, 0));                                            win.Top += pos.Y - transPointTop.Y;                                            win.Height += transPointTop.Y - pos.Y;                                            break;                                        case ResizePosition.Bottom:                                            //底部                                            Mouse.SetCursor(Cursors.SizeNS);                                            Point transPointBottom = win.PointToScreen(new Point(0, win.Height));                                            win.Height += (pos.Y - transPointBottom.Y);                                            break;                                        case ResizePosition.TopLeft:                                            //左上                                            Mouse.SetCursor(Cursors.SizeNWSE);                                            Point transPointTopLeft = win.PointToScreen(new Point(0, 0));                                            win.Left += pos.X - transPointTopLeft.X;                                            win.Top += pos.Y - transPointTopLeft.Y;                                            win.Width += transPointTopLeft.X - pos.X;                                            win.Height += transPointTopLeft.Y - pos.Y;                                            break;                                        case ResizePosition.BottomLeft:                                            //左下                                            Mouse.SetCursor(Cursors.SizeNESW);                                            Point transPointBottomLeft = win.PointToScreen(new Point(0, win.Height));                                            win.Left += pos.X - transPointBottomLeft.X;                                            win.Width += transPointBottomLeft.X - pos.X;                                            win.Height += pos.Y - transPointBottomLeft.Y;                                            break;                                        case ResizePosition.TopRight:                                            //右上                                            Mouse.SetCursor(Cursors.SizeNESW);                                            Point transPointTopRight = win.PointToScreen(new Point(win.Width, 0));                                            win.Top += pos.Y - transPointTopRight.Y;                                            win.Width = transPointTopRight.Y - pos.X;                                            win.Height = transPointTopRight.Y - pos.Y;                                            break;                                        case ResizePosition.BottomRight:                                            //右下                                            Mouse.SetCursor(Cursors.SizeNWSE);                                            Point transPointBottomRight = win.PointToScreen(new Point(win.Width, win.Height));                                            win.Width += pos.X - transPointBottomRight.X;                                            win.Height += pos.Y - transPointBottomRight.Y;                                            break;                                        case ResizePosition.None:                                        default:                                            Mouse.SetCursor(Cursors.Arrow);                                            break;                                    }                                    #endregion                                }                                else if (MS.X <= Resizer + 5 && MS.Y <= Resizer + 5)                                {                                    //左上 (不执行)                                    Mouse.SetCursor(Cursors.SizeNWSE);                                    ResPosition = ResizePosition.TopLeft;                                }                                else if (MS.X <= Resizer && MS.Y >= win.ActualHeight - Resizer)                                {                                    //左下                                    Mouse.SetCursor(Cursors.SizeNESW);                                    ResPosition = ResizePosition.BottomLeft;                                }                                else if (MS.X >= win.ActualWidth - Resizer - 5 && MS.Y <= Resizer + 5)                                {                                    //右上(不执行)                                    Mouse.SetCursor(Cursors.SizeNESW);                                    ResPosition = ResizePosition.TopRight;                                }                                else if (MS.X >= win.ActualWidth - Resizer && MS.Y >= win.ActualHeight - Resizer)                                {                                    //右下                                    Mouse.SetCursor(Cursors.SizeNWSE);                                    ResPosition = ResizePosition.BottomRight;                                }                                else if (MS.X <= Resizer)                                {                                    //左边                                    Mouse.SetCursor(Cursors.SizeWE);                                    ResPosition = ResizePosition.Left;                                }                                else if (MS.Y <= Resizer + 5)                                {                                    //顶部(不执行)                                    Mouse.SetCursor(Cursors.SizeNS);                                    ResPosition = ResizePosition.Top;                                }                                else if (MS.X >= win.ActualWidth - Resizer)                                {                                    //右边                                    Mouse.SetCursor(Cursors.SizeWE);                                    ResPosition = ResizePosition.Right;                                }                                else if (MS.Y >= win.ActualHeight - Resizer)                                {                                    //底部                                    Mouse.SetCursor(Cursors.SizeNS);                                    ResPosition = ResizePosition.Bottom;                                }                                else                                {                                    //无                                    Mouse.SetCursor(Cursors.Arrow);                                    ResPosition = ResizePosition.None;                                }                            }                        }                        catch                        {                            ResPosition = ResizePosition.None;                            win.ReleaseMouseCapture();                        }                        args.Handled = CaptureMouse;                    }                );            win.MouseLeftButtonDown += new MouseButtonEventHandler(                    delegate (object target, MouseButtonEventArgs args)                    {                        if (win.WindowState == WindowState.Normal)                        {                            //获取当前鼠标点击点相对于Dvap.Shell窗体的位置                            Point pos = args.GetPosition(win);                            if (ResPosition != ResizePosition.None)                            {                                CaptureMouse = win.CaptureMouse();                            }                            args.Handled = CaptureMouse;                        }                    }                );            win.MouseLeftButtonUp += new MouseButtonEventHandler(                    delegate (object target, MouseButtonEventArgs args)                    {                        if (win.WindowState == WindowState.Normal)                        {                            ResPosition = ResizePosition.None;                            if (CaptureMouse)                            {                                win.ReleaseMouseCapture();                                CaptureMouse = false;                            }                            args.Handled = CaptureMouse;                        }                    }                );        }        #endregion        private void SetWindowNoBorder()        {            Window win = Window.GetWindow(this);            // 获取窗体句柄              IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(win).Handle;            // 获得窗体的 样式              long oldstyle = Win32.GetWindowLong(hwnd, Win32.GWL_STYLE);            // 更改窗体的样式为无边框窗体              Win32.SetWindowLong(hwnd, Win32.GWL_STYLE, (int)(oldstyle & ~Win32.WS_CAPTION));        }    }}
 |