CSDN博客

img BestBear

系统之雕虫小技(二)——关机、待机、重启动

发表于2004/6/25 16:49:00  1164人阅读

五 关机,待机,重启动

// Sd.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "resource.h"
#include

#define MAX_LOADSTRING 100
#define BUTTON_HEIGHT  26
#define BUTTON_WIDTH   95
#define OK_X     180
#define OK_Y  45
#define Cancel_X OK_X
#define Cancel_Y OK_Y + BUTTON_HEIGHT + 10

// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];        // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];        // The title bar text
HBITMAP ScreenBitmap,MainFrameBitmap;

HBITMAP vScreenBitmap;
HDC vScreenDC;
int maxX, maxY;
HDC WindowDC;
int oldX, oldY;
int CaptionHeight, BorderSizeX, BorderSizeY;
UINT shutdown = 0;
BOOL OKplus = FALSE, Cancelplus = FALSE;
BOOL LButtonUp = TRUE;
short mouseX, mouseY, mouseDownX, mouseDownY;
INT timeout = 0;
DWORD count = 0;
HWND hwnd, countdlg;

// Foward declarations of functions included in this code module:
ATOM    MyRegisterClass(HINSTANCE hInstance);
BOOL    InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
void    StartShutdown();
BOOL    isNT();

inline BOOL InBox(int x1, int y1, int x2, int y2, int x, int y)
{
 return (x >= x1) && (x <= x2) && (y >= y1) && (y <= y2);
}
/********************************************************************/
void ShowBitmap(HWND hWnd, int sourceX, int sourceY, int destX, int destY, int width, int height)
{
HDC memdc;
 memdc = CreateCompatibleDC(WindowDC);
 SelectObject(memdc, MainFrameBitmap);
 BitBlt(WindowDC, destX, destY, width, height, memdc, sourceX, sourceY, SRCCOPY);
 DeleteDC(memdc);
}

void ShowMainFrame(HWND hWnd)
{
 ShowBitmap(hWnd, 0, 0 ,0, 0, 300, 180);
}
/********************************************************************/
void CheckShutdown(HWND hWnd, UINT sd)
{
 if (sd == 0)
 {
  ShowBitmap(hWnd, 205, 185, 38, 43, 18, 18);
  ShowBitmap(hWnd, 205, 210, 38, 64, 18, 18);//Clear
  ShowBitmap(hWnd, 205, 210, 38, 85, 18, 18);//Clear
 }
 else
 if (sd == 1)
 {
  ShowBitmap(hWnd, 205, 185, 38, 64, 18, 18);
  ShowBitmap(hWnd, 205, 210, 38, 43, 18, 18);//Clear
  ShowBitmap(hWnd, 205, 210, 38, 85, 18, 18);//Clear
 }
 else
 {
  ShowBitmap(hWnd, 205, 185, 38, 85, 18, 18);
  ShowBitmap(hWnd, 205, 210, 38, 43, 18, 18);//Clear
  ShowBitmap(hWnd, 205, 210, 38, 64, 18, 18);//Clear
 }

}

void CancelButton(HWND hWnd, BOOL plus)
{
const int Y = 208;
 if (plus)
 {
  ShowBitmap(hWnd, 0, Y, Cancel_X, Cancel_Y, BUTTON_WIDTH, BUTTON_HEIGHT+1);
 }
 else
 {
  ShowBitmap(hWnd, BUTTON_WIDTH+1, Y, Cancel_X, Cancel_Y, BUTTON_WIDTH, BUTTON_HEIGHT+1);
 }
}

void OKButton(HWND hWnd, BOOL plus)
{
const int Y = 180;
 if (plus)
 {
  ShowBitmap(hWnd, 0, Y, OK_X, OK_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
 }
 else
 {
  ShowBitmap(hWnd, BUTTON_WIDTH+1, Y, OK_X, OK_Y, BUTTON_WIDTH, BUTTON_HEIGHT);
 }
}

void TrackBar(HWND hWnd, UINT timeout, BOOL on)
{
static char strtime[10];
int hour,min;
 if (on)
 {
  ShowBitmap(hWnd, 260, 188, timeout + 12, 122, 6, 18);
  hour = timeout / 60;
  min = timeout % 60;
  SetBkMode(WindowDC, TRANSPARENT);
  sprintf(strtime, "%02d:%02d", hour, min);
  ShowBitmap(hWnd, 250, 2, 250, 2, 50, 20);
  SetTextColor(WindowDC, 0x0);
  TextOut(WindowDC, 251, 7, strtime, strlen(strtime));
  SetTextColor(WindowDC, 0xFFFFFFFF);
  TextOut(WindowDC, 250, 6, strtime, strlen(strtime));
 }
 else
 {
  if (timeout > 20)
   ShowBitmap(hWnd, 20, 122, timeout + 12, 122, 6, 18);
  else
   ShowBitmap(hWnd, 50, 122, timeout + 12, 122, 6, 18);
 }
}

inline void TitleBar(HWND hWnd)
{
 ShowBitmap(hWnd, 0, 0, 0, 0, 300, CaptionHeight+5);
 TrackBar(hWnd, timeout, TRUE);
}

void RefreshAll(HWND hWnd)
{
 ShowMainFrame(hWnd);
 OKButton(hWnd, OKplus);
 CancelButton(hWnd, Cancelplus);
 CheckShutdown(hWnd, shutdown);
 TrackBar(hWnd, timeout, TRUE);
}
/********************************************************************/
int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 MSG msg;
 HACCEL hAccelTable;

 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_SD, szWindowClass, MAX_LOADSTRING);
 MainFrameBitmap = LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_MAINFRAME));
 if (MainFrameBitmap == NULL) return FALSE;
 MyRegisterClass(hInstance);

 // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow))
 {
  return FALSE;
 }

 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SD);

 // Main message loop:
 while (GetMessage(&msg, NULL, 0, 0))
 {
  if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  {
   TranslateMessage(&msg);
   DispatchMessage(&msg);
  }
 }

 return msg.wParam;
}

//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage is only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
 WNDCLASSEX wcex;

 wcex.cbSize = sizeof(WNDCLASSEX);

 wcex.style   = CS_HREDRAW | CS_VREDRAW;
 wcex.lpfnWndProc = (WNDPROC)WndProc;
 wcex.cbClsExtra  = 0;
 wcex.cbWndExtra  = 0;
 wcex.hInstance  = hInstance;
 wcex.hIcon   = LoadIcon(hInstance, (LPCTSTR)IDI_SD);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = NULL;//(LPCSTR)IDC_SD;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

 return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HANDLE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable
   maxX = GetSystemMetrics(SM_CXSCREEN);
   maxY = GetSystemMetrics(SM_CYSCREEN);

   oldX = (maxX - 300) >> 1;
   oldY = ((maxY - 180) >> 1) - 30;
 //  hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPED,
 //     oldX, oldY, 300, 180, NULL, NULL, hInstance, NULL);
   hWnd = CreateWindowEx(WS_EX_APPWINDOW ,szWindowClass, szTitle, WS_VISIBLE | WS_POPUP,
      oldX, oldY, 300, 180, NULL, NULL, hInstance, NULL);
   if (!hWnd)
   {
      return FALSE;
   }

   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);
   SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);

   return TRUE;
}

inline BOOL MouseInOK(int mouseX, int mouseY)
{
 return ((mouseX >= OK_X-2) && (mouseX <= OK_X+BUTTON_WIDTH-2)
  && (mouseY > OK_Y - CaptionHeight) && (mouseY < OK_Y+BUTTON_HEIGHT - CaptionHeight));
}

inline BOOL MouseInCancel(int mouseX, int mouseY)
{
 return ((mouseX >= Cancel_X-2) && (mouseX <= Cancel_X+BUTTON_WIDTH-2)
   && (mouseY > Cancel_Y - CaptionHeight) && (mouseY < Cancel_Y+BUTTON_HEIGHT - CaptionHeight));
}

inline BOOL MouseInTrack(int mouseX, int mouseY)
{
 return (InBox(12, 122 - CaptionHeight, 300 - 18, 122 + 18 - CaptionHeight, mouseX, mouseY));
}

inline BOOL TrackInRange(int index)
{
 return (index >= 0) && (index <= 60*4+30);
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;
 PAINTSTRUCT ps;
 HDC hdc;
 static BOOL InMove = 0;

 switch (message)
 {
  case WM_CREATE:
   WindowDC = GetWindowDC(hWnd);
   vScreenDC = CreateCompatibleDC(WindowDC);
   vScreenBitmap = CreateCompatibleBitmap(WindowDC, maxX,maxY);
   CaptionHeight = 0;//GetSystemMetrics(SM_CYCAPTION)+GetSystemMetrics(SM_CYFIXEDFRAME);
   BorderSizeX = GetSystemMetrics(SM_CXBORDER);
   BorderSizeY = GetSystemMetrics(SM_CYBORDER);
   hwnd = hWnd;
   return TRUE;

  case WM_COMMAND:
   wmId    = LOWORD(wParam);
   wmEvent = HIWORD(wParam);
   // Parse the menu selections:
   switch (wmId)
   {
    case IDCANCEL:
     SendMessage(hWnd, WM_DESTROY, 0, 0);
     break;
    case IDOK:
     SendMessage(hWnd, WM_APP+100, 0, 0);
     break;
    case IDM_INC:
     if (timeout < 4*60 + 30)
      {
       TrackBar(hWnd, timeout, FALSE);
       timeout++;
       TrackBar(hWnd, timeout, TRUE);
      }
     break;
    case IDM_DEC:
     if (timeout > 0)
      {
       TrackBar(hWnd, timeout, FALSE);
       timeout--;
       TrackBar(hWnd, timeout, TRUE);
      }
     break;
    case IDM_CINC:
     if (timeout < 4*60 + 30)
      {
       TrackBar(hWnd, timeout, FALSE);
       timeout+=15;
       if (timeout > 4*60 + 30) timeout = 4*60+30;
       TrackBar(hWnd, timeout, TRUE);
      }
     break;
    case IDM_CDEC:
     if (timeout > 0)
      {
       TrackBar(hWnd, timeout, FALSE);
       timeout-=15;
       if (timeout < 0) timeout = 0;
       TrackBar(hWnd, timeout, TRUE);
      }
     break;
    case IDM_EXIT:
       DestroyWindow(hWnd);
       break;
    default:
       return DefWindowProc(hWnd, message, wParam, lParam);
   }
   break;
  
  case WM_LBUTTONDOWN:
   SetCapture(hWnd);
   InMove = FALSE;
   LButtonUp = FALSE;
   mouseX = LOWORD(lParam);
   mouseY = HIWORD(lParam);
   mouseDownX = mouseX;
   mouseDownY = mouseY;
   if (MouseInOK(mouseX, mouseY))
   {
    OKplus = TRUE;
    OKButton(hWnd, OKplus);
   }
   else
   if (MouseInCancel(mouseX, mouseY))
   {
    Cancelplus = TRUE;
    CancelButton(hWnd, Cancelplus);
   }
   else
   if (InBox(37, 43-CaptionHeight, 40+15+70, 43+13-CaptionHeight, mouseX, mouseY))
   {
    shutdown = 0;
    CheckShutdown(hWnd, shutdown);
   }
   else
   if (InBox(37, 66-CaptionHeight, 40+15+58, 66+13-CaptionHeight, mouseX, mouseY))
   {
    shutdown = 1;
    CheckShutdown(hWnd, shutdown);
   }
   else
   if (InBox(37, 87-CaptionHeight, 40+15+90, 87+13-CaptionHeight, mouseX, mouseY))
   {
    shutdown = 2;
    CheckShutdown(hWnd, shutdown);
   }
   else
   if (MouseInTrack(mouseX, mouseY))
   {
    TrackBar(hWnd, timeout, FALSE);
    timeout = mouseX - 12;
    TrackBar(hWnd, timeout, TRUE);
   }
   else InMove = TRUE;
   break;

  case WM_KILLFOCUS:
   TitleBar(hWnd);
   //Beep(1000, 300);
   break;
  case WM_SETFOCUS:
   TitleBar(hWnd);
   break;

  case WM_LBUTTONUP:
   ReleaseCapture();
   LButtonUp = TRUE;
   mouseX = LOWORD(lParam);
   mouseY = HIWORD(lParam);
   //***************
   if (MouseInOK(mouseX, mouseY) && MouseInOK(mouseDownX, mouseDownY))
   {
    SendMessage(hWnd, WM_APP+100, 0, 0);
   }
   else
   if (MouseInCancel(mouseX, mouseY) && MouseInCancel(mouseDownX, mouseDownY))
   {
    SendMessage(hWnd, WM_CLOSE, 0, 0);
   }
  
   if (OKplus)
   {
    OKplus = FALSE;
    OKButton(hWnd, OKplus);
   }
   
   if (Cancelplus)
   {
    Cancelplus = FALSE;
    CancelButton(hWnd, Cancelplus);
   }
   break;

  case WM_MOVE:
   mouseX = LOWORD(lParam);
   mouseY = HIWORD(lParam);
   if ((oldY < CaptionHeight) && (oldY < HIWORD(lParam)))
   {
    //InvalidateRect(hWnd, NULL, FALSE);
    TitleBar(hWnd);
   }

   if (((oldX <= BorderSizeX+3) && (mouseX > oldX))
    || ((oldX-BorderSizeX + 300 >= maxX) && (mouseX < oldX))
    || ((oldY + 180 >= maxY) && (mouseY < oldY)))
    RefreshAll(hWnd);

   //TrackBar(hWnd, oldX, TRUE);

   oldX = mouseX;
   oldY = mouseY;
   break;

  case WM_MOUSEMOVE:
   mouseX = LOWORD(lParam);
   mouseY = HIWORD(lParam);

   LButtonUp = wParam & MK_LBUTTON?FALSE:TRUE;

   if (MouseInTrack(mouseDownX, mouseDownY) && !LButtonUp)
   {
    if (mouseX < 12)
     {
     if (timeout > 0)
      {
       TrackBar(hWnd, timeout, FALSE);
       timeout = 0;
       TrackBar(hWnd, timeout, TRUE);
      }
     }
    else
    if (mouseX > 4*60+30+12)
     {
     if (timeout < 4*60+30)
      {
       TrackBar(hWnd, timeout, FALSE);
       timeout = 4*60+30;
       TrackBar(hWnd, timeout, TRUE);
      }
     }
    else
     {
      TrackBar(hWnd, timeout, FALSE);
      timeout = mouseX - 12;
      TrackBar(hWnd, timeout, TRUE);
     }
   }
   else
   if (OKplus && (!MouseInOK(mouseX, mouseY)))
   {
    OKplus = FALSE;
    OKButton(hWnd, OKplus);
   }
   else
   if (Cancelplus &&(!MouseInCancel(mouseX, mouseY)))
   {
    Cancelplus = FALSE;
    CancelButton(hWnd, Cancelplus);
   }
   else
   if (!LButtonUp && (MouseInOK(mouseX, mouseY)) && (MouseInOK(mouseDownX, mouseDownY)))
   {
    OKplus = TRUE;
    OKButton(hWnd, OKplus);
   }
   else
   if (!LButtonUp &&(MouseInCancel(mouseX, mouseY)) && (MouseInCancel(mouseDownX, mouseDownY)))
   {
    Cancelplus = TRUE;
    CancelButton(hWnd, Cancelplus);
   }
   else
   if (!LButtonUp && InMove)
   {
   RECT Rect;
    GetWindowRect(hWnd, &Rect);
    MoveWindow(hWnd, Rect.left + (mouseX - mouseDownX), Rect.top + (mouseY - mouseDownY), Rect.right - Rect.left, Rect.bottom - Rect.top, TRUE);
   }
   break;

  case WM_PAINT:
   hdc = BeginPaint(hWnd, &ps);
    //hdc = GetWindowDC(hWnd);
    //MemDC = CreateCompatibleDC(WinDC);
    //SelectObject(MemDC, MainFrameBitmap);
   RefreshAll(hWnd);
   //BitBlt(WindowDC, 0, 0, 300, 180, vScreenDC, 0, 0, SRCCOPY);
    //ReleaseDC(hWnd, WinDC);
    //DeleteDC(MemDC);

   EndPaint(hWnd, &ps);
   break;
  case WM_DESTROY:
   ReleaseDC(hWnd, WindowDC);
   DeleteDC(vScreenDC);
   PostQuitMessage(0);
   break;
  case WM_APP+100:
   //Beep(100,1000);
   StartShutdown();
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
   }
   return 0;
}

LRESULT CALLBACK CountDownDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static char time[10];
static int X = 0, Y = 0;
 switch (message)
 {
  case WM_INITDIALOG:
   countdlg = hDlg;
    return TRUE;

  case WM_COMMAND:
   if (LOWORD(wParam) == IDCANCEL)
   {
    EndDialog(hDlg, LOWORD(wParam));
    return TRUE;
   }
   break;

  case WM_TIMER:
   sprintf(time, "%02d:%02d:%02d", count / 3600, (count % 3600) / 60, count % 60);
   SetDlgItemText(hDlg, IDC_TIME, time);
   break;

  case WM_CLOSE:
   EndDialog(hDlg, 0);
   return TRUE;

  case WM_MOUSEMOVE:
   if (wParam & MK_LBUTTON)
   {
   int x, y;
   RECT Rect;
    GetWindowRect(hDlg, &Rect);
    x = LOWORD(lParam);
    y = HIWORD(lParam);
    MoveWindow(hDlg, Rect.left + (x - X), Rect.top + (y - Y), Rect.right - Rect.left, Rect.bottom - Rect.top, TRUE);
   }
   break;

  case WM_LBUTTONDOWN:
   X = LOWORD(lParam);
   Y = HIWORD(lParam);
   SetCapture(hDlg);
   break;

  case WM_LBUTTONUP:
   ReleaseCapture();
   break;
}
    return FALSE;
}

BOOL ShutdownNT()
{
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
 
if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return FALSE;
 
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);
 
tkp.PrivilegeCount = 1;  // one privilege to set   
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
 
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
        (PTOKEN_PRIVILEGES)NULL, 0);
 
if (GetLastError() != ERROR_SUCCESS) return FALSE;  
 
 if (shutdown == TRUE)
  ExitWindowsEx(EWX_SHUTDOWN, 0L);
 else ExitWindowsEx(EWX_REBOOT, 0L);
return TRUE;
}

void Shutdown9x()
{
 switch (shutdown)
 {
 case 0:
  ExitWindowsEx(EWX_SHUTDOWN, 0L);
  break;
 case 1:
  ExitWindowsEx(EWX_REBOOT, 0L);
  break;
 default:
  WinExec("Exit To Dos.pif", SW_SHOWNORMAL);
  break;
 }
}

VOID WINAPI TimerProc(HWND hDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{
 if (count <= 0)
 {
  KillTimer(hwnd, 0xCAFE);
  if (isNT())
   ShutdownNT();
  else
   Shutdown9x();
  SendMessage(countdlg, WM_CLOSE, 0, 0);
  SendMessage(hwnd, WM_CLOSE, 0, 0);
 }
 else
 {
  count--;
  SendMessage(countdlg, WM_TIMER, 0, 0);
 }
}

void StartShutdown()
{
char message[MAX_LOADSTRING];
 if (timeout == 0)
 {
  if (isNT())
   ShutdownNT();
  else
   Shutdown9x();
  SendMessage(hwnd, WM_CLOSE, 0, 0);
 }
 else
 {
  char shutdown_msg[3][30] = {"shutdown", "restart", "restart in MSDOS mode"};
  count = (long)timeout * 60;
  SetTimer(hwnd, 0xCAFE, 1000, (TIMERPROC)TimerProc);
  ShowWindow(hwnd, SW_HIDE);
  sprintf(message, "    System will be %s after %d hour(s) and %d minute(s)    ", shutdown_msg[shutdown], timeout / 60, timeout % 60);
  MessageBox(hwnd, message, "Warning", MB_OK | MB_ICONWARNING);

  if (DialogBox(hInst, MAKEINTRESOURCE(IDD_COUNTDOWN), hwnd, (DLGPROC)CountDownDlg)==IDCANCEL)
   SendMessage(hwnd, WM_CLOSE, 0, 0);
 }
}

BOOL isNT()
{
OSVERSIONINFO os;
   os.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
   GetVersionEx(&os);
   return os.dwPlatformId == VER_PLATFORM_WIN32_NT;
}

0 0

相关博文

我的热门文章

img
取 消
img