+ -
当前位置:首页 → 问答吧 → 为什么不能实现在托盘显示程序图标?

为什么不能实现在托盘显示程序图标?

时间:2011-12-07

来源:互联网

C# code
using System;
using System.Resources;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 考试系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnLogon_Click(object sender, EventArgs e)
        {
            if (txtUserName.Text == "admin" && txtPassword.Text == "123")
            {
                MessageBox.Show("登陆成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Form2  fs2= new Form2();
                fs2.Show();
            }
            else
                MessageBox.Show("登录失败", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);

        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (this.WindowState==FormWindowState.Minimized)
            {
                this.notifyIcon1.Visible=true;
                this.ShowInTaskbar = true;
                this.notifyIcon1.Visible = false;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            notifyIcon1.Visible = false;
        }

        private void Form1_MinimumSizeChanged(object sender, EventArgs e)
        {
                this.Hide();
                this.ShowInTaskbar = false;
                this.notifyIcon1.Visible = true;

        }


    }
}



这个代码中 为什么不能实现程序最小化到托盘,而不在任务栏显示;当点击托盘图标,程序重新恢复任务栏显示?

作者: ds2009w   发布时间: 2011-12-07

if (this.WindowState==FormWindowState.Minimized)
  {
  this.notifyIcon1.Visible=true;
  this.ShowInTaskbar = true;
  this.notifyIcon1.Visible = false;
  }


  // this.notifyIcon1.Visible = false;这句去掉

作者: Tsapi   发布时间: 2011-12-07

Visible是false, 当然看不到。。
前面已经设置为true了,为什么又设置成false

作者: mizuho_2006   发布时间: 2011-12-07

引用楼主 ds2009w 的回复:
▪ 云计算IDC服务都包括什么?▪ IT部门如何成为云服务提供商?▪ 云计算能让商业交易变得更透明...▪ 相对于公共PaaS,私人PaaS有哪...▪ 云计算能否简化企业项目管理?C# code
using System;
using System.Resources;
using System.Collections.Generic;
using System.ComponentModel;……


这个也是不行的。。你帮我写个示例吧。。
就是当程序运行时,托盘处无图标,任务栏有图标;
当程序最小化时,托盘处有图标,任务栏没有图标。

作者: ds2009w   发布时间: 2011-12-07

引用 1 楼 tsapi 的回复:

if (this.WindowState==FormWindowState.Minimized)
{
this.notifyIcon1.Visible=true;
this.ShowInTaskbar = true;
this.notifyIcon1.Visib……


我的想法是这样的:


当程序运行时,托盘处无图标,任务栏有图标;
当程序最小化时,托盘处有图标,任务栏没有图标。

作者: ds2009w   发布时间: 2011-12-07

C# code

 private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.IsDisposed || this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                this.notifyIcon1.Visible = true;

            }
            else
            {
                this.ShowIcon = true;
                this.notifyIcon1.Visible = false;

            }
        }


作者: DENQH   发布时间: 2011-12-07

C# code

 private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.IsDisposed || this.WindowState == FormWindowState.Minimized)
            {
                this.ShowInTaskbar = false;
                this.notifyIcon1.Visible = true;

            }
            else
            {
                this.ShowInTaskbar = true;
                this.notifyIcon1.Visible = false;

            }
        }

作者: DENQH   发布时间: 2011-12-07

//我想你要的是这个效果,试一下吧。  
  private void Form1_Load(object sender, EventArgs e)
  {
  notifyIcon1.Visible = false;
  }
 
  private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
  {
  if (this.WindowState == FormWindowState.Normal)
  {
  this.notifyIcon1.Visible = true;
  this.ShowInTaskbar = false;
  this.Hide();
  this.WindowState = FormWindowState.Minimized;

  }
  }
  private void Form1_Resize(object sender, EventArgs e)
  {
  if (this.WindowState == FormWindowState.Minimized)
  {
  this.ShowInTaskbar = false ;
  this.notifyIcon1.Visible = true;
  }

  } 
}

作者: Tsapi   发布时间: 2011-12-07

引用 5 楼 denqh 的回复:

C# code

private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.IsDisposed || this.WindowState == FormWindowState.Minimized)
{
this.Sh……


可不可以把你的整个窗体代码贴一下呢

作者: ds2009w   发布时间: 2011-12-07