注:案例是用WPF写的,在winfrom 和 uwp 中应该也可以实现。
1.效果截图:
2.新建一个WPF工程窗口。
3.双击MainWindow.xaml,设计窗口。
使用 ResizeMode="CanMinimize" 属性值,禁止窗口最大化,这里可以不禁用,看具体的需求。
<Window x:Class="WpfApplication1.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
ResizeMode="CanMinimize"
Title="MainWindow" Height="200" Width="750">
<Grid>
<Label Content="My Managed window" FontWeight="Bold" Foreground="Blue" FontSize="20px" Margin="250,60"></Label>
</Grid>
</Window>
4.给项目找个好看的图标 .ico 和 .png 格式。
①新建资源文件,将上面两张图分别添加至资源文件当中。
②分别更改图标资源属性。
③更改窗口运行时标题栏和任务栏上的图标;打开窗体属性,选择 .ico 图标。
④更改程序exe图标,选择资源文件中的图标。
⑤添加托管图标 并且 在图标上添加 双击事件,打开事件和 退出事件;另外添加单例窗口;代码如下:
注:MainWindow.xaml.cs 中主要实现托管图标,App.xaml.cs 中主要实现单例窗口。
MainWindow.xaml.cs 代码:
using System;
using System.IO;
using System.Windows;
using System.Windows.Forms;
namespace WpfApplication1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
private NotifyIcon notifyIcon = null;
public MainWindow()
{
InitializeComponent();
//窗口显示屏幕中间
WindowStartupLocation = WindowStartupLocation.CenterScreen;
//重写关闭事件
Closing += Window_Closing;
//托管图标
notifyIcon = new NotifyIcon
{
BalloonTipText = "My Managed Window",
Text = "My Managed Window",
Visible = true
};
Stream iconStream = System.Windows.Application.GetResourceStream(new Uri("pack://application:,,,/WpfApplication1;component/Resources/Managed.ico")).Stream;
notifyIcon.Icon = new System.Drawing.Icon(iconStream);
notifyIcon.DoubleClick += NotifyIcon_DoubleClick;
System.Windows.Forms.MenuItem open = new System.Windows.Forms.MenuItem("打开");
open.Click += Open_Click;
System.Windows.Forms.MenuItem close = new System.Windows.Forms.MenuItem("退出");
close.Click += Close_Click;
System.Windows.Forms.MenuItem[] m = new System.Windows.Forms.MenuItem[] { open, close };
notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(m);
}
/// <summary>
/// 托管图标,打开事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Open_Click(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
Show();
WindowState = WindowState.Normal;
ShowInTaskbar = true;
}
else
{
IntPtr hWnd = App.FindWindow(null, "My Managed Window");
App.ShowWindow(hWnd, 9);
}
}
/// <summary>
/// 托管图标,退出事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void Close_Click(object sender, EventArgs e)
{
if (System.Windows.MessageBox.Show("是否确认退出?", "My Managed Window", MessageBoxButton.YesNo, MessageBoxImage.Question, MessageBoxResult.No) == MessageBoxResult.Yes)
{
Close();
notifyIcon.Dispose();
Environment.Exit(0);
}
}
/// <summary>
/// 托管图标,双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void NotifyIcon_DoubleClick(object sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
Show();
WindowState = WindowState.Normal;
ShowInTaskbar = true;
}
else
{
IntPtr hWnd = App.FindWindow(null, "My Managed Window");
App.ShowWindow(hWnd, 9);
}
}
/// <summary>
/// 窗口关闭事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
IntPtr hWnd = App.FindWindow(null, "My Managed Window");
App.ShowWindow(hWnd, 0);
e.Cancel = true;
}
}
}
App.xaml.cs 代码:
using System;
using System.Runtime.InteropServices;
using System.Windows;
namespace WpfApplication1
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
[DllImport("USER32.DLL", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string classname, string captionName);
[DllImport("USER32.DLL")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("USER32.DLL")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool SetForegroundWindow(IntPtr hWnd);
public System.Threading.Mutex mutex;
public App()
{
this.Startup += new StartupEventHandler(App_Startup);
}
/// <summary>
/// 单例窗口,防止用户打开多个窗口
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void App_Startup(object sender, StartupEventArgs e)
{
try
{
bool ret;
mutex = new System.Threading.Mutex(true, "My Managed Window", out ret);
if (!ret)
{
IntPtr hWnd = FindWindow(null, "My Managed Window");
ShowWindow(hWnd, 9);
SetForegroundWindow(hWnd);
Environment.Exit(0);
}
}
catch (Exception /*ex*/)
{
Environment.Exit(0);
}
}
}
}