簡體   English   中英

單擊關閉按鈕時隱藏表單而不是關閉

[英]Hide form instead of closing when close button clicked

當用戶單擊表單上的X按鈕時,如何隱藏它而不是關閉它?

我在FormClosing嘗試過this.hide()但它仍然關閉了表單。

像這樣:

private void MyForm_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing) 
    {
        e.Cancel = true;
        Hide();
    }
}

(通過Tim Huffman

我在之前的回答中評論過,但我想我會提供自己的答案。 根據您的問題,此代碼與最佳答案類似,但添加了另一個提及的功能:

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    if (e.CloseReason == CloseReason.UserClosing) 
    {
        e.Cancel = true;
        Hide();
    }
}

如果用戶只是在窗口中點擊X ,則表單會隱藏; 如果還有其他任何內容,例如任務管理器, Application.Exit()或Windows關閉,則表單將正確關閉,因為將執行return語句。

來自MSDN

要取消窗體的關閉,請將傳遞給事件處理程序的FormClosingEventArgsCancel屬性設置為true

所以取消然后隱藏。

如果你想使用show / hide方法,我實際上是自己做了一個菜單結構我最近做過的游戲...這就是我做的方法:

創建一個按鈕以及您想要做的事情,例如“下一步”按鈕,並將以下代碼與您的程序相匹配。 對於此示例中的下一個按鈕,代碼將是:

btnNext.Enabled = true; //This enabled the button obviously
this.Hide(); //Here is where the hiding of the form will happen when the button is clicked
Form newForm = new newForm(); //This creates a new instance object for the new form
CurrentForm.Hide(); //This hides the current form where you placed the button.

這是我在游戲中使用的代碼片段,以幫助您理解我要解釋的內容:

    private void btnInst_Click(object sender, EventArgs e) 
    {
        btnInst.Enabled = true; //Enables the button to work
        this.Hide(); // Hides the current form
        Form Instructions = new Instructions(); //Instantiates a new instance form object 
        Instructions.Show(); //Shows the instance form created above
    }

所以有一個顯示/隱藏方法幾行代碼,而不是為這么簡單的任務做一大堆代碼。 我希望這有助於解決您的問題。

請注意,在執行此操作(已發布多個答案)時,您還需要找到一種方法來允許用戶在他們真正想要時關閉表單。 如果用戶在應用程序運行時嘗試關閉計算機,這確實會成為問題,因為(至少在某些操作系統上)這將阻止操作系統正常或有效地關閉。

我解決這個問題的方法是檢查堆棧跟蹤 - 當用戶嘗試單擊X vs系統嘗試結束應用程序以准備關閉時,存在差異。

private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
    StackTrace trace = new StackTrace();
    StackFrame frame;
    bool bFoundExitCommand = false;
    for (int i = 0; i < trace.FrameCount; i++)
    {
        frame = trace.GetFrame(i);
        string methodName = frame.GetMethod().Name;
        if (methodName == "miExit_Click")
        {
            bFoundExitCommand = true;
            Log("FormClosing: Found Exit Command ({0}) - will allow exit", LogUtilityLevel.Debug3, methodName);
        }
        if (methodName == "PeekMessage")
        {
            bFoundExitCommand = true;
            Log("FormClosing: Found System Shutdown ({0}) - will allow exit", LogUtilityLevel.Debug3, methodName);
        }
        Log("FormClosing: frame.GetMethod().Name = {0}", LogUtilityLevel.Debug4, methodName);
    }
    if (!bFoundExitCommand)
    {
        e.Cancel = true;
        this.Visible = false;
    }
    else
    {
        this.Visible = false;
    }
}

這是Modal表單的行為。 當您使用form.ShowDialog()要求此行為。 原因是form.ShowDialog在窗體被隱藏或銷毀之前不會返回。 因此,當窗體被隱藏時,窗體內的泵.ShowDialog會將其銷毀,以便它可以返回。

如果要顯示和隱藏表單,那么您應該使用無模式對話框模型http://msdn.microsoft.com/en-us/library/39wcs2dh(VS.80).aspx

form.Show()立即返回,你可以顯示和隱藏你想要的所有窗口,在你明確銷毀之前它不會被銷毀。

當您使用非模態形式的子模式時,您還需要在循環中使用Application.RunApplication.DoEvents運行消息泵。 如果創建表單的線程退出,則表單將被銷毀。 如果該線程沒有運行泵,那么它擁有的表單將沒有響應。

編輯:這聽起來像ApplicationContext旨在解決的問題。 http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext.aspx

基本上,您從ApplicationContext派生一個類,將ApplicationContext的一個實例作為參數傳遞給Application.Run()

// Create the MyApplicationContext, that derives from ApplicationContext,
// that manages when the application should exit.

MyApplicationContext context = new MyApplicationContext();

// Run the application with the specific context. 
Application.Run(context);

您的應用程序上下文需要知道何時可以退出應用程序,並且隱藏表單時不應退出應用程序。 什么時候應用程序退出。 您的應用程序上下文或表單可以調用應用程序上下文的ExitThread()方法來終止消息循環。 此時Application.Run()將返回。

如果不了解更多關於表單的層次結構以及決定何時隱藏表單以及何時退出的規則,則不可能更具體。

根據其他響應,您可以將其放在表單代碼中:

    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        base.OnFormClosing(e);
        if (e.CloseReason == CloseReason.UserClosing)
        {
            e.Cancel = true;
            Hide();
        }
    }

首選覆蓋: MSDN “OnFormClosing方法還允許派生類在不附加委托的情況下處理事件。這是在派生類中處理事件的首選技術。”

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM