求解释代码,越详细越好
public MyCanvas(){
InitializeComponent();
this.Loaded += new RoutedEventHandler(MyCanvas_Loaded);
this.scrollViewer.AddHandler(Image.MouseMoveEvent, new MouseEventHandler(newImage_MouseMove), true);
}
/// <summary>
/// 用于在画布上拖动控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void newImage_MouseMove(object sender, MouseEventArgs e)
{
if (MouseButtonState.Pressed != Mouse.LeftButton)
{
return;
}
MyImage image = e.Source as MyImage;
if (image == null)
{
return;
}
isImagerDrag = true;
moveImage = image;
CurrentSelectMyImage = image;
NotifyPropertyChanged("CurrentSelectMyImage");
//此方法的第一个参数位置是需要拖动的数据的内容
//DragDropEffects.Move实现的是拖放的效果。Move 将拖动源的数据移动到放置的目标
DragDrop.DoDragDrop(image, new DataObject("aa"), DragDropEffects.Move);
}
void MyCanvas_Loaded(object sender, RoutedEventArgs e)
{
//页面加载时,网格画布处于中间位置
scrollViewer.ScrollToVerticalOffset(1600);
scrollViewer.ScrollToHorizontalOffset(800);
}
List<MyImage> images = new List<MyImage>();
private void canvas_Drop(object sender, DragEventArgs e)
{
isImagerDrag = false;
//验证拖入的是计算器 传入的 new DataObject(image)
ImageAndTextDecorator sourceIamge = e.Data.GetData(typeof(ImageAndTextDecorator)) as ImageAndTextDecorator;
if (sourceIamge == null)
{
return;
}
//位置计算
Point newLocation;
if (calculateLocation(e, out newLocation) == false)
{
return;
}
if (!(e.OriginalSource is Border))
{
return;
}
MyImage newImage = new MyImage();
newImage.Source = sourceIamge.ImageSource;
newImage.Width = newImage.Height = 39 + 39 + 39 + 1 + 1;//三个格子加两条边框
newImage.SetValue(Canvas.LeftProperty, newLocation.Y - 40);
newImage.SetValue(Canvas.TopProperty, newLocation.X - 40);
canvas.Children.Add(newImage);
newImage.Drop += new DragEventHandler(newImage_Drop);
images.Add(newImage);
// System.Diagnostics.Trace.WriteLine(e.OriginalSource);
if (border != null)
{
border.SetValue(Canvas.LeftProperty, 3.0);
border.SetValue(Canvas.TopProperty, 3.0);
border.Visibility = Visibility.Hidden;
}
}
//需要画线的控件组
List<LineControlBoth> lineControlBoth_List = new List<LineControlBoth>();
//将树控件拖放画布上的图像上
void newImage_Drop(object sender, DragEventArgs e)
{
//验证拖入的是计算器 传入的 new DataObject(image)
TextBlock txt = (TextBlock)e.Data.GetData(typeof(TextBlock));
if (txt == null)
{
return;
}
Point pos = e.GetPosition(canvas);
HitTestResult result = VisualTreeHelper.HitTest(canvas, pos);
if (result == null) return;
MyImage item = Uitls.FindVisualParent<MyImage>(result.VisualHit);
if (item == null)
{
return;
}
lineControlBoth_List.Add(new LineControlBoth() { SourceControl = txt, TargetControl = item });
}
Grid root2 = null;
Border border = null;
private bool calculateLocation(DragEventArgs e, out Point pointLocation)
{
pointLocation = new Point();
//鼠标在画布中的位置
Point pos = e.GetPosition(canvas);
//是否在允许拖放的位置上,上下左右各有42PX为不可用区域
if (pos.X <= 42)
{
return false;
}
if (pos.Y <= 42)
{
return false;
}
if (pos.X >= 1600 - 42)
{
return false;
}
if (pos.Y >= 3200 - 42)
{
return false;
}
//是否在允许拖放的位置上,滚动scrollViewer对可用区域有影响,比如各边的方框里有一部份在可示区域内
double scrollTop = this.scrollViewer.VerticalOffset;
double scrollLeft = this.scrollViewer.HorizontalOffset;
if (pos.X - scrollLeft < 42)
{
return false;
}
if (pos.Y - scrollTop < 42)
{
return false;
}
if (scrollViewer.ActualWidth - (pos.X - scrollLeft) < 42)
{
return false;
}
if (scrollViewer.ActualHeight - (pos.Y - scrollTop) < 42)
{
return false;
}
//计算顶点
int x = (int)pos.X % 40;
int y = (int)pos.Y % 40;
// double top = pos.X+(x>=20?40-x:-x) + 1.0;
// double left = pos.Y+(y>=20?40-y:-y) + 1.0;
double top = pos.X - x + 2.0;
double left = pos.Y - y + 2.0;
pointLocation.X = left;
pointLocation.Y = top;
return true;
}
bool isImagerDrag = false;
MyImage moveImage = null; --------------------编程问答-------------------- 除 --------------------编程问答-------------------- 好长的代码啊,帮你顶下 --------------------编程问答-------------------- //构造函数
public MyCanvas()
{
//调用设计器生成的代码初始化组件
InitializeComponent();
//注册窗体载入时的事件处理MyCanvas_Loaded
this.Loaded += new RoutedEventHandler(MyCanvas_Loaded);
//向scrollViewer控件的注册鼠标移动时的事件处理newImage_MouseMove
this.scrollViewer.AddHandler(Image.MouseMoveEvent, new MouseEventHandler(newImage_MouseMove), true);
--------------------编程问答-------------------- 这个 真蛋疼啊,解释一些特定的地方还有人有时间,全丢出来,╮(╯▽╰)╭
补充:.NET技术 , C#