Vahid
Vahid

Reputation: 5444

How to get the boundaries of an area after scale/translate

I have a simple WPF interface with two panels. In the right one, I have two sliders which I use to scale and move a simple shape on the left panel.

I also have a MouseMove event that runs when I move the cursor over the shape.

When I use the slider to translate/scale the shape and then move the mouse cursor to get the mouse position I get the right results which are different for before and after the scale/transform. (as shown in the figures below).

What I want to do is how can I get the boundaries of the area (colored azure) after each transformation.

enter image description here

enter image description here

<Window x:Class="GUI.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Zoom Slider" Height="300" Width="600">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="300"></ColumnDefinition>
            <ColumnDefinition Width="300"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid Background="Azure" x:Name="_window" MouseMove="_window_MouseMove">
        <Canvas x:Name="_c" Width="100" Height="100" 
                Background="Black" RenderTransformOrigin="0.5,0.5">
            <Canvas.RenderTransform>
                <TransformGroup>
                    <ScaleTransform ScaleX="{Binding ElementName=_zoomSlider, Path=Value}" 
                                    ScaleY="{Binding ElementName=_zoomSlider, Path=Value}"/>
                        <TranslateTransform X="{Binding ElementName=_panSlider, Path=Value}"/>
                    </TransformGroup>
            </Canvas.RenderTransform>
        </Canvas>
        </Grid>
        <StackPanel Background="Aquamarine" Grid.Column="1">
            <Slider x:Name="_zoomSlider" 
                    Maximum="2" Minimum="0.5" Value="1" 
                    Width="200"/>
            <Slider x:Name="_panSlider" 
                    Maximum="100" Minimum="-100" Value="0" 
                    Width="200"/>            
            <Label x:Name="_mousePosition">Mouse Position</Label>
            <Label x:Name="_boundaries">Boundaries</Label>
        </StackPanel>
    </Grid>
</Window>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace GUI
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }


        private void _window_MouseMove(object sender, MouseEventArgs e)
        {
            _mousePosition.Content = (Math.Round(Mouse.GetPosition(_c).X)).ToString()
                + ", "
                + (Math.Round(Mouse.GetPosition(_c).Y)).ToString();
        }
    }
}

Upvotes: 0

Views: 1028

Answers (1)

dev hedgehog
dev hedgehog

Reputation: 8791

You seem to be asking for this:

http://msdn.microsoft.com/en-us/library/system.windows.uielement.transformtovisual(v=vs.95).aspx

// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myStackPanel.TransformToVisual(myTextBlock);

// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));

Upvotes: 3

Related Questions