透明覆盖在软件上的帮助的实现
接到任务,下周要给软件做一个帮助,要类似于许多网站上的那种教程,于是自己想了下实现的方式,发现原来是这么的简单:
1、做一个UC,放在需要出帮助的window上;
2、根据需要给UC进行布局,在需要高亮的地方,设置 Opacity="0";不需要高亮的地方,设置Background="Gray" Opacity="0.2";
原来的window
放入帮助的界面之后的Window,上面的按钮被背景挡着,效果可以看的出来,也不能被使用,中间的按钮是在完全透明的区域内,能够使用
3、控制UC的大小和出现的时机,就可以了;
我实现以上逻辑的代码:
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1" Title="MainWindow" Height="350" Width="525"> <Grid> <Button HorizontalAlignment="Center" VerticalAlignment="Top" Height="50" Width="50" Margin="0,10"/> <local:jiaocheng x:Name="jc"/> <Button Content="ssss" Width="80" Height="25" Click="Button_Click"/> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; 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; using System.Windows.Forms; using System.IO; using System.Drawing; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { NotifyIcon notifyIcon = new NotifyIcon(); private System.Windows.Forms.ContextMenu contextMenu; private System.Windows.Forms.MenuItem menuItemExit; private System.Windows.Forms.MenuItem menuItemOpen; public MainWindow() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { //this.ShowInTaskbar = false; iconShow(); this.jc.Width = 300; this.jc.Height = 300; } /// <summary> /// 显示小图标 /// </summary> private void iconShow() { //window.notifyIcon.BalloonTipText = "Hello, 财蕴天下!"; notifyIcon.Text = "金拐棍"; //Stream stream = this.GetType().Assembly. // GetManifestResourceStream("E:\\WPFStudy\\WpfApplication1\\logo_setup.ico"); //if (stream != null) //{ notifyIcon.Icon = new Icon("E:\\WPFStudy\\WpfApplication1\\logo_setup.ico"); //} notifyIcon.Visible = true; notifyIcon.MouseDoubleClick += onNotifyIconDoubleClick; //window.notifyIcon.ShowBalloonTip(500); //创建右键菜单 this.contextMenu = new System.Windows.Forms.ContextMenu(); this.menuItemExit = new System.Windows.Forms.MenuItem(); this.menuItemOpen = new System.Windows.Forms.MenuItem(); // Initialize contextMenu1 //添加右键菜单项 this.contextMenu.MenuItems.Add(menuItemExit); this.contextMenu.MenuItems.Add(menuItemOpen); // Initialize menuItem1 //右键菜单项内容 this.menuItemExit.Index = 1; this.menuItemExit.Text = "退出"; this.menuItemExit.Click += new System.EventHandler(this.menuItemExit_Click); //右键菜单项内容 this.menuItemOpen.Index = 0; this.menuItemOpen.Text = "打开"; this.menuItemOpen.Click += new System.EventHandler(this.menuItemOpen_Click); notifyIcon.ContextMenu = contextMenu; UpdateLayout(); } /// <summary> /// 右键菜单点击事件 /// </summary> /// <param name="Sender"></param> /// <param name="e"></param> private void menuItemExit_Click(object Sender, EventArgs e) { this.Closing += new System.ComponentModel.CancelEventHandler(this.WinMainWindow_Closing); this.Close(); } /// <summary> /// 窗口关闭事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public void WinMainWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) { this.Close(); } /// <summary> /// 右键菜单点击事件 /// </summary> /// <param name="Sender"></param> /// <param name="e"></param> private void menuItemOpen_Click(object Sender, EventArgs e) { qPan_OpenFromTuoPan(); } /// <summary> /// 小图标双击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void onNotifyIconDoubleClick(object sender, EventArgs e) { qPan_OpenFromTuoPan(); } /// <summary> /// 打开托盘 /// </summary> private void qPan_OpenFromTuoPan() { this.Visibility = Visibility.Visible; this.ShowInTaskbar = true; this.WindowState = WindowState.Maximized; //FinanceContext.MainWindow.WindowState = WindowState.Maximized; } } }
<UserControl x:Class="WpfApplication1.jiaocheng" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition/> <RowDefinition/> <RowDefinition/> </Grid.RowDefinitions> <Grid Background="Gray" Opacity="0.2"> <Button Width="20" Height="20" HorizontalAlignment="Right" VerticalAlignment="Top" Content="X" Click="Button_Click"/> </Grid> <Grid Grid.Row="1" Opacity="0"> </Grid> <Grid Grid.Row="2" Background="Gray" Opacity="0.2"> </Grid> </Grid> </UserControl>
using System; using System.Collections.Generic; using System.Linq; 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.Shapes; namespace WpfApplication1 { /// <summary> /// jiaocheng.xaml 的交互逻辑 /// </summary> public partial class jiaocheng : UserControl { public jiaocheng() { InitializeComponent(); } private void Button_Click(object sender, RoutedEventArgs e) { this.Width = 0; this.Height = 0; } } }
记录一下,以供自己学习。