原文:给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. 17、网卡驱动程序-DM9000举例

    (参考:cs89x0.c可以参考) DM9000 芯片实现网络功能的基础,在接收数据时采用中断方式,即当有数据到来并在 DM9000 内部 CRC 校验通过后会产生一个接收中断: 网卡驱动程序框架: ...

  2. 【Codeforces Round #439 (Div. 2) B】The Eternal Immortality

    [链接] 链接 [题意] 求b!/a!的最后一位数字 [题解] b-a>=20的话 a+1..b之间肯定有因子2和因子5 答案一定是0 否则暴力就好 [错的次数] 在这里输入错的次数 [反思] ...

  3. [Angular] Bind async requests in your Angular template with the async pipe and the "as" keyword

    Angular allows us to conveniently use the async pipe to automatically register to RxJS observables a ...

  4. Html表单使用实例

    原文 https://www.jianshu.com/p/b01f32844ac1 大纲 1.单选框多选框实现的商品选择 2.添加下拉框和删除下拉框 3.观察textarea中事件处理器的运行顺序 推 ...

  5. mariadb远程不能访问,出现Can't connect to MySQL server on '' (10061)

    一,现象: 1. 1 远程连接数据库mariadb时,报错 二,定位: 2. 1  首先本地连接上数据库,然后操作权限表数据 ,然后远程再次连接依然连接不上: 2. 2   搜索mariadb的配置文 ...

  6. python 升级pip

    废话少说,直接上图,希望谅解我的懒惰!:)

  7. 怎样推断DIV中的内容为空

    怎样推断DIV中的内容为空 1.问题背景 推断div内部是否为空.假设为空,给出无数据提示:否则显示正常页面 2.设计源代码 <!DOCTYPE html PUBLIC "-//W3C ...

  8. [React] Update Component State in React With Ramda Lenses

    In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. ...

  9. html中的span有value属性么(可以作为自定义属性在jquery中用)

    html中的span有value属性么(可以作为自定义属性在jquery中用) 一.总结 可以作为自定义属性在jquery中用 二.html中的span有value属性么 value并不是span标签 ...

  10. js获取浏览器尺寸

    Javascript: alert(document.body.clientWidth);        //网页可见区域宽(body) alert(document.body.clientHeigh ...