Program The operating system can be shut down or restarted at this time so that if the program is not exited, the system cannot be shut down. How can we automatically exit the program when it is shut down? In fact, it is very simple. When the Windows operating system executes the close action, it will send a message wm_queryendsession to each running application, telling the application to shut down, if the returned message value is 1, Windows will automatically shut down. therefore, the program can be automatically exited by intercepting the wm_queryendsession message.
Code
1 /**/ /// <Summary>
2 /// Close the application when the operating system is disabled.
3 /// </Summary>
4 /// <Param name = "M"> Intercept System messages </Param>
5 Protected Override Void Wndproc ( Ref Message m)
6 {
7 Switch (M. msg)
8 {
9 Case Zero X 0011 : // Wm_queryendsession
10 M. Result = (Intptr) 1 ;
11 Break ;
12 Default :
13 Base . Wndproc ( Ref M );
14 Break ;
15 }
16 }
17
18 /**/ /// <Summary>
19 /// Reload wndproc Message Processing Function
20 /// </Summary>
21 /// <Param name = "M"> Windows messages </Param>
22 Protected Override Void Wndproc ( Ref System. Windows. Forms. Message m)
23 {
24 Try
25 {
26 Switch (M. msg)
27 {
28 // The system exits message processing. wm_queryendsession is used to ask whether the program needs to be closed,
29 // There must be a reverse return value. 0 does not close the program; 1 closes the program.
30 Case Wm_queryendsession:
31 M. Result = (Intptr) wm_true;
32 Return ;
33 // Sleep event processing
34 Case Wm_powerbroadcast:
35 If (M. wparam = (Intptr) pbt_apmquerysuspend)
36 {
37 // The system is about to sleep the Message Processing
38 Try
39 {
40 This . Busmanager. Close ();
41 M. Result = (Intptr) wm_true;
42 }
43 Catch
44 {
45//Capture exceptions without handling
46}
47 }
48 Break ;
49 Default :
50 Break ;
51 }
52 Base . Wndproc ( Ref M );
53 }
54 Catch (Exception E)
55 {
56MessageBox. Show (E. Message );
57}
58 }
59
60
The above are two paragraphsCode, Any one can shut down the program!