随笔-4  评论-9  文章-0  trackbacks-0

                                                                                       自动关机程序
前几天在网上看到一个自动关机的程序,不过是一个共享的,需要花钱,于是我自己就想写一个尝试一下,练练手吧。
说一下前提知识吧:

Windows XP的关机是由Shutdown.exe程序来控制的,位于Windows\System32文件夹中。如
果想让Windows 2000也实现同样的效果,可以把Shutdown.exe复制到系统目录下。
 
Shutdown.exe的参数,每个都具有特定的用途,执行每一个都会产生不同的效果,比如
“-s”就表示关闭本地计算机,“-a”表示取消关机操作,下面列出了更多参数,大家
可以在Shutdown.exe中按需使用。

-f:强行关闭应用程序

-m \\计算机名:控制远程计算机

-i:显示图形用户界面,但必须是Shutdown的第一个选项

-l:注销当前用户

-r:关机并重启

-t 时间:设置关机倒计时

-c "消息内容":输入关机对话框中的消息内容(不能超127个字符)

这就是前提知识,其实关机也可以用API(ExitWindowsEx )函数来实现,但是没有是么心意,也不好玩。我是用SDK编程实现的,所以代码有些“乱”,大家将就着看吧!!!

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

#include "stdafx.h"
#include "resource.h"
#include <stdio.h>

#define MAX_LOADSTRING 100
#define IDB_BUTTON 101
#define IDB_EDIT 102
#define IDE_STATIC 103
#define IDB_BUTTON2 109
// Global Variables:
HINSTANCE hInst;        // current instance
TCHAR szTitle[MAX_LOADSTRING];        // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];        // The title bar text

// 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);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
 // TODO: Place code here.
 MSG msg;
 HACCEL hAccelTable;
 
 // Initialize global strings
 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
 LoadString(hInstance, IDC_CLOSE, szWindowClass, MAX_LOADSTRING);
 MyRegisterClass(hInstance);
 
 // Perform application initialization:
 if (!InitInstance (hInstance, nCmdShow))
 {
  return FALSE;
 }
 
 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_CLOSE);
 
 // 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_ICON1);
 wcex.hCursor  = LoadCursor(NULL, IDC_ARROW);
 wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
 wcex.lpszMenuName = (LPCSTR)IDC_CLOSE;
 wcex.lpszClassName = szWindowClass;
 wcex.hIconSm  = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_ICON1);
 
 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
 
 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW & ~WS_MAXIMIZEBOX,
  CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
 
 if (!hWnd)
 {
  return FALSE;
 }
 
 ShowWindow(hWnd, nCmdShow);
 UpdateWindow(hWnd);
 
 return TRUE;
}

//
//  FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND - process the application menu
//  WM_PAINT - Paint the main window
//  WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
 int wmId, wmEvent;
 int itime;
 PAINTSTRUCT ps;
 HDC hdc;
 TCHAR szHello[MAX_LOADSTRING];
 LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);
 static char time[MAX_PATH];
 FILE *file;
 
 switch (message)
 {
 case WM_CREATE:
  {
   ::SetWindowPos(hWnd,HWND_TOPMOST,0,0,380,200,SWP_NOMOVE|SWP_NOREDRAW);
  
   ::CreateWindowEx(0,"button","设置",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
    30,40,50,20,hWnd,(HMENU)IDB_BUTTON,0,0);
  
   ::CreateWindowEx(0,"button","取消",WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
    30,70,50,20,hWnd,(HMENU)IDB_BUTTON2,0,0);
  
   ::CreateWindowEx(0,"edit","0",WS_CHILD|WS_VISIBLE|ES_MULTILINE|WS_BORDER,
    150,40,170,20,hWnd,(HMENU)IDB_EDIT,0,0);
  
   ::CreateWindowEx(0,"static","时间:",WS_CHILD|WS_VISIBLE|SS_SUNKEN,
    150,20,40,20,hWnd,(HMENU)IDE_STATIC,0,0);
  
  
   break;
  }
 case WM_COMMAND:
  wmId    = LOWORD(wParam);
  wmEvent = HIWORD(wParam);
  // Parse the menu selections:
  switch (wmId)
  {
  case IDM_ABOUT:
   DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
   break;
  case IDM_EXIT:
   DestroyWindow(hWnd);
   break;
  case IDB_BUTTON:
   {
    ::SetWindowText(hWnd,"关机程序");
    ::GetWindowText(::GetDlgItem(hWnd,IDB_EDIT),time,MAX_PATH);
   
    itime=atoi(time);
    itime=itime*60;

    itoa(itime,time,10);

    file=fopen("c:\\windows\\system32\\shutdown.exe","r");
    if (file==NULL)
    {
     ::MessageBox(NULL,"DFDF","DFD",MB_OK);
     return 0;
    }
    char ptime[MAX_PATH]="c:\\windows\\system32\\shutdown.exe -s -t ";
    strcat(ptime,time);
   
    ::MessageBox(NULL,ptime,"时间",MB_OK);
    system(ptime);
    fclose(file);
    break;
   }

  case IDB_BUTTON2:
   {
    system("c:\\windows\\system32\\shutdown.exe -a");
    break;
   }
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
  }
  break;
  case WM_PAINT:
   hdc = BeginPaint(hWnd, &ps);
   // TODO: Add any drawing code here...
   RECT rt;
   GetClientRect(hWnd, &rt);
   //DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
   EndPaint(hWnd, &ps);
   break;
  case WM_DESTROY:
   PostQuitMessage(0);
   break;
  default:
   return DefWindowProc(hWnd, message, wParam, lParam);
 }
 return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
 switch (message)
 {
 case WM_INITDIALOG:
  return TRUE;
 
 case WM_COMMAND:
  if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  {
   EndDialog(hDlg, LOWORD(wParam));
   return TRUE;
  }
  break;
 }
    return FALSE;
}


这个程序其实还有很多的问题,比如时间设计,功能上加强,界面的美观等等,但是也算是有个框架了,大家自己去加把,我就不再弄了。

posted on 2009-01-06 11:43 烟雨八戒 阅读(1986) 评论(5)  编辑 收藏 引用

评论:
# re: 自动关机程序 (SDK编写) 2009-01-06 15:03 | jeffry
强人啊 学习学习  回复  更多评论
  
# re: 自动关机程序 (SDK编写)[未登录] 2009-01-06 15:31 | 关中刀客
要是能关远程的电脑就很牛了,以前我还给微软的工程师咨询能不能,人说看msdn,但是msdn上的实际不能实现。你这样子实现没有什么技术含量,你可以google搜索我以前写的“红火蚁 定时关机”  回复  更多评论
  
# re: 自动关机程序 (SDK编写) 2009-01-06 22:58 | 烟雨八戒
谢谢你的评论了,这个程序没有什么技术含量的,你的推荐的那个程序我看了,他用的是GetLocalTime函数获得系统的时间,在用SetTimer函数更新时间,用ExitWindowsEx关机。很不错的程序,学习了,谢谢了。  回复  更多评论
  
# re: 自动关机程序 (SDK编写) 2009-01-08 08:45 | guest
调用Shutdown.exe也没什么新意啊。。直接写个bat不更好?

其实可以调用ZwShutdownSystem来关机~~这个效果很好哦。呵呵。。。  回复  更多评论
  
# re: 自动关机程序 (SDK编写) 2009-03-13 13:17 | te
非管理员能做到吗?  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理