原文:WPF 用Clip属性实现蒙板特效

上一篇,已简单介绍Clip属性的用法,这一篇用它来实现简单蒙板功能,很简单,直接上代码

<Window x:Class="擦除效果.MainWindow"

        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="MainWindow" Height="537" Width="866" MouseMove="canvas_MouseMove" MouseDown="Window_MouseDown" MouseUp="Window_MouseUp">

    <Grid>

        <Rectangle Height="100" HorizontalAlignment="Left" Margin="164,92,0,0" Name="rectangle1" Stroke="Black" VerticalAlignment="Top" Width="200" Fill="Lime" />

        <Button Content="Button" Height="83" HorizontalAlignment="Left" Margin="526,109,0,0" Name="button1" VerticalAlignment="Top" Width="115" Click="button1_Click" />

        <Grid x:Name="grid"  Background="#88FFFFFF" ></Grid>

    </Grid>

    <Window.Background>

        <ImageBrush ImageSource="/擦除效果;component/Images/s01.jpg" />

    </Window.Background>

</Window>

后台:

    public partial class MainWindow : Window

    {

        private bool isDown = false;

        public MainWindow()

        {

            InitializeComponent();

            RectangleGeometry rg = new RectangleGeometry();

            rg.Rect = new Rect(0, 0, this.Width, this.Height);

            gridGeometry = Geometry.Combine(gridGeometry, rg, GeometryCombineMode.Union, null);

            grid.Clip = gridGeometry;

        }

        PathGeometry gridGeometry = new PathGeometry();

        private void canvas_MouseMove(object sender, MouseEventArgs e)

        {

            if (isDown)

            {

                EllipseGeometry rg = new EllipseGeometry();

                rg.Center = e.GetPosition(this);

                rg.RadiusX = 50;

                rg.RadiusY = 50;

                //排除几何图形

                gridGeometry = Geometry.Combine(gridGeometry, rg, GeometryCombineMode.Exclude, null);

                grid.Clip = gridGeometry;

            }

        }

        private void button1_Click(object sender, RoutedEventArgs e)

        {

            MessageBox.Show("OK!");

        }

        private void Window_MouseDown(object sender, MouseButtonEventArgs e)

        {

            isDown = true;

        }

        private void Window_MouseUp(object sender, MouseButtonEventArgs e)

        {

            isDown = false;

        }

    }

效果如下:

源码

最新文章

  1. 【XSS】延长 XSS 生命期
  2. JavaScript局部变量和全局变量的理解
  3. 两种设计模式(1)==&gt;&gt;“简单工厂”
  4. Socurce Insight 快捷键
  5. HDU 1574 RP问题
  6. JSP页面同时操作所有Input输入框
  7. [o] duplicate column name: _id 问题解决
  8. AspNetPager
  9. 网易云课堂_C++开发入门到精通_章节8:设计模式
  10. [LeetCode][Python]Longest Palindromic Substring
  11. html5 存储篇(一)
  12. Spark算子--union、intersection、subtract
  13. jquery 实现一个简单的成功提示框,失败提示框
  14. Java进阶篇之十五 ----- JDK1.8的Lambda、Stream和日期的使用详解(很详细)
  15. MySQL视图,函数,触发器,存储过程
  16. listview的gridview视图中,获取列中模板内的button按钮(找控件内的控件)
  17. Hadoop01的主要总结
  18. Windows系统使用vbs脚本或bat脚本强制杀死指定所有进程 vbs实现循环持续写入内容到vbs打开开的记事本 使用vbs、bat添加windows计划任务 使用cmd schtasks命令添加windows计划任务
  19. HttpClient post封装
  20. LFS(Linux From Scratch)学习

热门文章

  1. Mac OS X Kernel Basic User Credentials
  2. python爬虫(一)抓取 色影无忌图片
  3. 【SPOJ QTREE】树链剖分模板
  4. javascrit开发的基本代码结构的
  5. php面试题二--解决网站大流量高并发方案(从url到硬盘来解决高并发方案总结)
  6. [Node.js] Pass command line arguments to node.js
  7. CMakeListx.txt 编辑语法学习
  8. vs 2013 常用快捷键及常见问题的解决
  9. [tmux] Reuse terminal workspaces using tmux sessions
  10. Android使用JNI实现Java与C之间传递数据