WPF刮刮乐
<Windowx:Class="WpfApp2.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:local="clr-namespace:WpfApp2"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"Title="MainWindow"Width="329"Height="450"MouseDown="UserControl_MouseDown"MouseMove="UserControl_MouseMove"MouseUp="UserControl_MouseUp"mc:Ignorable="d"><Grid><Grid.Background><ImageBrush ImageSource="/220810025-677037712005f5749b8c6481aa1b6c9a507047e97479d68af717064904b1db2d (1).png" Stretch="Uniform" /></Grid.Background><Gridx:Name="gridShadow"><Grid.Background><ImageBrush ImageSource="/325e99d639690e7e8a30d908d6c8b1cd399f0ac611a49e30a848ccf2eaae4ccf.png"/></Grid.Background><TextBlockHorizontalAlignment="Center"VerticalAlignment="Center"FontSize="30"Foreground="White">刮奖区</TextBlock></Grid></Grid> </Window>
using System; using System.Text; 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 WpfApp2 {/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{PathGeometry gridGeometry = new PathGeometry();private bool isDown = false;double originArea;bool hasShowResult = 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);gridShadow.Clip = gridGeometry;originArea = gridGeometry.GetArea();}private void UserControl_MouseMove(object sender, MouseEventArgs e){if (isDown){EllipseGeometry rg = new EllipseGeometry();rg.Center = e.GetPosition(gridShadow);rg.RadiusX = 20;rg.RadiusY = 20;//排除几何图形gridGeometry = Geometry.Combine(gridGeometry, rg, GeometryCombineMode.Exclude, null);gridShadow.Clip = gridGeometry;var currentArea = gridGeometry.GetArea();if ((currentArea * 100 / originArea) < 50 && !hasShowResult){hasShowResult = true;MessageBox.Show("恭喜中奖1个亿!");}}}private void UserControl_MouseDown(object sender, MouseButtonEventArgs e){isDown = true;}private void UserControl_MouseUp(object sender, MouseButtonEventArgs e){isDown = false;}}}