版权声明:biubiubiu https://blog.csdn.net/a_lllll/article/details/88909928
1.在XAML中window下添加菜单
<Window.Resources>
<ContextMenu x:Key="NotifyIconMenu" StaysOpen="False">
<MenuItem Header="在线"/>
<MenuItem Header="离线"/>
<MenuItem Header="隐身"/>
<MenuItem Header="忙碌"/>
<MenuItem Header="离开"/>
<MenuItem Header="关于我们"/>
<MenuItem Header="还原" Name="MenuBlack" Click="MenuBlack_Click"/>
<MenuItem Header="退出QQ" Name="MenuExit" Click="MenuExit_Click"/>
</ContextMenu>
</Window.Resources>
2.在XAML.CS中添加如下代码
System.Windows.Forms.NotifyIcon notifyIcon = null;
public MainWindow()
{
//无任务栏按钮
this.ShowInTaskbar = false;
this.notifyIcon = new System.Windows.Forms.NotifyIcon();
this.notifyIcon.Icon = new Icon("../../Img/Log/QQ.ico");
this.notifyIcon.Visible = true;
//小图标文本提示
this.notifyIcon.BalloonTipText = "QQ";
this.notifyIcon.Text = "QQ";
notifyIcon.MouseClick += NotifyIcon_MouseClick;
}
/// <summary>
/// 小图标鼠标的点击事件
/// </summary>
private void NotifyIcon_MouseClick(object sender, System.Windows.Forms.MouseEventArgs e)
{
//判断鼠标右键点击
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{
//控件弹出菜单来自于NotifyIconMenu
ContextMenu NotifyIconMenu = (ContextMenu)this.FindResource("NotifyIconMenu");
NotifyIconMenu.IsOpen = true;
}
}
//还原窗口
private void MenuBlack_Click(object sender, RoutedEventArgs e)
{
this.WindowState = WindowState.Normal;
}
//退出窗口
private void MenuExit_Click(object sender, RoutedEventArgs e)
{
//结束所有线程即当前这个进程
Environment.Exit(0);
}