Disable the main form without exiting the main program, and how to obtain information about the shutdown and logout of the Operating System

Source: Internet
Author: User

How to hide the form without exiting the main form by clicking the "X" buttonProgram? Let's first analyze the order of several events that the form responds:

1. When the current user clicks the form "close", the system message response sequence in the "X" icon in the upper right corner is:
1. The system intercepts users' click messages and can intercept them by reloading wndproc;
2. Trigger the formclosing event of the form;
3. Close the form and reclaim the resources occupied by the form;

2. When this. Close () is used in the program to close the form, the system response sequence is:
1. Trigger the formclosing event of the form;
2. Close the form and reclaim the resources occupied by the form;

3. When the user closes the operating system, the order of the form response is:
1. Trigger the formclosing event of the form;
2. Trigger the systemevents_sessionending event of the form;
3. Close the form and reclaim the resources occupied by the form;

Therefore, when we want to click the main form "X" icon, we do not want the program to exit, but only minimize and hide the form, it is best to overload wndproc and intercept the click button event, minimize the form to avoid exiting the main program.
Instead of simply adding E. Cancel = true to the formclosing event, the form (Program) cannot be automatically closed when the system is closed or logged out;

After the above analysis, we only need to add the following in the formCodeYou can:

 

Protected   Override   Void Wndproc ( Ref Message m)
{
Const   Int Wm_syscommand =   Zero X 0112 ;
Const   Int SC _close =   0xf060 ;
If (M. msg = Wm_syscommand && ( Int ) M. wparam = SC _close)
{
This . Windowstate = Formwindowstate. minimized;
This . Hide ();
MessageBox. Show ( " Click Close button " );
Return ;
}
Base . Wndproc ( Ref M );
}

4. Interception of system shutdown and cancellation Information

You only need to add the systemevents_sessionending event for the main form in the main () function of the program;

Form1 frmmain= NewForm1 ();
Systemevents. sessionending+ = NewSessionendingeventhandler (frmmain. systemevents_sessionending );

Add the following code in the form1 form to respond to shutdown, logout, or restart events:

Internal   Void Systemevents_sessionending ( Object Sender, sessionendingeventargs E)
{
String Exittype = E. Reason. tostring (). Trim (). toupper ();
MessageBox. Show (exittype );
String Msgtitle =   "" ;
String Msgquestion =   "" ;
If (Exittype. Contains ( " Shutdown " ))
{
Msgtitle =   " Shutdown " ;
Msgquestion =   " Are you sure you want to shut down? " ;
}
Else   If (Exittype. Contains ( " Logoff " ))
{
Msgtitle =   " Cancel " ;
Msgquestion =   " Are you sure you want to log out? " ;
}
Dialogresult result = MessageBox. Show (msgquestion, msgtitle, messageboxbuttons. yesno );
E. Cancel = (Result = Dialogresult. No );
}

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.