小弟最近在写一个程序,需要将程序设置成点击form1里一个button,打开一个新窗体form2,使得form2只能通过form1控制关闭,不能用右上角的关闭按钮关闭,即设置form2右上角的关闭按钮为灰色不可用,请问这需要怎么写?谢谢~

解决方案 »

  1.   

    如下可以么?
     Form2 f2 = new Form2();
                    f2.ControlBox = false;
                    f2.Show();
      

  2.   

    不一定非得设置为false,可以隐藏或者在关闭事件中动作设为取消
      

  3.   

    如果仅仅是关闭按钮变为灰色不可用,那么如下操作[DllImport("USER32.DLL")]
                public static extern int GetSystemMenu(int hwnd, int bRevert);
                [DllImport("USER32.DLL")]
                public static extern int RemoveMenu(int hMenu, int nPosition, int wFlags);
                const int MF_REMOVE = 0x1000;
                const int SC_RESTORE = 0xF120; //还原 
                const int SC_MOVE = 0xF010; //移动 
                const int SC_SIZE = 0xF000; //大小 
                const int SC_MINIMIZE = 0xF020; //最小化 
                const int SC_MAXIMIZE = 0xF030; //最大化 
                const int SC_CLOSE = 0xF060; //关闭
                private void button1_Click(object sender, EventArgs e)
                {
                    Form2 f2 = new Form2();
                    int hMenu = GetSystemMenu(f2.Handle.ToInt32(), 0);
                    RemoveMenu(hMenu, SC_CLOSE, MF_REMOVE);
                    f2.Show();
                }
      

  4.   

    在 formclosing 事件里,将关闭动作取消
      

  5.   

    将form2的FormBorderStyle设为none看,是不是你想要的