Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#在画布中拖放图像_C#_Wpf_Canvas_Drag And Drop_Draggable - Fatal编程技术网

C#在画布中拖放图像

C#在画布中拖放图像,c#,wpf,canvas,drag-and-drop,draggable,C#,Wpf,Canvas,Drag And Drop,Draggable,我试着在谷歌上搜索如何在画布上拖放UIElements,但找不到我想要的任何东西 我有一个带窗口的C#WPF应用程序。在窗口内,我得到了一块画布,可以在其中添加图像。 我想要的是能够拖放图像,同时保持在画布的边界内。 我还希望这是在代码中,所以不是在xaml中 我在向画布添加/更新图像的函数中得到了这一点。拖放事件应替换TODO Image img = ImageList[i].Image; img.Name = "Image" + i; // TODO: Drag and Drop even

我试着在谷歌上搜索如何在画布上拖放UIElements,但找不到我想要的任何东西

我有一个带窗口的C#WPF应用程序。在窗口内,我得到了一块画布,可以在其中添加图像。 我想要的是能够拖放图像,同时保持在画布的边界内。 我还希望这是在代码中,所以不是在xaml中

我在向画布添加/更新图像的函数中得到了这一点。拖放事件应替换TODO

Image img = ImageList[i].Image;
img.Name = "Image" + i;

// TODO: Drag and Drop event for Image

// TODO: Check if Left and Top are within Canvas (minus width / height of Image) 

Canvas.SetLeft(img, Left); // Default Left when adding the image = 0
Canvas.SetTop(img, Top); // Default Top when adding the image = 0

MyCanvas.Children.Add(img);
OnPropertyChanged("MyCanvas");
PS:虽然这是为了以后,如果有人有代码一次拖放多个图像作为额外的奖励,我将不胜感激


提前感谢您的帮助。

我做了一个项目,它使用了您的代码块,并在画布上拖放,查看它是否有任何差异,但我没有时间检查

private void pinCanvas_PreviewMouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
        {
         //   Point pt = e.GetPosition(pinCanvas);
         //   Curosor.Text = String.Format("You are at ({0}in, {1}in) in window coordinates", (pt.X / (96 / 72)) * 1/72, (pt.Y / (96 / 72)) * 1/72);
        }
        bool captured = false;
        double x_shape, x_canvas, y_shape, y_canvas;
        UIElement source = null;
        string elementName;
        double elementHeight, elementWidth;
        private void pinCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            setCanvasSize();
            source = (UIElement)sender;
            elementName = ((Label)source).Name;
            switch (elementName)
            {
                case "pinTextBox" :
                    elementHeight = pinActualHeight;
                    elementWidth = pinActualWidth;
                    break;
                case "serialTextBox" :
                    elementHeight = serialActualHeight;
                    elementWidth = serialActualWidth;
                    break;
                case "batchTextBox" :
                    elementHeight = batchActualHeight;
                    elementWidth = batchActualWidth;
                    break;
            }
            Mouse.Capture(source);
            captured = true;
            x_shape = Canvas.GetLeft(source);
            x_canvas = e.GetPosition(Maincanvas).X;
            y_shape = Canvas.GetTop(source);
            y_canvas = e.GetPosition(Maincanvas).Y;
        }

        private void pinCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (captured)
            {

                double x = e.GetPosition(Maincanvas).X;
                double y = e.GetPosition(Maincanvas).Y;
                var xCond = Math.Round(appActivities.DIP2Inch(x_shape), 4).ToString();
                var yCond = Math.Round(appActivities.DIP2Inch(y_shape), 4).ToString();
                var name = ((Label)source).Name;
                x_shape += x - x_canvas;
            //    if ((x_shape < Maincanvas.ActualWidth - elementWidth) && x_shape > 0)
           //     {
                    Canvas.SetLeft(source, x_shape);
                    switch (name)
                    {
                        case "pinTextBox" :
                            pinOffsetLeft.Text = xCond;
                            break;
                        case "serialTextBox" :
                            serialOffsetLeft.Text = xCond;
                            break;
                        case "batchTextBox" :
                            batchOffsetLeft.Text = xCond;
                            break;
                    }

         //       }
                x_canvas = x;
                y_shape += y - y_canvas;
            //    if (y_shape < Maincanvas.ActualHeight - elementHeight && y_shape > 0)
             //   {
                    Canvas.SetTop(source, y_shape);
                    switch (name)
                    {
                        case "pinTextBox":
                            pinOffsetTop.Text = yCond;
                            break;
                        case "serialTextBox":
                            serialOffsetTop.Text = yCond;
                            break;
                        case "batchTextBox":
                            batchOffsetTop.Text = yCond;
                            break;
                    }

          //      }
                y_canvas = y;
            }
        }

        private void pinCanvas_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Mouse.Capture(null);
            captured = false;
          //  MessageBox.Show((Canvas.GetTop(source)).ToString());
         /*   if (Canvas.GetTop(source) < 0)
            {
                Canvas.SetTop(source, 0);
            }
            if (Canvas.GetLeft(source) < 0)
            {
                Canvas.SetLeft(source, 0);
            }

            if (Canvas.GetLeft(source) > Maincanvas.ActualWidth - elementWidth)
            {
              //  MessageBox.Show("Left Too Much " + (Canvas.GetLeft(source) * 1/96).ToString());
                Canvas.SetLeft(source, Maincanvas.ActualWidth - elementWidth);
            }

            if (Canvas.GetTop(source) > Maincanvas.ActualHeight - elementHeight)
            {
                Canvas.SetTop(source, Maincanvas.ActualHeight - elementHeight);
            } */
            oneElemntTorched = true;
            //MessageBox.Show(this.pinTextBox.ActualHeight.ToString() + ", " + this.pinTextBox.ActualWidth.ToString());
        }
private void pinCanvas\u PreviewMouseLeftButtonDown\u 1(对象发送器,鼠标按钮ventargs e)
{
//点pt=e.GetPosition(pinCanvas);
//Curosor.Text=String.Format(“您在窗口坐标中的({0}英寸,{1}英寸),(pt.X/(96/72))*1/72,(pt.Y/(96/72))*1/72);
}
bool=false;
双x_形状,x_画布,y_形状,y_画布;
UIElement源=空;
字符串元素名;
双元素高度、元素宽度;
私有void pinCanvas_MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e)
{
setCanvasSize();
source=(UIElement)发送方;
elementName=((标签)源).Name;
开关(elementName)
{
案例“pinTextBox”:
elementHeight=销实际高度;
elementWidth=pinActualWidth;
打破
案例“serialTextBox”:
elementHeight=串行实际高度;
elementWidth=串行实际宽度;
打破
案例“batchTextBox”:
元素高度=批次实际高度;
elementWidth=批次实际宽度;
打破
}
鼠标捕捉(来源);
捕获=真;
x_shape=Canvas.GetLeft(源代码);
x_canvas=e.GetPosition(Maincanvas).x;
y_shape=Canvas.GetTop(源代码);
y_canvas=e.GetPosition(Maincanvas).y;
}
私有void pinCanvas_MouseMove(对象发送器,MouseEventArgs e)
{
如果(捕获)
{
双x=e.GetPosition(主画布).x;
双y=e.GetPosition(主画布).y;
var xCond=Math.Round(appActivities.DIP2Inch(x_形状),4).ToString();
var yCond=Math.Round(appActivities.DIP2Inch(y_形),4).ToString();
变量名称=((标签)源).name;
x_形状+=x-x_画布;
//if((x_形状0)
//     {
Canvas.SetLeft(源,x_形状);
交换机(名称)
{
案例“pinTextBox”:
pinOffsetLeft.Text=xCond;
打破
案例“serialTextBox”:
serialOffsetLeft.Text=xCond;
打破
案例“batchTextBox”:
batchOffsetLeft.Text=xCond;
打破
}
//       }
x_=x;
y_形状+=y-y_画布;
//if(y_形状0)
//   {
Canvas.SetTop(源,y_形状);
交换机(名称)
{
案例“pinTextBox”:
pinOffsetTop.Text=yCond;
打破
案例“serialTextBox”:
serialOffsetTop.Text=yCond;
打破
案例“batchTextBox”:
batchOffsetTop.Text=yCond;
打破
}
//      }
y_=y;
}
}
私有void pinCanvas_MouseLeftButtonUp(对象发送器,MouseButtonEventArgs e)
{
鼠标捕捉(空);
捕获=假;
//Show((Canvas.GetTop(source)).ToString());
/*if(Canvas.GetTop(源)<0)
{
Canvas.SetTop(源,0);
}
if(Canvas.GetLeft(源)<0)
{
Canvas.SetLeft(源,0);
}
if(Canvas.GetLeft(source)>maincavas.ActualWidth-elementWidth)
{
//Show(“左过多”+(Canvas.GetLeft(source)*1/96.ToString());
SetLeft(源代码,maincavas.ActualWidth-elementWidth);
}
if(Canvas.GetTop(source)>maincavas.ActualHeight-elementHeight)
{
Canvas.SetTop(源,maincavas.ActualHeight-elementHeight);
} */
oneElemntTorched=真;
//Show(this.pinTextBox.ActualHeight.ToString()+”,“+this.pinTextBox.ActualWidth.ToString());
}

使用以下代码修复了我下面的问题:

img.AllowDrop = true;
img.PreviewMouseLeftButtonDown += this.MouseLeftButtonDown;
img.PreviewMouseMove += this.MouseMove;
img.PreviewMouseLeftButtonUp += this.PreviewMouseLeftButtonUp;


private object movingObject;
private double firstXPos, firstYPos;
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e) {
    // In this event, we get the current mouse position on the control to use it in the MouseMove event.
    Image img = sender as Image;
    Canvas canvas = img.Parent as Canvas;

    firstXPos = e.GetPosition(img).X;
    firstYPos = e.GetPosition(img).Y;

    movingObject = sender;

    // Put the image currently being dragged on top of the others
    int top = Canvas.GetZIndex(img);
    foreach (Image child in canvas.Children)
        if (top < Canvas.GetZIndex(child))
            top = Canvas.GetZIndex(child);
    Canvas.SetZIndex(img, top + 1);
}
private void PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e) {
    Image img = sender as Image;
    Canvas canvas = img.Parent as Canvas;

    movingObject = null;

    // Put the image currently being dragged on top of the others
    int top = Canvas.GetZIndex(img);
    foreach (Image child in canvas.Children)
        if (top > Canvas.GetZIndex(child))
            top = Canvas.GetZIndex(child);
    Canvas.SetZIndex(img, top + 1);
}
private void MouseMove(object sender, MouseEventArgs e) {
    if (e.LeftButton == MouseButtonState.Pressed && sender == movingObject) {
        Image img = sender as Image;
        Canvas canvas = img.Parent as Canvas;

        double newLeft = e.GetPosition(canvas).X - firstXPos - canvas.Margin.Left;
        // newLeft inside canvas right-border?
        if (newLeft > canvas.Margin.Left + canvas.ActualWidth - img.ActualWidth)
            newLeft = canvas.Margin.Left + canvas.ActualWidth - img.ActualWidth;
        // newLeft inside canvas left-border?
        else if (newLeft < canvas.Margin.Left)
            newLeft = canvas.Margin.Left;
        img.SetValue(Canvas.LeftProperty, newLeft);

        double newTop = e.GetPosition(canvas).Y - firstYPos - canvas.Margin.Top;
        // newTop inside canvas bottom-border?
        if (newTop > canvas.Margin.Top + canvas.ActualHeight - img.ActualHeight)
            newTop = canvas.Margin.Top + canvas.ActualHeight - img.ActualHeight;
        // newTop inside canvas top-border?
        else if (newTop < canvas.Margin.Top)
            newTop = canvas.Margin.Top;
        img.SetValue(Canvas.TopProperty, newTop);
    }
}
img.AllowDrop=true;
img.PreviewMouseLeftButtonDown+=this.MouseLeftButtonDown;
img.PreviewMouseMove+=this.MouseMove;
img.PreviewMouseLeftButtonUp+=此.PreviewMouseLeftButtonUp;
私有对象移动对象;
私人双FirstXPO、FirstYPO;
私有void MouseLeftButtonDown(对象发送器,MouseButtonEventArgs e){
//在这个事件中,我们得到当前的鼠标po