| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 861 人关注过本帖
标题:c#项目中怎么把提示放到桌面托盘中
只看楼主 加入收藏
ggg22
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2023-4-16
结帖率:0
收藏
已结贴  问题点数:20 回复次数:2 
c#项目中怎么把提示放到桌面托盘中
有没有大佬教一下
搜索更多相关主题的帖子: 桌面 c# 项目 托盘 提示 
2023-04-16 19:30
qq2889577966
Rank: 4
等 级:业余侠客
威 望:5
帖 子:66
专家分:277
注 册:2021-4-14
收藏
得分:10 
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编辑过]

收到的鲜花
  • 龙胆草2023-04-21 07:46 送鲜花  1朵   附言:感谢分享!
2023-04-17 08:50
东海ECS
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:Python
等 级:版主
威 望:32
帖 子:412
专家分:1646
注 册:2023-1-24
收藏
得分:10 
在使用C#开发桌面应用程序时,可以使用System.Windows.Forms.NotifyIcon类将通知图标添加到系统托盘中,从而在桌面上显示通知消息。

以下是一个简单的示例代码。首先,在需要添加通知的窗体上添加一个NotifyIcon控件,并设置其图标等属性。

程序代码:
private System.Windows.Forms.NotifyIcon notifyIcon;

private void InitializeComponent()
{
    this.notifyIcon = new System.Windows.Forms.NotifyIcon(this.components);
    this.notifyIcon.BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info;
    this.notifyIcon.Icon = new System.Drawing.Icon("icon.ico");
    this.notifyIcon.Text = "提示信息";
    this.notifyIcon.Visible = true;
    this.notifyIcon.Click += new System.EventHandler(this.notifyIcon_Click);
}

然后,在需要显示通知的时候,可以使用NotifyIcon类的ShowBalloonTip方法来显示通知消息。

private void ShowNotification()
{
    notifyIcon.ShowBalloonTip(5000, "通知", "您有新的消息", ToolTipIcon.Info);
}

这将在系统托盘中显示一个通知,等待5秒钟后自动消失。当用户点击通知时,可以在notifyIcon_Click事件处理程序中执行相应操作。

private void notifyIcon_Click(object sender, EventArgs e)
{
    // 执行相应操作
}

请注意,如果通知图标在任务栏中隐藏,则用户可能不会立即注意到通知。因此,建议在显示通知时播放一段声音或者使用其他方式来吸引用户的注意力。



收到的鲜花
  • 龙胆草2023-04-21 07:30 送鲜花  1朵   附言:谢谢解答,向您学习了!

会当凌绝顶,一览众山小.
2023-04-19 19:27
快速回复:c#项目中怎么把提示放到桌面托盘中
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023116 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved