日期:2014-05-18  浏览次数:20804 次

托盘点击4下才显示???
winform的超级菜鸟,请教一下,
从网上抄的代码,托盘后,2下2下点击,总点击4次后托盘才显示窗口,是不是代码问题,谢谢
C# code

        private void Form1_Load(object sender, EventArgs e)
        {
            // 任务栏显示小图标
            this.notifyIcon1.ShowBalloonTip(1, "系统提示", "程序正在运行~~~", ToolTipIcon.Info);
        }
        private void HideMainForm()
        {
            this.Hide();
        }

        private void ShowMainForm()
        {
            this.Show();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }
        private void ExitMainForm()
        {
            if (MessageBox.Show("您确定要退出化验数据接收程序吗?", "确认退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.OK)
            {
                this.notifyIcon1.Visible = false;
                this.Close();
                this.Dispose();
                Application.Exit();
            }
        }


        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            HideMainForm();
        }

        private void notifyIcon1_DoubleClick(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Normal)
            {
                this.WindowState = FormWindowState.Minimized;

                HideMainForm();

            }
            else if (this.WindowState == FormWindowState.Minimized)
            {
                //this.WindowState = FormWindowState.Normal;
                ShowMainForm();

            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideMainForm();
            }

        }

        private void 显示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ShowMainForm();

        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExitMainForm();

        }




------解决方案--------------------
你这段干嘛的:
C# code
        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                HideMainForm();
            }
        }

------解决方案--------------------
把notifyIcon1_DoubleClick这里的HideMainForm()去掉,
因为你double之后会触发sizechanged事件。change事件又调用了一次hide
------解决方案--------------------
加这句话 this.WindowState = FormWindowState.Minimized;
[code = c#]private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
this.WindowState = FormWindowState.Minimized;
HideMainForm();

}[/code]