无意间发现抖音里有一个段视频,逼迫批准辞职的小程序。虽然小白菜,但闲来无事,举手投足间打发时间。
这里主要分以下几步骤:
- 布局:在顶端放置内容,在中间布满按键,在底部排一排,并将最后一排头尾两个按键显示,其他按键全部隐藏。
- 按键事件:除最后一个按键添加点击触发事件外,其他所有按键统一为鼠标进入触发事件,并共用一个触发事件。
- 点击事件处理“批准”事件,代表同意“辞职”,鼠标移动事件为“不批准”事件;最后一个事件为关闭事件,该事件只有批准事件发生,才会退出程序。
先看一下源码:
1 <Window x:Class="Resignation.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="MainWindow" Height="350" Width="525"> 5 <Grid Background="Black"> 6 <DockPanel VerticalAlignment="Top"> 7 <StackPanel DockPanel.Dock="Top"> 8 <TextBlock HorizontalAlignment="Center" Foreground="White" Width="auto" FontWeight="Bold" FontSize="24"> 9 I have to resign, please kindly approve. 10 </TextBlock> 11 </StackPanel> 12 <StackPanel DockPanel.Dock="Left" Orientation="Vertical"> 13 <Button Name="btn00_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 14 <Button Name="btn01_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 15 <Button Name="btn02_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 16 <Button Name="btn03_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 17 </StackPanel> 18 <StackPanel DockPanel.Dock="Left" Orientation="Vertical"> 19 <Button Name="btn10_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 20 <Button Name="btn11_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 21 <Button Name="btn12_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 22 <Button Name="btn13_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 23 </StackPanel> 24 <StackPanel DockPanel.Dock="Left" Orientation="Vertical"> 25 <Button Name="btn20_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 26 <Button Name="btn21_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 27 <Button Name="btn22_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 28 <Button Name="btn23_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 29 </StackPanel> 30 <StackPanel DockPanel.Dock="Left" Orientation="Vertical"> 31 <Button Name="btn30_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 32 <Button Name="btn31_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 33 <Button Name="btn32_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 34 <Button Name="btn33_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 35 </StackPanel> 36 <StackPanel DockPanel.Dock="Left" Orientation="Vertical"> 37 <Button Name="btn40_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 38 <Button Name="btn41_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 39 <Button Name="btn42_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 40 <Button Name="btn43_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 41 </StackPanel> 42 </DockPanel> 43 <DockPanel VerticalAlignment="Bottom"> 44 <StackPanel DockPanel.Dock="Left" Orientation="Horizontal"> 45 <Button Name="btn50_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter"/> 46 <Button Name="btn51_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 47 <Button Name="btn52_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 48 <Button Name="btn53_ok" Content="Disapproved" Width="80" Height="30" Margin="10" MouseEnter="btn1_ok_MouseEnter" Visibility="Hidden"/> 49 <Button Name="btn54_ok" Content="Approved" Width="80" Height="30" Margin="10" FontWeight="Bold" FontSize="14" Click="approved_Click"/> 50 </StackPanel> 51 </DockPanel> 52 </Grid> 53 </Window>
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using System.Windows; 7 using System.Windows.Controls; 8 using System.Windows.Data; 9 using System.Windows.Documents; 10 using System.Windows.Input; 11 using System.Windows.Media; 12 using System.Windows.Media.Imaging; 13 using System.Windows.Navigation; 14 using System.Windows.Shapes; 15 16 namespace Resignation 17 { 18 /// <summary> 19 /// Interaction logic for MainWindow.xaml 20 /// </summary> 21 public partial class MainWindow : Window 22 { 23 public MainWindow() 24 { 25 InitializeComponent(); 26 Closing += window_close; 27 } 28 int i = 0; 29 bool IsApproved = false; 30 string[] abuf = { 31 "btn00_ok", "btn01_ok", "btn02_ok", "btn03_ok", 32 "btn10_ok", "btn11_ok", "btn12_ok", "btn13_ok", 33 "btn20_ok", "btn21_ok", "btn22_ok", "btn23_ok", 34 "btn30_ok", "btn31_ok", "btn32_ok", "btn33_ok", 35 "btn40_ok", "btn41_ok", "btn42_ok", "btn43_ok", 36 "btn50_ok", "btn51_ok", "btn52_ok", "btn53_ok", 37 }; 38 private void btn1_ok_MouseEnter(object sender, MouseEventArgs e) 39 { 40 var d = sender as Button; 41 d.Visibility = Visibility.Hidden; 42 i = DateTime.Now.Millisecond % 24; 43 if (abuf[i] == d.Name) 44 { 45 if (++i >= 24) 46 i = 0; 47 } 48 ((Button)this.FindName(abuf[i++])).Visibility=Visibility.Visible; 49 } 50 private void approved_Click(object sender, RoutedEventArgs e) 51 { 52 btn54_ok.Content = "Thank You"; 53 btn54_ok.Foreground = new SolidColorBrush(Colors.LightCyan); 54 btn54_ok.Background = System.Windows.Media.Brushes.ForestGreen; 55 IsApproved = true; 56 } 57 private void window_close(object sender, System.ComponentModel.CancelEventArgs ex) 58 { 59 if (IsApproved == false) 60 { 61 MessageBox.Show("Please Approved, thanks."); 62 ex.Cancel = true; 63 return; 64 } 65 MessageBox.Show("Thank you! GoodBye!"); 66 } 67 } 68 }
效果:
End.
Thank you.