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?
/// Optional action of the close button |
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 |
Else if (R. alwayscloseaction = clickclosebuttonaction. Close) // close |
Else if (R. alwayscloseaction = clickclosebuttonaction. Tip) // prompt |
Closetipform form = new closetipform (); |
If (R. closeaction = clickclosebuttonaction. Cancel) // cancel |
Else if (R. closeaction = clickclosebuttonaction. min) // minimize |
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.