WPF的.net6
.net6的话 在csproj文件的 <PropertyGroup>里面加上: <UseWindowsForms>true</UseWindowsForms>
string[] win_item = new string[] { "最小化到任务栏", "显示在托盘", "关闭窗口", "浮动/固定 组件列表", "浮动/固定 组件属性", "打开/关闭 编译配置 窗口", "打开/关闭 \"安装文件配置 窗口", "打开/关闭 辅助配置选项 窗口" };
string[] win_img = new string[] { "min_16x.png", "tray_16x.png", "close_16x.png", "pinned_16x.png", "pinned_16x.png", "oc_16x.png", "oc_16x.png", "oc_16x.png" };
string[] win_tag = new string[] { "MIN","TRAY", "CLOSE", "F_ELEMENT", "F_ATTRIBUTE", "OC_CONFIG", "OC_FILE", "OC_AUXLI" };
win_menu = new ContextMenu();
for (int i = 0; i < win_item.Length; i++)
{
MenuItem menuItem = new()
{
Header = win_item[i],
Icon = new Image { Source = new BitmapImage(new Uri("pack://application:,,,/Images/" + win_img[i])) },
Tag = win_tag[i]
};
if (i == 3 || i==5)
{
Separator separator = new Separator();
win_menu.Items.Add(separator);
}
menuItem.Click += MenuItem_Click;
win_menu.Items.Add(menuItem);
}
// 在其他窗口设置主窗口的方法 主窗口加上
/*
public System.Windows.Forms.NotifyIcon notifyIcon;
。。。。。。
this.notifyIcon = new System.Windows.Forms.NotifyIcon();
this.notifyIcon.Text = "hahahah-xxxxxx ";
this.notifyIcon.Icon = System.Drawing.Icon.ExtractAssociatedIcon(System.Windows.Forms.Application.ExecutablePath);
this.notifyIcon.Visible = false;
this.notifyIcon.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler((o, e) =>
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
this.ShowInTaskbar = true;
this.notifyIcon.Visible = false;
this.WindowState = WindowState.Normal;
this.Activate();
}
});
*/
private static void MenuItem_Click(object sender, RoutedEventArgs e)
{
var pL = (MenuItem)sender;
// 直接调用主窗口
var _mainWindow = Application.Current.Windows.Cast<Window>().FirstOrDefault(window => window is MainWindow) as MainWindow;
switch (pL.Tag)
{
case "TRAY":
_mainWindow.WindowState = WindowState.Minimized;
_mainWindow.ShowInTaskbar = false;
_mainWindow.notifyIcon.Visible = true;
break;
有一段时间没搞winform,忘记了,估计差不多吧
[此贴子已经被作者于2023-4-17 15:42编辑过]