Vertiacal/Horizontal offset problem when mouve window

Nov 16, 2011 at 10:31 AM

Hi !

I am working with this useful extension, but i have a problem which i think you can resolve.

When i start (first time i show) a floatableWindow, the verticaloffset (and horizontal) i put, fit OK.

But, if I show the Fwindow, i mouve it (with mouse). When i put a new vertical/horizontal offset, and RE-Show the window, it is showed in the same place that i have mouved with the mouse. It doesnt fit with the new vertical/horizontal offset y have set.

To sum up:

my application, show 4 FWindows, in the places i have set, in a square shape, after this, you can move one (or more) of them, and i have put a button to REorganice the 4 FWindow in the original square.

but i cant due to the FWindows always go to the last place where it was mouved by the mouse.

 

Thanks!

Aug 15, 2012 at 5:04 PM

Im working on this myself. I discovered (using the source) that VerticalOffset/HorizontalOffset has no effect unless the window is closed/shown. To reposition it relative to the parent Canvas you need to do this:

public void MoveTo(Point point)
{
    this.VerticalOffset = point.Y;
    this.HorizontalOffset = point.X;
    this.SetValue(Canvas.TopProperty, _verticalOffset);
    this.SetValue(Canvas.LeftProperty, _horizontalOffset);    
}

This then has a problem where if you drag the floatable window then call MoveTo there is an offset (because of the renderTransform applied to the ContentRoot. To workaround this, I modified the source for Chrome_MouseMove and Chrome_MouseDown. Note this only works for modeless dialogs as we are using the Canvas property not setting the position of the childwindow. 

/// <summary>
        /// Executed when mouse left button is down on the chrome.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Mouse button event args.</param>
        private void Chrome_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            if (this._chrome != null)
            {
                e.Handled = true;

                if (this.CloseButton != null && !this.CloseButton.IsTabStop)
                {
                    this.CloseButton.IsTabStop = true;
                    try
                    {
                        this.Focus();
                    }
                    finally
                    {
                        this.CloseButton.IsTabStop = false;
                    }
                }
                else
                {
                    this.Focus();
                }
                this._chrome.CaptureMouse();
                this._isMouseCaptured = true;

                if (Application.Current.RootVisual == null)
                    return;

                this._clickPoint = e.GetPosition(Application.Current.RootVisual);
                this.startOffset = new Point(_horizontalOffset, _verticalOffset);
            }
        }

        /// <summary>
        /// Executed when mouse moves on the chrome.
        /// </summary>
        /// <param name="sender">Sender object.</param>
        /// <param name="e">Mouse event args.</param>
        private void Chrome_MouseMove(object sender, MouseEventArgs e)
        {
            if (this._isMouseCaptured && this.ContentRoot != null)
            {
                if (Application.Current.RootVisual == null)
                    return;

                // If the child window is dragged out of the page, return
                if (Application.Current != null && Application.Current.RootVisual != null &&
                    (e.GetPosition(Application.Current.RootVisual).X < 0 || e.GetPosition(Application.Current.RootVisual).Y < 0))
                {
                    return;
                }

                var currentPos = e.GetPosition(Application.Current.RootVisual);

                Debug.WriteLine("Click: x={0:0.0},y={1:0.0}\tPos: x={2:0.0},y={3:0.0}",
                    this._clickPoint.X,
                    this._clickPoint.Y,
                    currentPos.X,
                    currentPos.Y);
                
                _horizontalOffset = (currentPos.X - this._clickPoint.X) + startOffset.X;
                _verticalOffset = (currentPos.Y - this._clickPoint.Y) + startOffset.Y;

                this.SetValue(Canvas.TopProperty, _verticalOffset);
                this.SetValue(Canvas.LeftProperty, _horizontalOffset);            
            }
        }