========================================================================
MICROSOFT FOUNDATION CLASS LIBRARY : AutoClose
========================================================================
AppWizard has created this AutoClose application for you. This application
not only demonstrates the basics of using the Microsoft Foundation classes
but is also a starting point for writing your application.
This file contains a summary of what you will find in each of the files that
make up your AutoClose application.
AutoClose.dsp
This file (the project file) contains information at the project level and
is used to build a single project or subproject. Other users can share the
project (.dsp) file, but they should export the makefiles locally.
AutoClose.h
This is the main header file for the application. It includes other
project specific headers (including Resource.h) and declares the
CAutoCloseApp application class.
AutoClose.cpp
This is the main application source file that contains the application
class CAutoCloseApp.
AutoClose.rc
This is a listing of all of the Microsoft Windows resources that the
program uses. It includes the icons, bitmaps, and cursors that are stored
in the RES subdirectory. This file can be directly edited in Microsoft
Visual C++.
AutoClose.clw
This file contains information used by ClassWizard to edit existing
classes or add new classes. ClassWizard also uses this file to store
information needed to create and edit message maps and dialog data
maps and to create prototype member functions.
res\AutoClose.ico
This is an icon file, which is used as the application's icon. This
icon is included by the main resource file AutoClose.rc.
res\AutoClose.rc2
This file contains resources that are not edited by Microsoft
Visual C++. You should place all resources not editable by
the resource editor in this file.
/////////////////////////////////////////////////////////////////////////////
AppWizard creates one dialog class:
AutoCloseDlg.h, AutoCloseDlg.cpp - the dialog
These files contain your CAutoCloseDlg class. This class defines
the behavior of your application's main dialog. The dialog's
template is in AutoClose.rc, which can be edited in Microsoft
Visual C++.
/////////////////////////////////////////////////////////////////////////////
Other standard files:
StdAfx.h, StdAfx.cpp
These files are used to build a precompiled header (PCH) file
named AutoClose.pch and a precompiled types file named StdAfx.obj.
Resource.h
This is the standard header file, which defines new resource IDs.
Microsoft Visual C++ reads and updates this file.
/////////////////////////////////////////////////////////////////////////////
Other notes:
AppWizard uses "TODO:" to indicate parts of the source code you
should add to or customize.
If your application uses MFC in a shared DLL, and your application is
in a language other than the operating system's current language, you
will need to copy the corresponding localized resources MFC42XXX.DLL
from the Microsoft Visual C++ CD-ROM onto the system or system32 directory,
and rename it to be MFCLOC.DLL. ("XXX" stands for the language abbreviation.
For example, MFC42DEU.DLL contains resources translated to German.) If you
don't do this, some of the UI elements of your application will remain in the
language of the operating system.
/////////////////////////////////////////////////////////////////////////////
VC编写定时关机程序
4星 · 超过85%的资源 需积分: 0 164 浏览量
更新于2008-02-14
1
收藏 47KB RAR 举报
在本文中,我们将深入探讨如何使用Microsoft Foundation Class (MFC) 库在Visual C++ (VC++)环境中编写一个定时关机程序。MFC是微软提供的一套C++类库,它封装了Windows API,使开发者能更方便地构建Windows应用程序。
我们需要理解定时关机的核心机制。在Windows操作系统中,可以通过发送一个特定的系统消息来实现电脑的定时关机。这个消息通常是`WM_SHUTDOWN`。然而,在实际编程中,我们不能直接发送这个消息,而是要通过调用API函数来执行此操作。其中,`ExitWindowsEx`函数是最常用的,它可以安全地关闭或重新启动计算机。
创建一个MFC应用程序的第一步是启动Visual Studio,并选择“新建项目” -> “MFC应用程序”。在向导中,你可以选择“基于对话框”的应用程序模板,因为定时关机程序通常需要用户界面来设置关机时间。
接下来,我们需要添加一个时间选择控件,如DateTimePicker,让用户能够选择关机的日期和时间。在资源编辑器中,右键单击对话框资源,选择“插入” -> “DateTimePicker”,并为其分配一个ID(例如IDD_SHUTDOWN_TIME)。
然后,我们需要处理DateTimePicker的`ON_WM_COMMAND`消息,当用户选择一个时间时,获取所选的时间值。在`CMyApp::OnInitDialog()`方法中,可以添加以下代码:
```cpp
void CMyApp::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 获取DateTimePicker的控件指针
CDateTimePickerCtrl* pDateTimePicker = (CDateTimePickerCtrl*)GetDlgItem(IDC_SHUTDOWN_TIME);
// 设置初始时间,如果需要的话
// ...其他初始化代码
}
```
接着,定义一个定时器来触发关机操作。在`ON_BN_CLICKED`消息处理程序中,为按钮添加以下代码:
```cpp
void CMyApp::OnBnClickedShutdownButton()
{
// 获取DateTimePicker中的时间
SYSTEMTIME systemTime;
CDateTimePickerCtrl* pDateTimePicker = (CDateTimePickerCtrl*)GetDlgItem(IDC_SHUTDOWN_TIME);
pDateTimePicker->GetSystemTime(&systemTime);
// 计算从当前时间到关机时间的毫秒数
ULONGLONG millisecondsToShutdown = GetTimeDifferenceInMilliseconds(&systemTime);
// 创建定时器
SetTimer(NULL, 1, (UINT)millisecondsToShutdown, NULL);
}
```
这里,`GetTimeDifferenceInMilliseconds`是一个自定义函数,用于计算两个时间之间的差值。实现如下:
```cpp
ULONGLONG CMyApp::GetTimeDifferenceInMilliseconds(const SYSTEMTIME* shutdownTime)
{
FILETIME fileTime1, fileTime2;
ULARGE_INTEGER largeInt1, largeInt2;
// 将当前时间转换为FILETIME
GetSystemTimeAsFileTime(&fileTime1);
// 将关机时间转换为FILETIME
SystemTimeToFileTime(shutdownTime, &fileTime2);
// 转换为ULARGE_INTEGER
largeInt1.LowPart = fileTime1.dwLowDateTime;
largeInt1.HighPart = fileTime1.dwHighDateTime;
largeInt2.LowPart = fileTime2.dwLowDateTime;
largeInt2.HighPart = fileTime2.dwHighDateTime;
// 计算差值
ULONGLONG difference = largeInt2.QuadPart - largeInt1.QuadPart;
// 转换为毫秒
return difference / 10000;
}
```
我们需要处理定时器的`ON_TIMER`消息,当定时器触发时调用`ExitWindowsEx`函数:
```cpp
void CMyApp::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
// 关闭计算机
ExitWindowsEx(EWX_SHUTDOWN, 0);
KillTimer(NULL, 1); // 停止定时器
}
CDialogEx::OnTimer(nIDEvent);
}
```
在实际应用中,可能还需要添加错误处理和取消关机的功能。这只是一个基本的实现,你可以根据需求扩展功能,例如添加通知提示、允许用户取消关机等。
通过以上步骤,我们就成功地使用MFC和Visual C++编写了一个简单的定时关机程序。这个程序利用了MFC的对话框、控件以及Windows API,展示了如何在Windows环境中集成用户界面和系统级别的功能。在开发过程中,熟悉MFC类库和Windows API是至关重要的,这将有助于创建更复杂的应用程序。