绘制一个矩形

时间:2014-12-31 14:44:07

标签: c# wpf

当我按住鼠标右键并在图像上移动鼠标时,我试图在图像上绘制一个矩形。 在鼠标单击时,布尔值更改为true,当鼠标移动事件发生且bool为true时,我有下一个代码:

            // currMouselocation - the current mouse location rightClickTrackLocation - the point where the user right clicked
            trackRect.Visibility = Visibility.Visible;//making the rectangle visible
            var xPos = Math.Min(currMouselocation.X, rightClickTrackLocation.X);
            var yPos = Math.Min(currMouselocation.Y, rightClickTrackLocation.Y);
            var w = Math.Max(currMouselocation.X, rightClickTrackLocation.X) - xPos;
            var h = Math.Max(currMouselocation.Y, rightClickTrackLocation.Y) - yPos;
            trackRect.Width = w;
            trackRect.Height = h;

            Canvas.SetLeft(trackRect, xPos);
            Canvas.SetTop(trackRect, yPos);

问题是矩形是从图像的中心绘制的,而不是从点击的点绘制的。

0 个答案:

没有答案