WPF学习11:基于MVVM Light 制作图形编辑工具(2)
本文是WPF学习10:基于MVVM Light 制作图形编辑工具(1)的后续
这一次的目标是完成
两个任务。
画布
效果:
画布上,选择的方案是:直接以Image作为画布,使用RenderTargetBitmap绑定为Image的图片源,这样可以为后续的导出图片功能提供很大的便利。
对拖动栏XAML进行如下修改:
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Grid.Column="1" Grid.Row="1">
<Canvas VerticalAlignment="Top" HorizontalAlignment="Left" Width="{Binding ActualWidth,ElementName=ImageBorder}" SnapsToDevicePixels="False" Height="{Binding Path=ActualHeight,ElementName=ImageBorder}" Margin="50 50 0 0 " ClipToBounds="True">
<Border Name="ImageBorder" BorderBrush="Black" BorderThickness="1">
<Image Source="{Binding DrawingBitmap}">
</Image>
</Border>
</Canvas>
</ScrollViewer>
相应的,ViewModel中也要添加代码。
private RenderTargetBitmap _drawingBitmap; public RenderTargetBitmap DrawingBitmap
{
get { return _drawingBitmap; }
set
{
_drawingBitmap = value;
RaisePropertyChanged("DrawingBitmap");
}
}
到这里,画布的绑定就完成了。
现在要完成调节画布大小的相关代码,首先,在XAML增加两个输入框使得长宽由界面配置:
<TextBlock VerticalAlignment="Center"><Run Text="宽:"/></TextBlock>
<TextBox Width="50" Margin="0 0 10 0" Text="{Binding DrawingAreaWidth, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock VerticalAlignment="Center"><Run Text="高:"/></TextBlock>
<TextBox Width="50" Margin="0 0 10 0" Text="{Binding DrawingAreaHeight, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"/>
<Button Margin="10 0 10 0" Content="配置" Command="{Binding SetDrawingAreaSize}"/>
ViewModel部分:
private Int32 _drawingAreaWidth;
public Int32 DrawingAreaWidth
{
get { return _drawingAreaWidth; }
set
{
_drawingAreaWidth = value;
RaisePropertyChanged("DrawingAreaWidth");
}
} private Int32 _drawingAreaHeight;
public Int32 DrawingAreaHeight
{
get { return _drawingAreaHeight; }
set
{
_drawingAreaHeight = value;
RaisePropertyChanged("DrawingAreaHeight");
}
}
最后是Command SetDrawingAreaSize 的实现:
private ICommand _setDrawingAreaSize;
public ICommand SetDrawingAreaSize
{
get
{
return _setDrawingAreaSize ?? (_setDrawingAreaSize = new RelayCommand(() =>
{
DrawingBitmap = new RenderTargetBitmap(DrawingAreaWidth, DrawingAreaHeight,
96, 96, PixelFormats.Pbgra32);
var drawingVisual = new DrawingVisual();
using (var context = drawingVisual.RenderOpen())
{
context.DrawRectangle(Brushes.White, null,
new Rect(0, 0, DrawingAreaWidth, DrawingAreaHeight));
}
DrawingBitmap.Render(drawingVisual);
}
, () => (DrawingAreaWidth != 0 && DrawingAreaHeight != 0)));
}
}
至此,本节最开始的效果就完成啦。
直线
效果如下:
XAML中需要引入两个命名空间:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:command=http://www.galasoft.ch/mvvmlight
引入后我们就可以为Image添加三个响应鼠标的命令。
<Image Source="{Binding DrawingBitmap}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseMove">
<command:EventToCommand Command="{Binding MouseMoveCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
<i:EventTrigger EventName="MouseDown" >
<command:EventToCommand Command="{Binding MouseDownCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseUp" >
<command:EventToCommand Command="{Binding MouseUpCommand}" PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Image>
在界面上绘制Line控件,用于动态操作时的显示,操作完毕,隐藏控件,在Image上绘图。
<Line Stroke="Black" StrokeThickness="1" Visibility="{Binding LineVisibility}"
X1="{Binding PositionX1}" X2="{Binding PositionX2}"
Y1="{Binding PositionY1}" Y2="{Binding PositionY2}"/>
ViewModel添加以下属性,X2,Y1,Y2略。
private Visibility _lineVisibility = Visibility.Hidden;
public Visibility LineVisibility
{
get { return _lineVisibility; }
set
{
_lineVisibility = value;
RaisePropertyChanged("LineVisibility");
}
} private Double _positionX1;
public Double PositionX1
{
get { return _positionX1; }
set
{
_positionX1 = value;
RaisePropertyChanged("PositionX1");
}
}
为了让ViewModel能知道当前的绘图状态(直线,圆,矩形)在加一些数据绑定:
<RadioButton Style="{StaticResource StatusBarButton}" IsChecked="{Binding LineModeEnable}">
<Line X1="0" Y1="0" X2="15" Y2="15" Stroke="Black" StrokeThickness="1"></Line>
</RadioButton>
<RadioButton Style="{StaticResource StatusBarButton}" IsChecked="{Binding RectangleModeEnable}">
<Rectangle Width="20" Height="15" Stroke="Black" StrokeThickness="1"></Rectangle>
</RadioButton>
<RadioButton Style="{StaticResource StatusBarButton}" IsChecked="{Binding EllipseModeEnable}">
<Ellipse Width="20" Height="20" Stroke="Black" StrokeThickness="1"></Ellipse>
</RadioButton>
最后,我们编写三个鼠标相应的指令:
public ICommand SetDrawingAreaSize
{
get
{
return _setDrawingAreaSize ?? (_setDrawingAreaSize = new RelayCommand(() =>
{
DrawingBitmap = new RenderTargetBitmap(DrawingAreaWidth, DrawingAreaHeight,
96, 96, PixelFormats.Pbgra32);
var drawingVisual = new DrawingVisual();
using (var context = drawingVisual.RenderOpen())
{
context.DrawRectangle(Brushes.White, null,
new Rect(0, 0, DrawingAreaWidth, DrawingAreaHeight));
}
DrawingBitmap.Render(drawingVisual);
}
, () => (DrawingAreaWidth != 0 && DrawingAreaHeight != 0)));
}
} private ICommand _mouseMoveCommand;
public ICommand MouseMoveCommand
{
get
{
return _mouseMoveCommand ?? (_mouseMoveCommand = new RelayCommand<MouseEventArgs>((e) =>
{
PositionX2 = e.GetPosition((IInputElement)e.Source).X;
PositionY2 = e.GetPosition((IInputElement)e.Source).Y;
}
, (e) => true));
}
} private ICommand _mouseDownCommand;
public ICommand MouseDownCommand
{
get
{
return _mouseDownCommand ?? (_mouseDownCommand = new RelayCommand<MouseEventArgs>((e) =>
{
if(LineModeEnable)
LineVisibility = Visibility.Visible;
PositionX1 = e.GetPosition((IInputElement)e.Source).X;
PositionY1 = e.GetPosition((IInputElement)e.Source).Y;
}
, (e) => true));
}
} private ICommand _mouseUpCommand;
public ICommand MouseUpCommand
{
get
{
return _mouseUpCommand ?? (_mouseUpCommand = new RelayCommand<MouseEventArgs>((e) =>
{
var drawingVisual = new DrawingVisual();
using (var context = drawingVisual.RenderOpen())
{
//此处的-1用于消除画布边界带来的偏差,因为目前都是固定的,所以没有使用数据绑定。
if(LineModeEnable)
context.DrawLine(new Pen(Brushes.Black, 1), new Point(PositionX1 - 1, PositionY1 - 1), new Point(PositionX2 - 1, PositionY2 - 1 ));
}
DrawingBitmap.Render(drawingVisual);
LineVisibility = Visibility.Hidden;
}
, (e) => true));
}
}
圆形
效果:
XAML代码:
<Ellipse Stroke="Black" StrokeThickness="1" Visibility="{Binding EllipseVisibility}"
Canvas.Left="{Binding PositionX1}" Canvas.Top="{Binding PositionY1}"
Width="{Binding ShapeWidth}" Height="{Binding ShapeHeight}"></Ellipse>
MouseDown增加:
if (EllipseModeEnable)
{
ShapeWidth = ShapeHeight = 0;
EllipseVisibility = Visibility.Visible;
}
Move增加:
ShapeWidth = Math.Abs(PositionX2 - PositionX1);
ShapeHeight = Math.Abs(PositionY2 - PositionY1);
Up增加:
if(EllipseModeEnable)
context.DrawEllipse(new SolidColorBrush(Colors.White), new Pen(Brushes.Black, 1),
new Point(PositionX1 + ShapeWidth / 2 - 1, PositionY1 + ShapeHeight / 2 - 1), ShapeWidth / 2, ShapeHeight / 2);
矩形
效果如上,代码与圆形相似,故省略。
下一节将会完成图形的放大、缩小、移动,颜色的填充。
开发环境VS2013, .NET4.5
WPF学习11:基于MVVM Light 制作图形编辑工具(2)的更多相关文章
- WPF学习12:基于MVVM Light 制作图形编辑工具(3)
本文是WPF学习11:基于MVVM Light 制作图形编辑工具(2)的后续 这一次的目标是完成 两个任务. 本节完成后的效果: 本文分为三个部分: 1.对之前代码不合理的地方重新设计. 2.图形可选 ...
- WPF学习10:基于MVVM Light 制作图形编辑工具(1)
图形编辑器的功能如下图所示: 除了MVVM Light 框架是一个新东西之外,本文所涉及内容之前的WPF学习0-9基本都有相关介绍. 本节中,将搭建编辑器的界面,搭建MVVM Light 框架的使用环 ...
- WPF学习笔记-用Expression Design制作矢量图然后导出为XAML
WPF学习笔记-用Expression Design制作矢量图然后导出为XAML 第一次用Windows live writer写东西,感觉不错,哈哈~~ 1.在白纸上完全凭感觉,想象来画图难度很大, ...
- WPF学习08:MVVM 预备知识之COMMAND
WPF内建的COMMAND是GOF 提出的23种设计模式中,命令模式的实现. 本文是WPF学习07:MVVM 预备知识之数据绑定的后续,将说明实现COMMAND的三个重点:ICommand Comm ...
- 【Telerik控件学习】-建立自己的图形编辑工具(Diagram)
Telerik提供了RadDiagram控件,用于图形元素的旋转,拖拽和缩放.更重要的是,它还拓展了许多绑定的命令(复制,剪切,粘贴,回退等等). 我们可以用来组织自己的图形编辑工具. Step1.定 ...
- MAPZONE GIS SDK接入Openlayers3之五——图形编辑工具
图形编辑工具提供对要素图形进行增.删.改的功能,具体包括以下几种工具类型: 浏览工具 选择工具 创建要素工具 删除命令 分割工具 合并命令 节点编辑工具 修边工具 撤销命令 重做命令 工具的实现基本上 ...
- WPF学习07:MVVM 预备知识之数据绑定
MVVM是一种模式,而WPF的数据绑定机制是一种WPF内建的功能集,两者是不相关的. 但是,借助WPF各种内建功能集,如数据绑定.命令.数据模板,我们可以高效的在WPF上实现MVVM.因此,我们需要对 ...
- WPF学习笔记-用Expression Blend制作自定义按钮
1.从Blend工具箱中添加一个Button,按住shift,将尺寸调整为125*125; 2.右键点击此按钮,选择Edit control parts(template)>Edit a cop ...
- WPF学习笔记:MVVM模式下,ViewModel如何关闭View?
原文:http://blog.csdn.net/leftfist/article/details/32349731 矫枉过正,从一个极端走向另一个极端.MVVM模式,View只负责呈现,虽然也有后台代 ...
随机推荐
- HDU 2896 病毒侵袭 (AC自己主动机)
pid=2896">http://acm.hdu.edu.cn/showproblem.php?pid=2896 病毒侵袭 Time Limit: 2000/1000 MS (Java ...
- serverSpeed是一个android手机端到服务器间udp/tcp对比测速软件
https://github.com/eltld/serverSpeed https://github.com/c-wind/serverSpeed https://github.com/PeterK ...
- 在CentOS上把Nginx从1.2.4升级到1.6.0
在CentOS上升级把Nginx从1.2.4升级到1.6.0 摘要:本文记录了在CentOS 6.3上,把Nginx从1.2.4升级到1.6.0的过程. 1. 概述 在我做的一个项目中,最近我对生产服 ...
- vue入门教程 (vueJS2.X)
vue入门教程vueJS2.X 写在前面 看完此教程可以达到:能看懂并能修改简单的vue项目. 看的过程中,请把所有例子都放到html文件中跑一遍. Vue.js 是什么 Vue.js(读音 /vju ...
- FunsionCharts Demo
原文路径:http://www.cnblogs.com/xuhongfei/archive/2013/04/12/3016882.html 一.简介 Ø FusionCharts 是InfoSoft ...
- TCP/IP具体解释--TCP/IP可靠的原理 滑动窗体 拥塞窗体
TCP和UDP处在同一层---运输层,可是TCP和UDP最不同的地方是,TCP提供了一种可靠的数据传输服务,TCP是面向连接的,也就是说,利用TCP通信的两台主机首先要经历一个"拨打电话&q ...
- 常用的Sublime Text插件及安装方法
Package Control 功能:安装包管理 简介:sublime插件控制台,提供添加.删除.禁用.查找插件等功能 使用:https://sublime.wbond.net/installatio ...
- vim插件:显示树形目录插件NERDTree安装 和 使用【转】
本文转载自:https://my.oschina.net/VASKS/blog/388907 下载和配置 NERDTree插件的官方地址如下,可以从这里获取最新的版本 https://github.c ...
- bzoj3330: [BeiJing2013]分数
口胡 题目hint都给你是一个三分函数了 还不会上三分套三分吗 exp函数又卡 精度又卡 什么sb毒瘤题 浪费时间
- 洛谷 P1071 潜伏者 —— 模拟
题目:https://www.luogu.org/problemnew/show/P1071 按题意模拟即可. 代码如下: #include<iostream> #include<c ...