读书人

托盘再次打开窗口时出现异常

发布时间: 2012-01-10 21:26:51 作者: rapoo

托盘再次打开窗口时出现错误
我做了一个托盘程序,再打开程序时,能正确隐藏窗口。通过托盘打开窗口,点击窗口最小化,重新隐藏窗口,可再通过托盘显示时,窗口只能显示标题栏,这是怎么回事啊?
程序在下面:
public Form1()
{
InitializeComponent();
Initializenotifyicon();//使用托盘显示初始化
}
private void Initializenotifyicon()
{
this.MaximizeBox = false;
this.ShowInTaskbar = false;
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.Visible = false;

this.SizeChanged += new EventHandler(Form1_SizeChanged);

TrayIcon = new NotifyIcon();
TrayIcon.Icon = mNetTrayIcon;
TrayIcon.Text = "事件监听程序 " + "\n " + "版权:北京安图公司 ";
TrayIcon.Visible = true;
TrayIcon.Click += new System.EventHandler(this.OpenForm);

MenuItem[] mnuItms = new MenuItem[3];

mnuItms[0] = new MenuItem();
mnuItms[0].Text = "打开 ";
mnuItms[0].Click += new EventHandler(this.OpenForm);
mnuItms[0].DefaultItem = true;

mnuItms[1] = new MenuItem( "- ");

mnuItms[2] = new MenuItem();
mnuItms[2].Text = "退出系统 ";
mnuItms[2].Click += new EventHandler(this.CloseForm);

notifyiconMnu = new ContextMenu(mnuItms);


TrayIcon.ContextMenu = notifyiconMnu;
}

private void OpenForm(object sender, System.EventArgs e)
{
this.WindowState = System.Windows.Forms.FormWindowState.Normal;
this.Visible = true;
this.Show();
}

private void CloseForm(object sender, System.EventArgs e)
{
TrayIcon.Visible = false;
this.Close();
}

private void Form1_SizeChanged(object sender, EventArgs e)
{
if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
{
this.ShowInTaskbar = false;
this.Visible = false;
}
}


[解决办法]
给你参考一下,我封装在类库中的一段,有疑问加我的msn: zgoalh410@hotmail.com
====================================================================
/// <summary>
/// Occurs before the form is closed.
/// </summary>
/// <param name= "sender "> </param>
/// <param name= "e "> </param>
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
notifyIcnMainFrm.Visible = this.m_VisibleBalloonInTaskBar;

InitBalloonOfTaskBar();

if (!VisibleBalloonInTaskBar)
{
if (m_FormType == TypeOfForms.SubForm)
return;
else if (m_FormType == TypeOfForms.MainForm)
{
Application.Exit();
return;
}
}

//event while closing forms.
if (this.WindowState != FormWindowState.Minimized)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
this.Hide();
this.notifyIcnMainFrm.ShowBalloonTip(m_TipPeriod, m_strTipTitle, m_strTipText, ToolTipIcon.Info);


ResetTextImageOfContentTextMenu(m_conTxtType);
}
else //if (this.WindowState != FormWindowState.Maximized)
{
e.Cancel = false;
Application.Exit();
}

}

读书人网 >C#

热点推荐