只有一个主窗体的程序.在托盘点击时,判断主窗体是否为活动窗体(在桌面的最上面),如果在则让窗体最小化;如果不在,则把它激活.显示在上面?????
还是这个问题,怎么搞

解决方案 »

  1.   

    你可以定义一个全局bool值变量,在最小化时给它设置成 true,最大化设置成 false 
    最后在托盘点击事件里判断就好了
      

  2.   

    可以试试(我没有测试过)
    用Form.ContainsFocus来判断主窗体是否为活动窗体,
    用Form.Activate()来激活主窗体,比如如下的伪代码:
    void On_NotifyIcon_Click(object sender, EventArgv e)
    {
      if( !mainForm.ContainsFocus() )
      {
         mainForm.Activate();
      }
      else
      {
        mainForm.WindowState = FormWindowState.Minimized;
      }
    }
      

  3.   

    mainForm.ContainsFocus 这个是属性,不是方法
      

  4.   

    需要在窗体中增加一个NotifyIcon控件,并给控件指定图片。为NotifyIcon添加上下文菜单或单击或双击事件。
    事件处理程序调用下面的代码。/// <summary>
    /// 显示隐藏窗口
    /// </summary>
    /// <param name="showWindow"></param>
    private void ShowHideWindow(bool showWindow)
    {
    if(showWindow == true)//显示
    {
    if(this.ShowInTaskbar==false)
    {
    this.ShowInTaskbar = true;
    this.Visible = true;
    //this.Show();
    }
    if(this.WindowState == FormWindowState.Minimized)
    {
    this.WindowState = FormWindowState.Normal;
    }
    this.Activate();
    }
    else//隐藏
    {
    if(this.WindowState == FormWindowState.Minimized)
    {
    this.WindowState = FormWindowState.Normal;
    }
    if(this.ShowInTaskbar == true)
    {
    this.ShowInTaskbar = false;
    this.Visible = false;
    //this.Hide();

    }
    }
    }
      

  5.   


            private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
            {
                if (this.WindowState != FormWindowState.Minimized)
                {
                    if (this.TopMost) this.WindowState = FormWindowState.Minimized;
                    else this.TopMost = true;
                }
                else
                {
                    this.WindowState = FormWindowState.Normal;
                    this.TopMost = true;
                }
            }
      

  6.   

    要判断windowstate ,12行代码
            private void 关于我们ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                MessageBox.Show("关于我们");
            }        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
            {
                Application.Exit();
            }        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
            {
                if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
                {
                    显示隐藏主界面ToolStripMenuItem.Text = "显示主界面";
                }
                else
                {
                    显示隐藏主界面ToolStripMenuItem.Text = "隐藏主界面";
                }
            }        private void 显示隐藏主界面ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if (this.WindowState == System.Windows.Forms.FormWindowState.Minimized)
                {
                    this.WindowState = System.Windows.Forms.FormWindowState.Normal;
                }
                else
                {
                    this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
                }
            }        private void 设置ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                //if (this.Visible)
                //{
                //    this.Visible = false;
                //}
                //else
                //{
                //    this.Visible = true;
                //}
                MessageBox.Show("设置");
            }        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
            {
                MessageBox.Show("帮助");
            }
      

  7.   

    我是楼主,其实我是想通过这个问题解决我遇见的一个类似的问,我的程序运行,加载主窗体时,我做了个等待画面,等待画面结束后打开主窗体,但是主窗体取在桌面的最后面,windows资源管理器盖住了,试了好多方法都不行,最后用API函数搞定了
    [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
            public static extern int SetWindowPos(int hWnd, int hWndInsertAfter, int x, int y, int cx, int cy, int wFlags);
            public const int HWND_TOPMOST = -1;
            public const int SWP_SHOWWINDOW = 40;   
    这个API是把窗体放到windows桌面的最上面,虽然解决了,但是我不知道出现这种情况是为什么,必须调用API解决该问题吗??
    请大家帮忙分析一下,谢谢!!!!
      

  8.   

    这种情况调用API应该是最有效的吧。你的等待画面是怎么做的?怎么结束的?有没有试过在结束时设置主窗体的焦点呢?
      

  9.   

    设置窗体actived,窗体图标在任务栏上一闪一闪的,没用,我说过了,我试过这些方法了,另外设置topmost=true,该窗体一直在前,我要的不是这种结果
      

  10.   

    使用Windows API吧
    1,设置窗体显示在最前
       [DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
       public static extern bool SetForegroundWindow(IntPtr hWnd);
    2,获得显示在最前的窗体
       [DllImport("user32.dll")]
       public static extern IntPtr GetForegroundWindow();
       将取得的窗口句柄和当前的句柄比较一下就知道是不是当前窗体在最前了
      

  11.   

                            this.notifyIcon1.Visible =true;
    this.Visible = true;你把窗体或者托盘隐藏就行了。在加些判断条件
      

  12.   

    试过这两个函数,不行,SetWindowPos这个函数可以,但是FORM一直在前面,我不要这种效果
      

  13.   

    打开VS后运行exe可以,关闭VS后运行exe又跑后面啦,奇怪啦