为什么没有人下载我的兔子睡觉?
http://download.youkuaiyun.com/source/245188
为了防止把它丢失,把代码贴在这里了,算是备份吧。
文件的最后修改时间是 2005-7-22 14:39(估计是上班的时候悄悄改的)
图标的最后修改时间是 2005-7-12 1:16 (工程就是那天创建的,都画图标了)
原来这个东西是自己工作了一年写的。
#include <windows.h>
#include <string>
#include <sstream>
#include <utility>
#include "Resource.h"
#define WM_ICONNOTIFY (WM_USER + 101)
NOTIFYICONDATA smallRabbit;
struct ShutTime
{
ShutTime() : m_hour( -1 ), m_minute( -1 ), m_second( 0 ) {}
int m_hour;
int m_minute;
int m_second;
} shutTime;
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime);
bool MySystemShutdown();
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("RabbitSleeping") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = DLGWINDOWEXTRA ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (hInstance, szAppName) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) (COLOR_BTNFACE + 1) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateDialog (hInstance, szAppName, 0, NULL) ;
smallRabbit.cbSize = sizeof( NOTIFYICONDATA );
smallRabbit.hWnd = hwnd;
smallRabbit.uID = 100;
smallRabbit.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
smallRabbit.uCallbackMessage = WM_ICONNOTIFY;
smallRabbit.hIcon = LoadIcon( hInstance, "smallRabbit" );
strcpy(smallRabbit.szTip, "My Tooltip Text");
ShowWindow (hwnd, iCmdShow) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return (int)msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
SYSTEMTIME systime;
std::stringstream time;
switch ( message ) {
case WM_CREATE:
SetTimer( hwnd, 1, 1000, TimerProc );
break;
case WM_COMMAND:
switch (LOWORD (wParam))
{
case IDOK:
GetLocalTime( &systime );
shutTime.m_minute = systime.wMinute + GetDlgItemInt( hwnd, IDC_MINTIME, NULL, FALSE );
shutTime.m_hour = ( systime.wHour + shutTime.m_minute / 60 ) % 24;
shutTime.m_minute = shutTime.m_minute % 60;
time << ( shutTime.m_hour > 9 ? "" : "0" ) << shutTime.m_hour << ":"
<< ( shutTime.m_minute > 9 ? "" : "0" ) << shutTime.m_minute << ":"
<< "00";
SetDlgItemText( hwnd, IDC_STATICTIME, time.str().c_str() );
return 0;
case IDCANCEL:
SendMessage( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, 0 );
return 0;
}
break;
case WM_ICONNOTIFY:
if( lParam == WM_LBUTTONUP ) {
ShowWindow( hwnd, SW_SHOW );
Shell_NotifyIcon( NIM_DELETE, &smallRabbit );
return 0;
}
break;
case WM_SYSCOMMAND:
if( wParam == SC_MINIMIZE ){
Shell_NotifyIcon( NIM_ADD, &smallRabbit );
ShowWindow( hwnd, SW_HIDE );
return 0;
}
break;
case WM_DESTROY:
Shell_NotifyIcon( NIM_DELETE, &smallRabbit );
PostQuitMessage( 0 );
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam);
}
VOID CALLBACK TimerProc (HWND hwnd, UINT message, UINT iTimerID, DWORD dwTime)
{
SYSTEMTIME systime;
GetLocalTime( &systime );
std::stringstream timeNow;
timeNow << ( systime.wHour > 9 ? "" : "0" ) << systime.wHour
<< ":"
<< ( systime.wMinute > 9 ? "" : "0" ) << systime.wMinute
<< ":"
<< ( systime.wSecond > 9 ? "" : "0" ) << systime.wSecond;
SetDlgItemText( hwnd, IDC_STATICNOW, timeNow.str().c_str() );
if ( systime.wHour == shutTime.m_hour &&
systime.wMinute == shutTime.m_minute &&
systime.wSecond == shutTime.m_second )
if ( !MySystemShutdown() )
MessageBox( hwnd, "兔子睡觉", "睡觉失败", 0 );
}
bool MySystemShutdown()
{
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;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges( hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0 );
if (GetLastError() != ERROR_SUCCESS)
return false;
OSVERSIONINFO versionInfo;
versionInfo.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
GetVersionEx( &versionInfo );
if ( versionInfo.dwMajorVersion == 5 && versionInfo.dwMinorVersion == 1 ) {
if ( !InitiateSystemShutdown( NULL, "兔子睡觉", 0, true, false ) )
return false;
}
else {
typedef DWORD ( *ShutdownSystem_t )( DWORD );
ShutdownSystem_t shutdownsystem = 0;
HINSTANCE hInstance = LoadLibrary( "ntdll.dll" );
if ( hInstance )
shutdownsystem = (ShutdownSystem_t)GetProcAddress( hInstance, "NtShutdownSystem" );
if ( shutdownsystem )
shutdownsystem( 2 );
if ( hInstance == 0 || shutdownsystem == 0 )
return false;
FreeLibrary( hInstance );
}
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges( hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0 );
return true;
}
这是一个使用C++编写的简单应用程序,允许用户设定电脑自动关机的时间。程序通过系统托盘图标进行操作,用户可以在设置的时间到达后实现自动关机。
2616

被折叠的 条评论
为什么被折叠?



