internal static void DoWindowTitleThumbMoveOnDragDelta(IMetroThumb thumb, SimpleMetroWindow window, DragDeltaEventArgs dragDeltaEventArgs)
        {
            if (thumb == null)
            {
                throw new ArgumentNullException(nameof(thumb));
            }
            if (window == null)
            {
                throw new ArgumentNullException(nameof(window));
            }

            // drag only if IsWindowDraggable is set to true
            if (!window.IsWindowDraggable ||
                (!(Math.Abs(dragDeltaEventArgs.HorizontalChange) > 2) && !(Math.Abs(dragDeltaEventArgs.VerticalChange) > 2)))
            {
                return;
            }

            // tage from DragMove internal code
            window.VerifyAccess();

            var cursorPos = NativeMethods.GetCursorPos();

            // if the window is maximized dragging is only allowed on title bar (also if not visible)
            var windowIsMaximized = window.WindowState == WindowState.Maximized;

            ////var isMouseOnTitlebar = Mouse.GetPosition(thumb).Y <= window.TitlebarHeight && window.TitlebarHeight > 0;
            ////if (!isMouseOnTitlebar && windowIsMaximized)
            ////{
            ////    return;
            ////}

            // for the touch usage
            UnsafeNativeMethods.ReleaseCapture();

            if (windowIsMaximized)
            {
                var          cursorXPos           = cursorPos.x;
                EventHandler windowOnStateChanged = null;
                windowOnStateChanged = (sender, args) =>
                {
                    //window.Top = 2;
                    //window.Left = Math.Max(cursorXPos - window.RestoreBounds.Width / 2, 0);

                    window.StateChanged -= windowOnStateChanged;
                    if (window.WindowState == WindowState.Normal)
                    {
                        Mouse.Capture(thumb, CaptureMode.Element);
                    }
                };
                window.StateChanged += windowOnStateChanged;
            }

            var criticalHandle = window.CriticalHandle;

            // DragMove works too
            // window.DragMove();
            // instead this 2 lines
            NativeMethods.SendMessage(criticalHandle, WM.SYSCOMMAND, (IntPtr)SC.MOUSEMOVE, IntPtr.Zero);
            NativeMethods.SendMessage(criticalHandle, WM.LBUTTONUP, IntPtr.Zero, IntPtr.Zero);
        }
Exemple #2
0
        internal static void DoWindowTitleThumbMoveOnDragDelta(IMetroThumb thumb, MetroWindow window, VectorEventArgs dragDeltaEventArgs)
        {
            if (thumb == null)
            {
                throw new ArgumentNullException(nameof(thumb));
            }
            if (window == null)
            {
                throw new ArgumentNullException(nameof(window));
            }

            // drag only if IsWindowDraggable is set to true
            if (!window.IsWindowDraggable ||
                (!(Math.Abs(dragDeltaEventArgs.Vector.Y) > 2) && !(Math.Abs(dragDeltaEventArgs.Vector.X) > 2)))
            {
                return;
            }

            // tage from DragMove internal code
            window.VerifyAccess();

            //var cursorPos = WinApiHelper.GetPhysicalCursorPos();

            // if the window is maximized dragging is only allowed on title bar (also if not visible)
            var windowIsMaximized = window.WindowState == WindowState.Maximized;

            if (window._titleBar != null && window._titleBar.IsPointerOver /*&& _mouseDown*/)
            {
                window.WindowState = WindowState.Normal;

                ///var pointerPress=new

                try
                {
                    window.BeginMoveDrag(null);
                }
                catch
                {
                    //little hack so I do not need create Pointerpressed event
                    //which has a huge constructor
                }
                window._mouseDown = false;
            }

            //var isMouseOnTitlebar = Mouse.GetPosition(thumb).Y <= window.TitleBarHeight && window.TitleBarHeight > 0;
            //if (!isMouseOnTitlebar && windowIsMaximized)
            //{
            //    return;
            //}

#pragma warning disable 618
            // for the touch usage
            //UnsafeNativeMethods.ReleaseCapture();
#pragma warning restore 618

            if (windowIsMaximized)
            {
                //var cursorXPos = cursorPos.x;
                EventHandler windowOnStateChanged = null;
                windowOnStateChanged = (sender, args) =>
                {
                    //window.Top = 2;
                    //window.Left = Math.Max(cursorXPos - window.RestoreBounds.Width / 2, 0);

                    //window.StateChanged -= windowOnStateChanged;
                    //if (window.WindowState == WindowState.Normal)
                    //{
                    //    Mouse.Capture(thumb, CaptureMode.Element);
                    //}
                };
                //window.StateChanged -= windowOnStateChanged;
                //window.StateChanged += windowOnStateChanged;
            }

            //            var criticalHandle = window.CriticalHandle;
            //#pragma warning disable 618
            //            // these lines are from DragMove
            //            // NativeMethods.SendMessage(criticalHandle, WM.SYSCOMMAND, (IntPtr)SC.MOUSEMOVE, IntPtr.Zero);
            //            // NativeMethods.SendMessage(criticalHandle, WM.LBUTTONUP, IntPtr.Zero, IntPtr.Zero);

            //            var wpfPoint = window.PointToScreen(Mouse.GetPosition(window));
            //            var x = (int)wpfPoint.X;
            //            var y = (int)wpfPoint.Y;
            //            NativeMethods.SendMessage(criticalHandle, WM.NCLBUTTONDOWN, (IntPtr)HT.CAPTION, new IntPtr(x | (y << 16)));
            //#pragma warning restore 618
        }
Exemple #3
0
        /// <inheritdoc />
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            // really necessary?
            if (this.Template == null)
            {
                return;
            }

            var isActiveBindingAction = new Action(() => {
                var window = Window.GetWindow(this);
                if (window != null)
                {
                    this.SetBinding(ChildWindow.IsWindowHostActiveProperty, new Binding(nameof(Window.IsActive))
                    {
                        Source = window, Mode = BindingMode.OneWay
                    });
                }
            });

            if (!this.IsLoaded)
            {
                this.BeginInvoke(isActiveBindingAction, DispatcherPriority.Loaded);
            }
            else
            {
                isActiveBindingAction();
            }

            this.hideStoryboard = this.Template.FindName(HideStoryboard, this) as Storyboard;

            if (this.partOverlay != null)
            {
                this.partOverlay.MouseLeftButtonDown -= this.PartOverlayOnClose;
            }

            this.partOverlay = this.Template.FindName(PART_Overlay, this) as Grid;
            if (this.partOverlay != null)
            {
                this.partOverlay.MouseLeftButtonDown += this.PartOverlayOnClose;
            }

            this.partWindow = this.Template.FindName(PART_Window, this) as Grid;
            if (this.partWindow != null)
            {
                this.partWindow.RenderTransform = this.moveTransform;
            }

            this.icon = this.Template.FindName(PART_Icon, this) as ContentControl;

            if (this.headerThumb != null)
            {
                this.headerThumb.DragDelta -= this.HeaderThumbDragDelta;
            }

            this.headerThumb = this.Template.FindName(PART_HeaderThumb, this) as IMetroThumb;
            if (this.headerThumb != null && this.partWindow != null)
            {
                this.headerThumb.DragDelta += this.HeaderThumbDragDelta;
            }

            if (this.closeButton != null)
            {
                this.closeButton.Click -= this.OnCloseButtonClick;
            }

            this.closeButton = this.Template.FindName(PART_CloseButton, this) as Button;
            if (this.closeButton != null)
            {
                this.closeButton.Click += this.OnCloseButtonClick;
            }
        }