代码之家  ›  专栏  ›  技术社区  ›  Victor Bjelkholm

无退出申请关闭窗体

  •  6
  • Victor Bjelkholm  · 技术社区  · 15 年前

    我目前正在做一个小项目,希望得到一些帮助。

    我有两个窗体,第一个是登录窗口,第二个是主程序。我的问题是当我关门的时候 form1 具有 this.Close() 它正在退出整个程序。

    我有一种感觉,我需要使用线程或类似的东西,但我找不到合适的资源来解决我的问题。

    谢谢。

    6 回复  |  直到 14 年前
        1
  •  7
  •   user159088 user159088    15 年前

    您可以隐藏第一个窗体而不是关闭它:

    this.Hide();
    Form2 form2 = new Form2();
    form2.Show();
    
        2
  •  3
  •   Blam    15 年前

    你不能改变program.cs,让它运行主窗体吗?在主窗体启动时,它创建并显示一个登录窗体,然后隐藏自己(等待登录出现)?

        3
  •  2
  •   Reed Copsey    15 年前

    Application.MainWindow 在关闭您的登录窗体之前,转到您的第二个“主”窗口。

        4
  •  2
  •   Mike Webb    15 年前

    Program.cs是您的主要功能所在。如果您使用visualstudio将项目创建为Windows应用程序,那么main中将有一个函数运行启动程序时打开的窗体。只需从main中的登录窗体获取登录信息,然后调用第二个窗口。举个例子:

    [STAThread]
    private static void Main(string[] args)
    {
        //there's some other code here that initializes the program
    
        //Starts the first form (login form)
        Application.Run(new form1());
    
        //Get the login info here somehow. Maybe save in public members of form1 or
        // in a global utilities or global user class of some kind
    
        //Run the main program
        Application.Run(new mainProgramForm());
    }
    

    编辑:忘了什么

    private static void Main(string[] args)
    {
        //there's some other code here that initializes the program
    
        //Instead of running a new form here, first create it and then run it
        Form1 form1 = new Form1();    //Creates the form
        Application.Run(form1);       //Runs the form. Program.cs will continue after the form closes
    
        //Get the login info
        string username = form1.Username;
        string password = form1.Password;
    
        //Get rid of form1 if you choose
        form1.Dispose();
    
        //Validate the user info
    
        //Run the main program
        Application.Run(new mainProgramForm());
    }
    
        5
  •  0
  •   bluish dmajkic    14 年前

    Open Program.cs-在调用应用程序运行之前,您可以做很多事情,包括显示您的登录信息。这是我的一个项目的一个例子。它试图找到数据库连接。如果不能,它将打开一个向导并连接到access或mssql。如果打开良好,它会显示一个spalsh屏幕并最终运行应用程序,否则它会关闭。

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    DialogResult LclResult;
    
    EESDatabasePersistentData LclPersist;
    LclPersist = new EESDatabasePersistentData();
    string LclDataType;
    string LclDatabase;
    string LclServer;
    string Password;
    string UserID;
    if (!LclPersist.GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password))
    {
            // Run the connection wizard
            GetConnection(out LclServer, out LclDatabase, out LclDataType, out UserID, out Password);
    }
    
    
    if (LclDataType == "ACCESS")
            InitDataAccess(LclDatabase);
    else if (LclDataType == "SQLSERVER")
            InitDataSQL(LclServer, LclDatabase, UserID, Password);
    if (OpenGood)
    {
            if (!System.Diagnostics.Debugger.IsAttached)
            {
                    FormSplash.Instance.SetupVersion();
                    ///////////////////////////////////
                    // If we don't call do events 
                    // splash delays loading.
                    Application.DoEvents();
                    FormSplash.Instance.Show();
            }
            Application.DoEvents();
    
            Application.Run(new FormMentorMain());
    }
    else
            Application.Exit();
    
        6
  •  0
  •   Basheer AL-MOMANI    9 年前

    tray Icon

    它被称为托盘图标

    close all forms 以及 app still running user can go back to app any time

    让我们开始编码(注意这是WPF应用程序)

    private NotifyIcon m_notifyIcon;
    private ContextMenuStrip m_contextMenu;
    private bool _ForceClose;
    
    public MainWindow()
    {
         InitializeComponent();
         CreateNotifyIcon();
    
    }
    
    private void CreateNotifyIcon()
    {
        m_contextMenu = new ContextMenuStrip();
    
        ToolStripMenuItem mI1 = new ToolStripMenuItem { Text = "Open" };
        mI1.Click += (sender, args) => Maximize();
        ToolStripMenuItem mI2 = new ToolStripMenuItem { Text = "Exit" };
        mI2.Click += (sender, args) => EndApplication();
        m_contextMenu.Items.Add(mI1);
        m_contextMenu.Items.Add(mI2);
        //Initalize Notify Icon
        m_notifyIcon = new NotifyIcon
        {
            Text = "Application Title",
            Icon = new Icon("Icon.ico"),
            //Associate the contextmenustrip with notify icon
            ContextMenuStrip = m_contextMenu,
            Visible = true
        };
    }
    

    protected void Minimize()
    {
        Hide();
    }
    
    protected void Maximize()
    {
        Show();
        this.WindowState =WindowState.Normal;
    }
    
    protected void EndApplication()
    {
        Minimize();
        Thread.Sleep(1000);
        _ForceClose = true;
        WindowState = WindowState.Normal;
        this.Close();
        Environment.Exit(0);
    }
    

    而且在 Window Closing 事件侦听器(不要忘记添加它)

    private void Window_Closing(object sender, CancelEventArgs e)
    {
        if (_ForceClose == false)
        {
            e.Cancel = true;
            Minimize();
        }
    }