原文:给WPF示例图形加上方便查看大小的格子

有时,我们为了方便查看WPF图形的样式及比例等,需要一些辅助性的格线,置于图形、图像的背景中。

比如下图,就是为了更清晰地查看折线的图形,我们画了用于标示位置大小的背景格:

那么,怎么绘制这样的格子呢?

为了更通用些,我把它做成资源的形式,放到app.xaml文件中的 <Application.Resources>节内:
<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="Microsoft.Samples.Graphics.app"
    Startup="myAppStartingUp">
    <Application.Resources>
     
      <DrawingBrush x:Key="MyGridBrushResource" Viewport="0,0,10,10" ViewportUnits="Absolute" TileMode="Tile">
        <DrawingBrush.Drawing>
          <DrawingGroup>
            <DrawingGroup.Children>
              <GeometryDrawing Brush="White"><!-- 这里是格子填充颜色 -->
                <GeometryDrawing.Geometry>
                  <RectangleGeometry Rect="0,0,1,1" />
                </GeometryDrawing.Geometry>
              </GeometryDrawing>
              <GeometryDrawing Geometry="M0,0 L1,0 1,0.1, 0,0.1Z" Brush="#CCCCFF" /><!-- 这里是横线 -->
              <GeometryDrawing Geometry="M0,0 L0,1 0.1,1, 0.1,0Z" Brush="#CCCCFF" /><!-- 这里是竖线 -->
            </DrawingGroup.Children>
          </DrawingGroup>
        </DrawingBrush.Drawing>
      </DrawingBrush>  

      <!-- 这里是外框线 -->
      <Style x:Key="MyGridBorderStyle">
        <Setter Property="Border.Background" Value="{StaticResource MyGridBrushResource}"/>
        <Setter Property="Border.HorizontalAlignment" Value="Center"/>
        <Setter Property="Border.VerticalAlignment" Value="Top"/>
        <Setter Property="Border.BorderBrush" Value="Black"/>
        <Setter Property="Border.BorderThickness" Value="1"/>
      </Style>

    </Application.Resources>
</Application>

其中,Viewport="0,0,10,10"中的10,10表示格子的像素宽度、高度值。

使用方法:
// PolylineExample.xaml
<!-- This example shows how to draw Polyline elements. -->
<Page     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="BrawDraw.Com.WPF.Graphics.PolylineExample"
    WindowTitle="Polyline Example">
<StackPanel>
  <StackPanel Margin="10">
    <Border Style="{StaticResource MyGridBorderStyle}">
      <Canvas Height="400" Width="400">
        <Polyline
          Points="10,110 60,10 110,110"
          Stroke="Black"
          StrokeThickness="4" />

        <Polyline
          Points="10,110 110,110 110,10"
          Stroke="Black"
          StrokeThickness="4"
          Canvas.Left="150" />

        </Canvas>
      </Border>
    </StackPanel>
  </StackPanel>
</Page>

C#代码:
// PolylineExample.xaml.cs
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace BrawDraw.Com.WPF.Graphics
{
    public partial class PolylineExample : Page
    {
        public PolylineExample()
        {
            InitializeComponent();
        }
    }
}

给WPF示例图形加上方便查看大小的格子的更多相关文章

  1. 给WPF示例图形加上方便查看大小的格子之完善版本

    原文:给WPF示例图形加上方便查看大小的格子之完善版本 去年10月份, 我曾写过一篇"给WPF示例图形加上方便查看大小的格子"的BLOG(http://blog.csdn.net/ ...

  2. WPF中如何调整TabControl的大小,使其跟随Window的大小而改变?

    多年不写技术博客,手生的很,也不知道大家都关注什么,最近在研究Wpf及3d模型的展示,碰到很多问题,这个是最后一个问题,写出来小结一下...... WPF中如何调整TabControl的大小,使其跟随 ...

  3. wpf无边框窗体移动和大小调整

    原文:wpf无边框窗体移动和大小调整   using System; using System.Windows; using System.Windows.Interop; namespace Wpf ...

  4. WPF三维图形

    原文:WPF三维图形 wpf 三维图形基础生成三维图形的基本思想是能得到一个物体的三维立体模型(model).由于我们的屏幕只有二维,因而我们定义了一个用于给物体拍照的照相机(Camera).拍到的照 ...

  5. 使用 WPF 生成图形

    下载代码示例 基于一组与测试有关的数据来生成图形是一项常见的软件开发任务.根据我的经验,最常用的方法是将数据导入 Excel 电子表格,然后使用 Excel 内置的绘图功能手动生成图形.这种做法适用于 ...

  6. WPF 基本图形

    一.WPF的基本图形 WPF图形的基类是Shape,所有的wpf图形类都是继承于Shape.Height,Width等决定它所处的面积,位置等,在没有设置图形宽高的情况,坐标位置为所在的容器的坐标,设 ...

  7. WPF 2D图形 Shape入门(一)--Shape

    本文是篇WPF Shape的入门文章 Shape 首先看看shape的继承链关系: 一个Shape具有哪些重要属性: 属性 说明 DefiningGeometry 默认的几何形状 RenderedGe ...

  8. PHP 文件夹操作「复制、删除、查看大小」迭代实现

    "既然递归能很好的解决,为什么还要用迭代呢"?主要的原因还是效率问题-- 递归的概念是函数调用自身,把一个复杂的问题分解成与其相似的多个子问题来解决,可以极大的减少代码量,使得程序 ...

  9. PHP 文件夹操作「复制、删除、查看大小」递归实现

    PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...

随机推荐

  1. MongoDB 管理

    1.给数据库增加分片功能 mongos> use admin mongos> db.runCommand({enablesharding:"cipnet"}) mong ...

  2. POJ 3628 Bookshelf 2 0-1背包

    传送门:http://poj.org/problem?id=3628 题目看了老半天,牛来叠罗汉- -|||和书架什么关系啊.. 大意是:一群牛来叠罗汉,求超过书架的最小高度. 0-1背包的问题,对于 ...

  3. 又在折腾cygwin

    apt-cyg https://github.com/transcode-open/apt-cyg/blob/master/README.md cygwin 163镜像 http://mirrors. ...

  4. Swift--使图片360° 周期旋转

    UIImageView+Extension.swift import UIKit extension UIImageView { // 360度旋转图片 func rotate360Degree() ...

  5. 使用C#版本的gdal库打开hdf文件

    作者:朱金灿 来源:http://blog.csdn.net/clever101 最近应同事的请求帮忙研究下使用C#版的gdal库读取hdf文件,今天算是有一点成果,特地做一些记录. 首先是编译C#版 ...

  6. 洛谷 P1709 隐藏口令Hidden Password

    ->题目链接 题解: 贪心+字符串 #include<iostream> #include<cstring> #define N 5000005 using namesp ...

  7. 【2013】将x插入有序数列

    Time Limit: 3 second Memory Limit: 2 MB 将一个数x插入到有序数列a中,插入后a仍然有序. Input 第一行输入有序数列a的元素个数 第二行依次输入a的元素,以 ...

  8. Android OkHttp网络连接封装工具类

    package com.lidong.demo.utils; import android.os.Handler; import android.os.Looper; import com.googl ...

  9. js导出报表

    原文链接:https://blog.csdn.net/qq_37936542/article/details/78376156 需求:项目中有一个学生签到模块需要导出每天的签到数据,一开始用poi在后 ...

  10. Linux文件编辑命令具体整理

    刚接触Linux,前几天申请了个免费体验的阿里云server,选择的是Ubuntu系统.配置jdk环境变量的时候须要编辑文件. vi命令编辑文件,百度了一下,非常多回答不是非常全面,因此编辑文件话了一 ...