使用WPF创建画图箭头
原文:使用WPF创建画图箭头
今天要给leader line画个箭头,所以就google一下,找到下面的文章,写的不错,可以实现我的需求,所以就摘录下来。
我把源代码中的arraw.cs加入到我的工程,修改了namespace,然后写了个方法进行调用:
private void DrawLeaderLineArrow(Point startPt, Point endPt)
{
Arrow arrow = new Arrow();
arrow.X1 = startPt.X;
arrow.Y1 = startPt.Y;
arrow.X2 = endPt.X;
arrow.Y2 = endPt.Y;
arrow.HeadWidth = 15;
arrow.HeadHeight = 5;
arrow.Stroke = Brushes.Black;
arrow.StrokeThickness = 1;
_canvas.Children.Add(arrow);
}
WPF Arrow and Custom Shapes
Introduction
WPF is the best UI Framework ever. It provides us with a large arsenal of vector graphic types such as Line
, Ellipse
, Path
and others. Sometimes we need shapes which are not provided in the WPF arsenal (such an Arrow
), and with all due respect to the Path
shape, which can be used to create any type of 2D shape, we do not want to recalculate each point every time. This is a good reason and opportunity to create a custom shape.
Background
WPF provides two kinds of vector types: Shapes and Geometries.
Shape
is any type that derives from the Shape
base class. It provides Fill
, Stroke
and other properties for coloring, and is actually a FrameworkElement
. Thus we can place shapes inside Panel
s, we can register shapes routed events and do anything related to FrameworkElement
. (MSDN)
Geometry
is any type that derives from the Geometry
base type. It provides properties for describing any type of 2D geometry. A geometry is actually a Freezable
type, thus can be frozen. A frozen object provides better performance by not notifying changes, and can be safely accessed by other threads. Geometry
is not Visual
, hence should be painted by other types such as Path
. (MSDN)
Using the Code
Now that we have a little background, and we know what the differences between a Geometry
and Shape
are, we can create our shape based on one of these two types. Correct?
Well, surprisingly we can't base our custom shape on the Geometry
type, since its one and only default constructor is marked as internal. Shame on you Microsoft.
Don't worry! We still have an option to base our custom shape on the Shape
base class.
Now, let’s say that we want to create an Arrow
shape. An arrow is actually a kind of line, so let’s derive our custom type from the WPF Line
type which has X1
, Y1
, X2
and Y2
properties.
Ooopps... Line
is sealed! (Shame on you twice).
Never mind, let's derive directly from the Shape
base class, and add X1
, Y1
, X2
, Y2
and two additional properties for defining the arrow's head width
and height
.
Our code should end up with something like this:
public sealed class Arrow : Shape
{
public static readonly DependencyProperty X1Property = ...;
public static readonly DependencyProperty Y1Property = ...;
public static readonly DependencyProperty HeadHeightProperty = ...;
... [TypeConverter(typeof(LengthConverter))]
public double X1
{
get { return (double)base.GetValue(X1Property); }
set { base.SetValue(X1Property, value); }
} [TypeConverter(typeof(LengthConverter))]
public double Y1
{
get { return (double)base.GetValue(Y1Property); }
set { base.SetValue(Y1Property, value); }
} [TypeConverter(typeof(LengthConverter))]
public double HeadHeight
{
get { return (double)base.GetValue(HeadHeightProperty); }
set { base.SetValue(HeadHeightProperty, value); }
}
... protected override Geometry DefiningGeometry
{
get
{
// Create a StreamGeometry for describing the shape
StreamGeometry geometry = new StreamGeometry();
geometry.FillRule = FillRule.EvenOdd; using (StreamGeometryContext context = geometry.Open())
{
InternalDrawArrowGeometry(context);
} // Freeze the geometry for performance benefits
geometry.Freeze(); return geometry;
}
} /// <summary>
/// Draws an Arrow
/// </summary>
private void InternalDrawArrowGeometry(StreamGeometryContext context)
{
double theta = Math.Atan2(Y1 - Y2, X1 - X2);
double sint = Math.Sin(theta);
double cost = Math.Cos(theta); Point pt1 = new Point(X1, this.Y1);
Point pt2 = new Point(X2, this.Y2); Point pt3 = new Point(
X2 + (HeadWidth * cost - HeadHeight * sint),
Y2 + (HeadWidth * sint + HeadHeight * cost)); Point pt4 = new Point(
X2 + (HeadWidth * cost + HeadHeight * sint),
Y2 - (HeadHeight * cost - HeadWidth * sint)); context.BeginFigure(pt1, true, false);
context.LineTo(pt2, true, true);
context.LineTo(pt3, true, true);
context.LineTo(pt2, true, true);
context.LineTo(pt4, true, true);
}
}
As you can see, it is very easy to implement a custom shape, thanks to the great work in the Shape
base class. All we have to do is derive our custom shape type from Shape
, and override the DefiningGeometry
property. This property should return a Geometry
of any type.
使用WPF创建画图箭头的更多相关文章
- 利用WPF创建含多种交互特性的无边框窗体
咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...
- WPF 创建无边框的圆角窗口
原文:WPF 创建无边框的圆角窗口 如题所述,在WPF中要创建一个没有边框且为圆角的窗体,有如下几步工作要进行: 第一步:去掉窗体默认样式的边框 首先将窗体的背景设为透明,将允许透明的属性设置为Tru ...
- WPF 创建自定义窗体
在前面的一篇博客"WPF 自定义Metro Style窗体",展示了如何创建一个类似于Metro Style的Window,并在程序中使用.但是这个窗体不能够自由的改变大小.今天的 ...
- wpf 创建动画三种方式
动画类型 : 故事版,CompositionTarget,DispachTime 那么到此,三种动态创建动画的方法都已经详细介绍过了,大家可能会有种感觉,比较钟情于第一种WPF/Silverlight ...
- 使用WPF创建无边框窗体
一.无边框窗口添加窗口阴影 实际上在WPF中添加无边框窗口的窗口阴影十分简单. 首先,设置WindowStyle="None"以及AllowsTransparency=" ...
- 使用 WPF 创建单实例应用程序
一个简单的例子就是大家在使用很多应用程序,例如在使用Microsoft Word 时会遇到一种情况,不管你打开多少个文档,系统一次只能加载一个winword.exe 实例.当打开新文档时,文档在新窗口 ...
- WPF InkCanvas 画图 基础使用教程
大家好,由于很多原因,我有很长一段时间没有在 CSDN 上分享我的学习成果了,如今终于可以回归分享之路了. 之前在做一个项目的时候,想在一个区域里绘制自己的图形,于是上网搜索资料,无意中找到了 Ink ...
- AutoCAD 凸度(bulge)的概念及使用WPF函数画图
前言 凸度(bulge)是AutoCAD 中一个非常重要的概念,凸度控制着两点之间弧度大小,弧度的方向.各种复杂的图像有可能就是成百上千的弧线组成的.从AutoCAD中导出的数据也有该值,一般的形式 ...
- WPF 创建用户控件并引用
项目源码地址:https://github.com/lizhiqiang0204/WpfControlLibrary.git 首先创建新项目->WPF用户控件库项目 在UserControl1. ...
随机推荐
- Ehcache与Memcache的差别
ehcache是纯java编写的.通信是通过RMI方式,适用于基于java技术的项目. memcachedserver端是c编写的.client有多个语言的实现,如c.php(淘宝.sina等各大门户 ...
- Qt浅谈之一:内存泄露(总结),对于QWidget可以setAttribute(Qt::WA_DeleteOnClose),而且绝对不能手动删除栈上的对象
一.简介 Qt内存管理机制:Qt 在内部能够维护对象的层次结构.对于可视元素,这种层次结构就是子组件与父组件的关系:对于非可视元素,则是一个对象与另一个对象的从属关系.在 Qt 中,在 Qt 中,删除 ...
- POJ 1384 Piggy-Bank (ZOJ 2014 Piggy-Bank) 完全背包
POJ :http://poj.org/problem?id=1384 ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode ...
- Socket编程模型之完毕port模型
转载请注明来源:viewmode=contents">http://blog.csdn.net/caoshiying?viewmode=contents 一.回想重叠IO模型 用完毕例 ...
- 解密Arm中国:全球最具影响力的芯片公司中国布局浮出水面
经济观察报 记者 陈伊凡 沈怡然 李华清 对于Arm与中国合资公司事宜,5月4日下午,Arm授权的代表邮件回复<经济观察报>称:“合资公司目前刚开始运营”,“我们的重点是让这个新的合资公司 ...
- linux下如何获取每个线程的CPU占用率
啥也不说,直接上脚本: root@Storage:/mnt/mtd# cat cpu.sh #!/bin/sh while truedo ps -H -eo user,pid,ppid, ...
- node-sass的安装问题
1.认识node-sass 我觉得要解决node-sass的问题,你首先至少要简单的了解node-sass是个什么东西?为什么要安装它? 对于在项目中使用sass的语法的时候,需要通过sass-loa ...
- ASP.NET 的 ViewState Cookie Session 等的比較
类型 值保存在哪 值的有效范围 备注 View State client 不能跨页面传递.仅仅能在当前页面保存数据. 在HTML中能够看到ViewState值,只是是加密. 不是明文. ViewSta ...
- jquery-10 js加载的时机如何选择
jquery-10 js加载的时机如何选择 一.总结 一句话总结:主要应用widow的ready()方法和load()方法. 1.内部文件中DOM加载完毕执行js如何书写? 把js标签放在body之后 ...
- 益智小游戏(app)
最好的益智类游戏要基于一定的数学原理. 一笔完成:(拓扑学,哥尼斯堡问题) 哥尼斯堡七桥问题