Canvas 画矩形:
view和viewModel 绑定一起才显示移动轨迹(可以定义一个string 看是否绑定属性的路径是正确的)
前台(绑定事件和显示移动的线):
<Canvas Name="canvas" Background="#01FFFFFF"
PreviewMouseLeftButtonDown="VDM_MouseLeftButtonDown"
PreviewMouseMove="VDM_MouseMove"
PreviewMouseLeftButtonUp="VDM_MouseLeftButtonUp">
</Canvas>
<Canvas>
<Rectangle IsHitTestVisible="False" Stroke="Red" x:Name="rect"
StrokeDashArray="1,2" StrokeThickness="1" Stretch="Fill"
Width="{Binding RectWidth}"
Height="{Binding RectHeight}"
Canvas.Left="{Binding RectLeft}"
Canvas.Top="{Binding RectTop}"
/>
</Canvas>
后台:
若view和其他viewmodel绑定了 可以用(rect.DataContext = Model; DVMModel Model = new DVMModel(); DVMModel属性定义)
如果view和后台绑定:
bool is_LeftButtonDown = false;
public void VDM_MouseLeftButtonDown(object view, MouseButtonEventArgs e)
{
Canvas canvas = view as Canvas;
Point pt = Mouse.GetPosition(canvas);
downPt = pt;
is_LeftButtonDown = true;
if (pt.X < 0 || pt.X > canvas.ActualWidth || pt.Y < 0 || pt.Y > canvas.ActualHeight)
{
is_LeftButtonDown = false;
}
}
public void VDM_MouseMove(object view, MouseEventArgs e)
{
//鼠标坐标点
Point pt = Mouse.GetPosition(view as Canvas);
if (is_LeftButtonDown && e.LeftButton == MouseButtonState.Pressed) //确定鼠标左键处于按下状态
{
isMove = true;
if (pt.X < 0 || pt.X > canvas.ActualWidth || pt.Y < 0 || pt.Y > canvas.ActualHeight)
{
is_LeftButtonDown = false;
Model.RectHeight = 0;
Model.RectWidth = 0;
}
else
{
double left = (downPt.X < pt.X) ? downPt.X : pt.X;
double top = (downPt.Y < pt.Y) ? downPt.Y : pt.Y;
double width = Math.Abs(downPt.X - pt.X);
double height = Math.Abs(downPt.Y - pt.Y);
// 画矩形
Model.RectLeft = left;
Model.RectTop = top;
Model.RectWidth = width;
Model.RectHeight = height;
canvas.CaptureMouse(); //捕获鼠标
}
}
}
public void VDM_MouseLeftButtonUp(object view, MouseButtonEventArgs e)
{
Canvas canvas = view as Canvas;
if (is_LeftButtonDown && isMove)
{
is_LeftButtonDown = false;
isMove = false;
Point pt = Mouse.GetPosition(canvas);
double Wscale = canvas.ActualWidth / SingalSize.Width; //SingalSize 分辨率
double Hscale = canvas.ActualHeight / SingalSize.Height;
double left = (downPt.X < pt.X) ? downPt.X : pt.X;
double top = (downPt.Y < pt.Y) ? downPt.Y : pt.Y;
double width = Math.Abs(pt.X - downPt.X);
double height = Math.Abs(pt.Y - downPt.Y);
OsdStartX.Text = Convert.ToInt32(left).ToString();
OsdStartY.Text = Convert.ToInt32(top).ToString();
if (width * Wscale > 512)
HLength.Text = "512";
else
HLength.Text = Convert.ToInt32(width * Wscale).ToString();
if (height * Hscale > 64)
VLength.Text = "64";
else
VLength.Text = Convert.ToInt32(height * Hscale).ToString();
// 画矩形
Model.RectHeight = 0;
Model.RectWidth = 0;
canvas.ReleaseMouseCapture(); //释放捕获鼠标
}
}
原文地址:https://blog.csdn.net/BYH371256/article/details/134722474
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_24530.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。