当前位置: 首页 > article >正文

WPF 简单绘制矩形

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();	//释放捕获鼠标
	}
}


http://www.kler.cn/a/154978.html

相关文章:

  • 在 WPF 中,如何实现数据的双向绑定?
  • 【LeetCode】【算法】5. 最长回文子串
  • Linux 下 mysql 9.1 安装设置初始密码 【附脚本】
  • 微信小程序——01开发前的准备和开发工具
  • MyBatis——增删查改(XML 方式)
  • 力扣662:二叉树的最大宽度
  • 如何在没有备份的情况下从 Android 手机恢复已删除的数据
  • LLM推理部署(四):一个用于训练、部署和评估基于大型语言模型的聊天机器人的开放平台FastChat
  • 常见的AI安全风险(数据投毒、后门攻击、对抗样本攻击、模型窃取攻击等)
  • js中setinterval怎么用?怎么才能让setinterval停下来?
  • 微信小程序实现watch监听数值改变的效果
  • Kubernetes(K8s)_16_CSI
  • 时序预测 | Python实现LSTM长短期记忆神经网络时间序列预测(多图,多指标)
  • C#基础学习--命名空间和程序集
  • C语言之实现贪吃蛇小游戏篇(2)
  • Flink(九)【时间语义与水位线】
  • 开源播放器GSYVideoPlayer + ViewPager2 源码解析
  • 12.1 二叉树简单题
  • Redis--12--Redis分布式锁的实现
  • 【双指针】283. 移动零
  • 内网穿透工具获取一个公网ip
  • 提高wordpress网站收录速度,设置wp后台的“更新服务”功能
  • 【经验总结】网络关闭但ECU没有休眠前如何网络唤醒
  • Java 使用zxing生成二维码
  • Mybatis实用教程之XML实现动态sql
  • springboot教师进修培训管理系统设计与实现java+jsp