WPF 简单绘制矩形

Canvas 画矩形:

view和viewModel 绑定一起才显示移动轨迹(可以定义一个string 看是否绑定属性的路径是正确的)

前台(绑定事件和显示移动的线):

XML 复制代码
<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和后台绑定:

cs 复制代码
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;
	}
}

鼠标移动绘制矩形

cs 复制代码
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();	//捕获鼠标
		}
	}
}

鼠标弹起完成矩形绘制

cs 复制代码
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();	//释放捕获鼠标
	}
}
相关推荐
吉量*7 小时前
WPF系列四:图形控件Rectangle
wpf
假男孩儿19 小时前
WPF 最小化到系统托盘
wpf
勇敢小菜鸟1 天前
WPF自定义窗口 输入验证不生效
wpf
鲤籽鲲1 天前
WPF TextBox 输入限制 详解
wpf
鸿喵小仙女1 天前
C# WPF读写STM32/GD32单片机Flash数据
stm32·单片机·c#·wpf
六点的晨曦1 天前
WPF的右键菜单项目引入DLL和DllImport特性引入DLL文件的异同点
wpf
一个不正经的林Sir1 天前
C#WPF基础介绍/第一个WPF程序
开发语言·c#·wpf
可喜~可乐2 天前
C# WPF开发
microsoft·c#·wpf
界面开发小八哥2 天前
DevExpress WPF中文教程:Grid - 如何移动和调整列大小?(二)
ui·.net·wpf·界面控件·devexpress·ui开发
界面开发小八哥2 天前
「实战应用」如何用图表控件SciChart WPF实现应用程序的DPI感知?
信息可视化·wpf·数据可视化·图表·scichart wpf·scichart