NTShutdownDlg.cpp
上传用户:mosonhua
上传日期:2022-07-25
资源大小:31k
文件大小:5k
源码类别:

系统编程

开发平台:

Visual C++

  1. // NTShutdownDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "NTShutdown.h"
  5. #include "NTShutdownDlg.h"
  6. #include   <windows.h>  
  7. #include   <tchar.h>  
  8. #include   <stdio.h>   
  9. #ifdef _DEBUG
  10. #define new DEBUG_NEW
  11. #undef THIS_FILE
  12. static char THIS_FILE[] = __FILE__;
  13. #endif
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CAboutDlg dialog used for App About
  16. class CAboutDlg : public CDialog
  17. {
  18. public:
  19. CAboutDlg();
  20. // Dialog Data
  21. //{{AFX_DATA(CAboutDlg)
  22. enum { IDD = IDD_ABOUTBOX };
  23. //}}AFX_DATA
  24. // ClassWizard generated virtual function overrides
  25. //{{AFX_VIRTUAL(CAboutDlg)
  26. protected:
  27. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  28. //}}AFX_VIRTUAL
  29. // Implementation
  30. protected:
  31. //{{AFX_MSG(CAboutDlg)
  32. //}}AFX_MSG
  33. DECLARE_MESSAGE_MAP()
  34. };
  35. CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
  36. {
  37. //{{AFX_DATA_INIT(CAboutDlg)
  38. //}}AFX_DATA_INIT
  39. }
  40. void CAboutDlg::DoDataExchange(CDataExchange* pDX)
  41. {
  42. CDialog::DoDataExchange(pDX);
  43. //{{AFX_DATA_MAP(CAboutDlg)
  44. //}}AFX_DATA_MAP
  45. }
  46. BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
  47. //{{AFX_MSG_MAP(CAboutDlg)
  48. // No message handlers
  49. //}}AFX_MSG_MAP
  50. END_MESSAGE_MAP()
  51. /////////////////////////////////////////////////////////////////////////////
  52. // CNTShutdownDlg dialog
  53. CNTShutdownDlg::CNTShutdownDlg(CWnd* pParent /*=NULL*/)
  54. : CDialog(CNTShutdownDlg::IDD, pParent)
  55. {
  56. //{{AFX_DATA_INIT(CNTShutdownDlg)
  57. // NOTE: the ClassWizard will add member initialization here
  58. //}}AFX_DATA_INIT
  59. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  60. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  61. }
  62. void CNTShutdownDlg::DoDataExchange(CDataExchange* pDX)
  63. {
  64. CDialog::DoDataExchange(pDX);
  65. //{{AFX_DATA_MAP(CNTShutdownDlg)
  66. // NOTE: the ClassWizard will add DDX and DDV calls here
  67. //}}AFX_DATA_MAP
  68. }
  69. BEGIN_MESSAGE_MAP(CNTShutdownDlg, CDialog)
  70. //{{AFX_MSG_MAP(CNTShutdownDlg)
  71. ON_WM_SYSCOMMAND()
  72. ON_WM_PAINT()
  73. ON_WM_QUERYDRAGICON()
  74. //}}AFX_MSG_MAP
  75. END_MESSAGE_MAP()
  76. /////////////////////////////////////////////////////////////////////////////
  77. // CNTShutdownDlg message handlers
  78. BOOL CNTShutdownDlg::OnInitDialog()
  79. {
  80. CDialog::OnInitDialog();
  81. // Add "About..." menu item to system menu.
  82. // IDM_ABOUTBOX must be in the system command range.
  83. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
  84. ASSERT(IDM_ABOUTBOX < 0xF000);
  85. CMenu* pSysMenu = GetSystemMenu(FALSE);
  86. if (pSysMenu != NULL)
  87. {
  88. CString strAboutMenu;
  89. strAboutMenu.LoadString(IDS_ABOUTBOX);
  90. if (!strAboutMenu.IsEmpty())
  91. {
  92. pSysMenu->AppendMenu(MF_SEPARATOR);
  93. pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
  94. }
  95. }
  96. // Set the icon for this dialog.  The framework does this automatically
  97. //  when the application's main window is not a dialog
  98. SetIcon(m_hIcon, TRUE); // Set big icon
  99. SetIcon(m_hIcon, FALSE); // Set small icon
  100. // TODO: Add extra initialization here
  101. return TRUE;  // return TRUE  unless you set the focus to a control
  102. }
  103. void CNTShutdownDlg::OnSysCommand(UINT nID, LPARAM lParam)
  104. {
  105. if ((nID & 0xFFF0) == IDM_ABOUTBOX)
  106. {
  107. CAboutDlg dlgAbout;
  108. dlgAbout.DoModal();
  109. }
  110. else
  111. {
  112. CDialog::OnSysCommand(nID, lParam);
  113. }
  114. }
  115. // If you add a minimize button to your dialog, you will need the code below
  116. //  to draw the icon.  For MFC applications using the document/view model,
  117. //  this is automatically done for you by the framework.
  118. void CNTShutdownDlg::OnPaint() 
  119. {
  120. if (IsIconic())
  121. {
  122. CPaintDC dc(this); // device context for painting
  123. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  124. // Center icon in client rectangle
  125. int cxIcon = GetSystemMetrics(SM_CXICON);
  126. int cyIcon = GetSystemMetrics(SM_CYICON);
  127. CRect rect;
  128. GetClientRect(&rect);
  129. int x = (rect.Width() - cxIcon + 1) / 2;
  130. int y = (rect.Height() - cyIcon + 1) / 2;
  131. // Draw the icon
  132. dc.DrawIcon(x, y, m_hIcon);
  133. }
  134. else
  135. {
  136. CDialog::OnPaint();
  137. }
  138. }
  139. // The system calls this to obtain the cursor to display while the user drags
  140. //  the minimized window.
  141. HCURSOR CNTShutdownDlg::OnQueryDragIcon()
  142. {
  143. return (HCURSOR) m_hIcon;
  144. }
  145. void CNTShutdownDlg::OnOK() 
  146. {
  147. // TODO: Add extra validation here
  148. // WinShutdown(EWX_POWEROFF|EWX_FORCE);
  149. WinShutdown(EWX_LOGOFF|EWX_FORCE);
  150. }
  151. void CNTShutdownDlg::WinShutdown(UINT ShutdownFlag)
  152. {  
  153. OSVERSIONINFO   oi;  
  154. oi.dwOSVersionInfoSize   =   sizeof(OSVERSIONINFO);  
  155. GetVersionEx(&oi);  
  156. //如果是NT/2000下需先设置相关权限  
  157. if   (oi.dwPlatformId   ==   VER_PLATFORM_WIN32_NT)    
  158. {  
  159. HANDLE   handle;  
  160. TOKEN_PRIVILEGES   tkp;  
  161. OpenProcessToken(GetCurrentProcess(),   TOKEN_ADJUST_PRIVILEGES   |   TOKEN_QUERY,   &handle);  
  162. LookupPrivilegeValue(NULL,   SE_SHUTDOWN_NAME,   &tkp.Privileges[0].Luid);  
  163. tkp.PrivilegeCount   =   1;     //   one   privilege   to   set          
  164. tkp.Privileges[0].Attributes   =   SE_PRIVILEGE_ENABLED;    
  165. AdjustTokenPrivileges(handle,   FALSE,   &tkp,   0,   (PTOKEN_PRIVILEGES)NULL,   0);  
  166. }  
  167. //WIN98下关机或注销时可直接调用下面涵数既可  
  168. ::ExitWindowsEx(ShutdownFlag,0);  
  169. }