Hi Friends,
I am creating wpf window application. where i am adding images dynamically and the images are move freely with mouse down event on x-axis over the canvas.
what i need to do is
1> any image should not over lap on other image and they must have atleast 30pixel distance between them.
2> images should be stay in canvas. they should not go out of the canvas.
//--------------------------------------------------------wpf code-------------------------------------------------------
//------------------------------------------------------------------------------------------------------------------------
<Canvas Width="300" Height="200" Background="AntiqueWhite" DataContext="{Binding}">
<Image Name="image1" Source="/WpfApplication5;component/Images/Desert.jpg" Stretch="Fill" MouseLeftButtonDown="image1_MouseLeftButtonDown" MouseMove="image1_MouseMove" MouseLeftButtonUp="image1_MouseLeftButtonUp" Height="99" Width="126" StretchDirection="Both" Canvas.Left="-2" Canvas.Top="3" MouseWheel="image1_MouseWheel">
<Image.RenderTransform>
<TranslateTransform x:Name="imageTransform" X="5" Y="0" />
</Image.RenderTransform>
</Image>
<Label Canvas.Left="-49" Canvas.Top="210" Content="Height =" Height="28" Name="label1" Width="107" />
</Canvas>
//------------------------------------------------- c# code --------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------------------
public partial class MainWindow : Window
{
Point anchorPoint;
Point currentPoint;
bool isInDrag = false;
public MainWindow()
{
InitializeComponent();
Point anchorPoint;
Point currentPoint;
bool isInDrag = false;
}
private void image1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
FrameworkElement element = sender as FrameworkElement;
anchorPoint = e.GetPosition(null);
element.CaptureMouse();
isInDrag = true;
e.Handled = true;
}
private void image1_MouseMove(object sender, MouseEventArgs e)
{
if (isInDrag)
{
// FrameworkElement element = sender as FrameworkElement;
currentPoint = e.GetPosition(null);
imageTransform.X += (currentPoint.X - anchorPoint.X);
// imageTransform.Y += (currentPoint.Y - anchorPoint.Y);
label1.Content = imageTransform.X.ToString();
if (imageTransform.X > 120)
{
MessageBox.Show("grater");
}
anchorPoint = currentPoint;
}
}
private void image1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (isInDrag)
{
FrameworkElement element = sender as FrameworkElement;
element.ReleaseMouseCapture();
isInDrag = false;
e.Handled = true;
}
}
private void image1_MouseWheel(object sender, MouseWheelEventArgs e)
{
//// var st = (ScaleTransform)image.RenderTransform;
// var st = (ScaleTransform)image1.RenderTransform;
// double zoom = e.Delta > 0 ? .2 : -.2;
// st.ScaleX += zoom;
// st.ScaleY += zoom;
}
}