请问,如何使用winform技术,实现电脑关机?
例如,在winform窗体中添加一个button控件,当点击button时,电脑会自动关机,请给出源代码,谢了!
[解决办法]
添加button事件 加入 ExitWindowsEx(EWX_SHUTDOWN,0);
[解决办法]
Parameters
uFlags
The shutdown type. This parameter must include one of the following values.
Value Meaning
EWX_LOGOFF
0
Shuts down all processes running in the logon session of the process that called the ExitWindowsEx function. Then it logs the user off.
This flag can be used only by processes running in an interactive user's logon session.
EWX_POWEROFF
0x00000008
Shuts down the system and turns off the power. The system must support the power-off feature.
The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.
EWX_REBOOT
0x00000002
Shuts down the system and then restarts the system.
The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.
EWX_RESTARTAPPS
0x00000040
Shuts down the system and then restarts it, as well as any applications that have been registered for restart using the RegisterApplicationRestart function. These application receive the WM_QUERYENDSESSION message with lParam set to the ENDSESSION_CLOSEAPP value. For more information, see Guidelines for Applications.
EWX_SHUTDOWN
0x00000001
Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped.
The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.
Specifying this flag will not turn off the power even if the system supports the power-off feature. You must specify EWX_POWEROFF to do this.
Windows XP SP1: If the system supports the power-off feature, specifying this flag turns off the power.
This parameter can optionally include one of the following values.
Value Meaning
EWX_FORCE
0x00000004
This flag has no effect if terminal services is enabled. Otherwise, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_FORCEIFHUNG
0x00000010
Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message within the timeout interval. For more information, see the Remarks.
Windows NT and Windows Me/98/95: This value is not supported.
[解决办法]
BOOL WINAPI ExitWindowsEx(
__in UINT uFlags,
__in DWORD dwReason
);
Requirements:
Declared in Winuser.h; include Windows.h.