WPF DrawingContext Pen

<Window x:Class="WPFDrawing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPFDrawing"
mc:Ignorable="d"
Title="MainWindow"
Height=""
Width="">
<Grid>
<Grid.RowDefinitions> <RowDefinition Height="*" />
<RowDefinition Height="" />
</Grid.RowDefinitions>
<local:DrawingCanvas x:Name="drawCanvas"
Background="LightGray"
MouseLeftButtonDown="drawCanvas_MouseLeftButtonDown"
MouseMove="drawCanvas_MouseMove"> </local:DrawingCanvas>
<Button Content="test"
Width=""
Grid.Row=""
Click="Button_Click"></Button>
</Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes; namespace WPFDrawing
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Button_Click(null, null);
} private void Button_Click(object sender, RoutedEventArgs e)
{
var pts = new PointCollection();
pts.Add(new Point() { X = , Y = });
pts.Add(new Point() { X = , Y = });
Visual vs = drawCanvas.Polyline(pts, Brushes.Red, );
drawCanvas.AddVisual(vs);
} private void drawCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
{
var p = e.GetPosition(null);
if (vs != null)
{
drawCanvas.NewLine(vs, new Point() { X = , Y = }, p);
}
Console.WriteLine(p.X + " " + p.Y);
}
} DrawingVisual vs = null; private void drawCanvas_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var p = e.GetPosition(null);
Console.WriteLine(p.X + " " + p.Y);
vs = drawCanvas.GetVisual(p);
if (vs != null)
{
Console.WriteLine("已经选中了。。。。。");
}
}
}
}
using System.Collections.Generic;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows; namespace WPFDrawing
{
public class DrawingCanvas : Canvas
{
private List<Visual> visuals = new List<Visual>(); //获取Visual的个数
protected override int VisualChildrenCount
{
get { return visuals.Count; }
} //获取Visual
protected override Visual GetVisualChild(int index)
{
return visuals[index];
} //添加Visual
public void AddVisual(Visual visual)
{
visuals.Add(visual); base.AddVisualChild(visual);
base.AddLogicalChild(visual);
} //删除Visual
public void RemoveVisual(Visual visual)
{
visuals.Remove(visual); base.RemoveVisualChild(visual);
base.RemoveLogicalChild(visual);
} //命中测试
public DrawingVisual GetVisual(Point point)
{
HitTestResult hitResult = VisualTreeHelper.HitTest(this, point);
return hitResult.VisualHit as DrawingVisual;
} //使用DrawVisual画Polyline
public Visual Polyline(PointCollection points, Brush color, double thinkness)
{
DrawingVisual visual = new DrawingVisual();
DrawingContext dc = visual.RenderOpen();
Pen pen = new Pen(Brushes.Red, );
pen.Freeze(); //冻结画笔,这样能加快绘图速度 for (int i = ; i < points.Count - ; i++)
{
dc.DrawLine(pen, points[i], points[i + ]);
} dc.Close();
return visual;
} public void NewLine(DrawingVisual visual, Point ps, Point pd)
{
DrawingContext dc = visual.RenderOpen();
Pen pen = new Pen(Brushes.Red, );
pen.Freeze(); //冻结画笔,这样能加快绘图速度
dc.DrawLine(pen, ps, pd);
dc.Close();
} }
}
自定义流程图线段拖动改变长短,只是提供一种思路。
WPF DrawingContext Pen的更多相关文章
- WPF 如何画出1像素的线
如何有人告诉你,请你画出1像素的线,是不是觉得很简单,实际上在 WPF 上还是比较难的. 本文告诉大家,如何让画出的线不模糊 画出线的第一个方法,创建一个 Canvas ,添加一个线 界面代码 < ...
- WPF 绘制对齐像素的清晰显示的线条
此前有小伙伴询问我为何他 1 像素的线条显示发虚,然后我告诉他是“像素对齐”的问题,然而他设置了各种对齐像素的属性依旧没有作用.于是我对此进行了一系列试验,对 WPF 像素对齐的各种方法进行了一次总结 ...
- 2018-8-10-win10-uwp-气泡
title author date CreateTime categories win10 uwp 气泡 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 17: ...
- WPF的二维绘图(一)——DrawingContext
DrawingContext比较类似WinForm中的Graphics 类,是基础的绘图对象,用于绘制各种图形,它主要API有如下几种: 绘图API 绘图API一般形为DrawingXXX系列,常用的 ...
- 基于C#在WPF中使用斑马打印机进行打印【转】
原文链接:http://ju.outofmemory.cn/entry/132476 最近在项目中接手了一个比较有挑战性的模块——用斑马打印机将需要打印的内容打印出来.苦苦折腾了两天,总算有所收获,就 ...
- WPF自定义空心文字
首先创建一个自定义控件,继承自FrameworkElement,“Generic.xaml”中可以不添加样式. 要自定义空心文字,要用到绘制格式化文本FormattedText类.FormattedT ...
- WPF画线问题,几千条以后就有明显的延迟了。
我现在是这么画的,class A { private GeometryGroup _lines; private Path _path; public A() { _path.Data = ...
- WPF技巧-Canvas转为位图
转自:http://www.cnblogs.com/tmywu/archive/2010/09/14/1825650.html 在WPF中我们可以将Canvas当成一种画布,将Canvas中的控件当成 ...
- WPF打印原理,自定义打印
一.基础知识 1.System.Printing命名空间 我们可以先看一下System.Printing命名空间,东西其实很多,功能也非常强大,可以说能够控制打印的每一个细节,曾经对PrintDial ...
随机推荐
- C/C++结构体字节对齐详解
结构体的sizeof先看一个结构体:struct S1{ char c; int i;}; sizeof(S1)在VC6中按默认设置得到的结果为8.我们先看看sizeof的定义——size ...
- HDU 1142 A Walk Through the Forest(最短路+记忆化搜索)
A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- 【Step By Step】将Dotnet Core部署到Docker下
一.使用.Net Core构建WebAPI并访问Docker中的Mysql数据库 这个的过程大概与我之前的文章<尝试.Net Core—使用.Net Core + Entity FrameWor ...
- HDFS的Read过程分析
在hadoop中作为后端存储的文件系统HDFS发挥中重要的作用,HDFS是一个分布式文件系统,按照Google File System的思想开发的,针对的场景是低端服务器.写操作少而读操作多的情况.在 ...
- 深入理解计算机系统——系统级I/O
一.UNIX I/O 在UNIX系统中有一个说法,一切皆文件.所有的I/O设备,如网络.磁盘都被模型化为文件,而所有的输入和输出都被当做对相应文件的读和写来执行.这种将设备映射为文件的方式,允 ...
- mysql常见字符串处理函数
- java工作流引擎 Activiti6.0 websocket 即时聊天发图片文字 好友群组 SSM源码
时通讯:支持好友,群组,发图片.文件,消息声音提醒,离线消息,保留聊天记录 工作流模块--------------------------------------------------------- ...
- js获取今天,明天,本周五,下周五日期的函数
代码比较简单,随便写写 /** * a连接快速选择日期函数 */ function timeChooseSimple(key, me) { //today,tomorrow,thisWeek,next ...
- Linux系统初学-第二课 linux基础知识
一.用户与群组 Linux是多人多任务的操作系统,每个用户有一个主目录(或者叫家目录 /home),其他用户可以浏览,但是能否查看文件要看具体的权限设置.文件拥有者可以修改权限,选择是否允许其他用户进 ...
- WebGl 一个缓冲区传递颜色和坐标(矩形)
效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...