When you click the close button in winfrom, you are prompted to disable or minimize the implementation.

Source: Internet
Author: User

When we close QQ, a prompt will pop up asking if we want to minimize or exit.ProgramRun. This is a user-friendly prompt operation method. This articleArticleIt is to simulate the effect of QQ, which is implemented in winform.

1. First create a winform Project

2. Open the form1 form design and set the delegation of The formclosing event in the form1 window.

In this method, you need to close the prompts and process them according to your selection.

3. Create a form with the close prompt: closetipform

You can minimize the number to the system tray area or exit the program.

You can also record the user's choice. When you close the service next time, you will not be prompted to directly perform the corresponding operations.

 

The implementation of minimizing to the system tray area is not discussed in this article. I have already introduced a lot on the Internet. Please refer:

Minimize winform search to System Tray

 

When you click OK, You need to record the user's selection. Here we record the user's selection to settings:

Alwayscloseaction is used to permanently record user selection;

Closeaction is used to save the user's selection.

Let's take a look at how the operation is handled when you click OK:

A clickclosebuttonaction class is defined here:

View Source Code
Print?

/// <Summary>
/// Optional action of the close button
/// </Summary>
Public class clickclosebuttonaction
{
Public static int tip = 0;
Public static int min = 1;
Public static int close = 2;
Public static int cancel = 3;
}

 

There are several options for disabling: prompt, minimize, close, and cancel.

4. Write the close reminder Program

After creating a closetipform, you can write related programs for the form of the application closing prompt. Here, you can write them in the formclosing principal method created in the front.

View Source Code
Print?

Private void form=formclosing (Object sender, formclosingeventargs E)
{
// First, read from the default settings. If none of them are used, open the prompt window.
Settings r = new settings ();
If (R. alwayscloseaction = clickclosebuttonaction. min) // minimize
{
E. Cancel = true;
 
Setwindowmin ();
}
Else if (R. alwayscloseaction = clickclosebuttonaction. Close) // close
{
}
Else if (R. alwayscloseaction = clickclosebuttonaction. Tip) // prompt
{
Closetipform form = new closetipform ();
Form. showdialog ();
 
R. Reload ();
If (R. closeaction = clickclosebuttonaction. Cancel) // cancel
{
E. Cancel = true;
}
Else if (R. closeaction = clickclosebuttonaction. min) // minimize
{
E. Cancel = true;
 
Setwindowmin ();
}
Else if (R. closeaction = clickclosebuttonaction. Close) // close
{
}
}
}

 

CodeThere is a setwindowmin method in it, which is customized to minimize the window.

Because there is no direct minimization method, use:

This. windowstate = formwindowstate. minimized

Sometimes the problem cannot be solved well, so a Windows API is called for implementation.

5. Running Effect

After completing the above processing, you can basically run it now.

OK. This article is here, and some details are not introduced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.