Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. centering cursor and object

centering cursor and object

Scheduled Pinned Locked Moved WPF
csharpcsswpfcomquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sadas232341s
    wrote on last edited by
    #1

    I' m building a sample in which I want to move an object. I'm not fully familiar with Blend and Xaml and the C# code behind, but I really don't understand why my cursor is far away from the object (is it canvas?... I don't know, in fact I'm shooting in the dark). Here' s my xaml:

    <UserControl
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="CanvasMove.MainPage"
    Width="640" Height="480">

    <Grid x:Name="LayoutRoot" Background="White">
    	<Canvas Height="80" HorizontalAlignment="Left" Margin="100,100,0,0" VerticalAlignment="Top" Width="80">
    		<Rectangle Fill="#FF80C1E4" Height="80" Width="80" RenderTransformOrigin="0.5,0.5" MouseLeftButtonUp="CanvasUp" MouseLeftButtonDown="CanvasDown"/>
    		<Rectangle Fill="#FF7AB7D8" Height="32" Width="32" Canvas.Left="24" Canvas.Top="24"/>
    			<Canvas.RenderTransform>
    				<TranslateTransform X="0" Y="0" x:Name="CanvasTransform"/>
    			</Canvas.RenderTransform>
    	</Canvas>
    </Grid>
    

    </UserControl>

    Here's my C#:

    protected override void OnMouseMove(MouseEventArgs e)
    {
    base.OnMouseMove(e);

        CanvasTransform.X = e.GetPosition(null).X;
        CanvasTransform.Y = e.GetPosition(null).Y;
    

    }

    M 1 Reply Last reply
    0
    • S sadas232341s

      I' m building a sample in which I want to move an object. I'm not fully familiar with Blend and Xaml and the C# code behind, but I really don't understand why my cursor is far away from the object (is it canvas?... I don't know, in fact I'm shooting in the dark). Here' s my xaml:

      <UserControl
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      x:Class="CanvasMove.MainPage"
      Width="640" Height="480">

      <Grid x:Name="LayoutRoot" Background="White">
      	<Canvas Height="80" HorizontalAlignment="Left" Margin="100,100,0,0" VerticalAlignment="Top" Width="80">
      		<Rectangle Fill="#FF80C1E4" Height="80" Width="80" RenderTransformOrigin="0.5,0.5" MouseLeftButtonUp="CanvasUp" MouseLeftButtonDown="CanvasDown"/>
      		<Rectangle Fill="#FF7AB7D8" Height="32" Width="32" Canvas.Left="24" Canvas.Top="24"/>
      			<Canvas.RenderTransform>
      				<TranslateTransform X="0" Y="0" x:Name="CanvasTransform"/>
      			</Canvas.RenderTransform>
      	</Canvas>
      </Grid>
      

      </UserControl>

      Here's my C#:

      protected override void OnMouseMove(MouseEventArgs e)
      {
      base.OnMouseMove(e);

          CanvasTransform.X = e.GetPosition(null).X;
          CanvasTransform.Y = e.GetPosition(null).Y;
      

      }

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      TCPMem wrote:

      I really don't understand why my cursor is far away from the object

      You are responding to all the MouseMove events for the UserControl. When you call e.GetPosition(null) you are getting the cursor position relative to the entire Silverlight plugin. Your Canvas is 100 pixels down and to the right of the plugin. The Rectangle you are moving is another 24 pixels down and to the right of the Canvas. So the cursor is always 124x124 away from the object. What are you trying to do? Do you want to be able to drag the rectangle with the mouse by clicking on it?

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      S 1 Reply Last reply
      0
      • M Mark Salsbery

        TCPMem wrote:

        I really don't understand why my cursor is far away from the object

        You are responding to all the MouseMove events for the UserControl. When you call e.GetPosition(null) you are getting the cursor position relative to the entire Silverlight plugin. Your Canvas is 100 pixels down and to the right of the plugin. The Rectangle you are moving is another 24 pixels down and to the right of the Canvas. So the cursor is always 124x124 away from the object. What are you trying to do? Do you want to be able to drag the rectangle with the mouse by clicking on it?

        Mark Salsbery Microsoft MVP - Visual C++ :java:

        S Offline
        S Offline
        sadas232341s
        wrote on last edited by
        #3

        "What are you trying to do? Do you want to be able to drag the rectangle with the mouse by clicking on it?" Yes. That's exactly what I want. How can I make it?

        M 1 Reply Last reply
        0
        • S sadas232341s

          "What are you trying to do? Do you want to be able to drag the rectangle with the mouse by clicking on it?" Yes. That's exactly what I want. How can I make it?

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          TCPMem wrote:

          That's exactly what I want. How can I make it?

          First, I misread your code so I was wrong about how far off the cursor was from the Canvas. I now see you're moving the entire canvas, not just the rectangle, which is the way I read it :) Anyway, here's a simple example that will hopefully get you going:

          <Grid x:Name="LayoutRoot" Background="White">
              <Canvas Height="80" HorizontalAlignment="Left" Margin="100,100,0,0" VerticalAlignment="Top" Width="80" 
                      MouseLeftButtonUp="Canvas\_MouseLeftButtonUp" 
                      MouseMove="Canvas\_MouseMove" 
                      MouseLeftButtonDown="Canvas\_MouseLeftButtonDown" >
                  <Rectangle Fill="#FF80C1E4" Height="80" Width="80" RenderTransformOrigin="0.5,0.5" />
                  <Rectangle Fill="#FF7AB7D8" Height="32" Width="32" Canvas.Left="24" Canvas.Top="24"/>
                  <Canvas.RenderTransform>
                      <TranslateTransform X="0" Y="0" x:Name="CanvasTransform"/>
                  </Canvas.RenderTransform>
              </Canvas>
          </Grid>
          
          //protected override void OnMouseMove(MouseEventArgs e)
          //{
          //    base.OnMouseMove(e);
          
          //    CanvasTransform.X = e.GetPosition(null).X;
          //    CanvasTransform.Y = e.GetPosition(null).Y;
          //}
          
          Point anchorPoint;
          bool isInDrag = false;
          
          private void Canvas\_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
          {
              FrameworkElement element = sender as FrameworkElement;
              anchorPoint = e.GetPosition(null);
              element.CaptureMouse();
              isInDrag = true;
          }
          
          private void Canvas\_MouseMove(object sender, MouseEventArgs e)
          {
              if (isInDrag)
              {
                  Point currentPoint = e.GetPosition(null);
                  CanvasTransform.X = CanvasTransform.X + currentPoint.X - anchorPoint.X;
                  CanvasTransform.Y = CanvasTransform.Y + currentPoint.Y - anchorPoint.Y;
                  anchorPoint = currentPoint;
              }
          }
          
          private void Canvas\_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
          {
              if (isInDrag)
              {
                  FrameworkElement element = sender as FrameworkElement;
                  element.ReleaseMouseCapture();
                  isInDrag = false;
              }
          }
          

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          S 1 Reply Last reply
          0
          • M Mark Salsbery

            TCPMem wrote:

            That's exactly what I want. How can I make it?

            First, I misread your code so I was wrong about how far off the cursor was from the Canvas. I now see you're moving the entire canvas, not just the rectangle, which is the way I read it :) Anyway, here's a simple example that will hopefully get you going:

            <Grid x:Name="LayoutRoot" Background="White">
                <Canvas Height="80" HorizontalAlignment="Left" Margin="100,100,0,0" VerticalAlignment="Top" Width="80" 
                        MouseLeftButtonUp="Canvas\_MouseLeftButtonUp" 
                        MouseMove="Canvas\_MouseMove" 
                        MouseLeftButtonDown="Canvas\_MouseLeftButtonDown" >
                    <Rectangle Fill="#FF80C1E4" Height="80" Width="80" RenderTransformOrigin="0.5,0.5" />
                    <Rectangle Fill="#FF7AB7D8" Height="32" Width="32" Canvas.Left="24" Canvas.Top="24"/>
                    <Canvas.RenderTransform>
                        <TranslateTransform X="0" Y="0" x:Name="CanvasTransform"/>
                    </Canvas.RenderTransform>
                </Canvas>
            </Grid>
            
            //protected override void OnMouseMove(MouseEventArgs e)
            //{
            //    base.OnMouseMove(e);
            
            //    CanvasTransform.X = e.GetPosition(null).X;
            //    CanvasTransform.Y = e.GetPosition(null).Y;
            //}
            
            Point anchorPoint;
            bool isInDrag = false;
            
            private void Canvas\_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
            {
                FrameworkElement element = sender as FrameworkElement;
                anchorPoint = e.GetPosition(null);
                element.CaptureMouse();
                isInDrag = true;
            }
            
            private void Canvas\_MouseMove(object sender, MouseEventArgs e)
            {
                if (isInDrag)
                {
                    Point currentPoint = e.GetPosition(null);
                    CanvasTransform.X = CanvasTransform.X + currentPoint.X - anchorPoint.X;
                    CanvasTransform.Y = CanvasTransform.Y + currentPoint.Y - anchorPoint.Y;
                    anchorPoint = currentPoint;
                }
            }
            
            private void Canvas\_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
            {
                if (isInDrag)
                {
                    FrameworkElement element = sender as FrameworkElement;
                    element.ReleaseMouseCapture();
                    isInDrag = false;
                }
            }
            

            Mark Salsbery Microsoft MVP - Visual C++ :java:

            S Offline
            S Offline
            sadas232341s
            wrote on last edited by
            #5

            I used http://msdn.microsoft.com/en-us/library/cc189066(VS.95).aspx this code to move an object only and wondered how can I do this with the entire canvas. I took a look at your code and replaced

            Rectangle item = sender as Rectangle;

            with yours:

            FrameworkElement element = sender as FrameworkElement;

            and it happened. Thanks!

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups