由于平时喜欢玩射击游戏,人又很菜,于是突发奇想做个显示准星一直显示在屏幕中间的小程序。
思路:创建透明窗口,在窗口中间画准星、允许点击窗口后面的内容
准星窗口:
1.设置窗口属性,画出中间准星
<Window x:Class="ShotPointHelper.PointWindow"
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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ShotPointHelper"
mc:Ignorable="d"
Title="ShadeWindow1" Height="450" Width="800" ResizeMode="NoResize" WindowState="Maximized" Topmost="True"
WindowStyle="None" AllowsTransparency="True" Background="Transparent" IsEnabled="False" IsHitTestVisible="True" >
<Grid>
<Rectangle Width="10" Height="2" Fill="Red" Margin="0,0,20,0" x:Name="rec1"/>
<Rectangle Width="10" Height="2" Fill="Red" Margin="20,0,0,0" x:Name="rec2"/>
<Rectangle Width="2" Height="10" Fill="Red" Margin="0,0,0,20" x:Name="rec3"/>
<Rectangle Width="2" Height="10" Fill="Red" Margin="0,20,0,0" x:Name="rec4"/>
</Grid>
</Window>
2.写公开方法,用于主窗口设置准星间距
public void SetDistinct(double distinct)
{
try
{
rec1.Margin = new Thickness(0, 0, distinct, 0);
rec2.Margin = new Thickness(distinct, 0, 0, 0);
rec3.Margin = new Thickness(0, 0, 0, distinct);
rec4.Margin = new Thickness(0, distinct, 0, 0);
}
catch { }
}
操作界面:
1.增加一个按钮,用于显示和隐藏准星窗口
PointWindow pointWindow = new PointWindow();
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Button btn = (Button)sender;
if(pointWindow.IsVisible)
{
pointWindow.Hide();
btn.Content = "打开";
}
else
{
pointWindow.Show();
btn.Content = "关闭";
}
}
2.增加滑动条,滑动修改准星间距
private void ProgressSlider1_ucValueChanged(object sender, EventArgs e)
{
ProgressSlider1 slider = sender as ProgressSlider1;
slider.MaxValue = 150;
pointWindow.SetDistinct(slider.Value);
}
成品:
程序非常简单,但是能在枯燥的代码之路增加点有意思的东西。