My canvas is fixed.....I want to only resize rectangle inside canvas.....Here is my code:-
xaml:
<Canvas x:Name="rootCanvas"
Width="640"
Height="480"
Background="Pink"
>
<!-- You can drag this rectangle around the canvas. -->
<Rectangle
MouseLeftButtonDown="Handle_MouseDown"
MouseMove="Handle_MouseMove"
MouseLeftButtonUp="Handle_MouseUp"
Canvas.Left="30" Canvas.Top="30" Fill="Red"
Width="50" Height="50" />
</Canvas>
xaml.cs:
namespace demo
{
public partial class MainPage : UserControl
{
bool isMouseCaptured;
double mouseVerticalPosition;
double mouseHorizontalPosition;
public MainPage()
{
InitializeComponent();
}
public void Handle_MouseDown(object sender, MouseEventArgs args)
{
Rectangle item = sender as Rectangle;
mouseVerticalPosition = args.GetPosition(null).Y;
mouseHorizontalPosition = args.GetPosition(null).X;
isMouseCaptured = true;
item.CaptureMouse();
}
public void Handle_MouseMove(object sender, MouseEventArgs args)
{
Rectangle item = sender as Rectangle;
if (isMouseCaptured)
{
// Calculate the current position of the object.
double deltaV = args.GetPosition(null).Y - mouseVerticalPosition;
double deltaH = args.GetPosition(null).X - mouseHorizontalPosition;
double newTop = deltaV + (double)item.GetValue(Canvas.TopProperty);
double newLeft = deltaH + (double)item.GetValue(Canvas.LeftProperty);
//Set new position of object.
item.SetValue(Canvas.TopProperty, newTop);
item.SetValue(Canvas.LeftProperty, newLeft);
//Update position global variables.
mouseVerticalPosition = args.GetPosition(null).Y;
mouseHorizontalPosition = args.GetPosition(null).X;
}
}
public void Handle_MouseUp(object sender, MouseEventArgs args)
{
Rectangle item = sender as Rectangle;
isMouseCaptured = false;
item.ReleaseMouseCapture();
mouseVerticalPosition = -1;
mouseHorizontalPosition = -1;
}
}
}
please suggest me suitable solution...........
Answer: 1
Hi,
Do you mean you want to reposition rectangle only inside rootCanvas, not resize?
If you answer is yes, then you can modify your code like this:
//Set new position of object. if (newTop > 0 && newTop < rootCanvas.Height - item.Height) item.SetValue(Canvas.TopProperty, newTop); if (newLeft > 0 && newLeft < rootCanvas.Width - item.Width) item.SetValue(Canvas.LeftProperty, newLeft);
Just add an condition before you reposition the rectangle. Please feel free to let me know if you have any question.
Best Regards,
Answer: 2
Thanks Johnson
Answer: 3
Now i want to resize the shapes (Rectangle,Ellipse etc) from all sides................
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog