⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timetipdlg.cpp

📁 非比寻常的超级记事本 1.日历控件的使用 2.托盘技术 3.开机后应用程序自动启动 4.非模态对话框的创建和关闭 5.Singleton类的使用 6.文件的读写 7.弹出式菜单的创建
💻 CPP
字号:
// TimeTipDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SuperNotepad.h"
#include "TimeTipDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

const UINT ID_TIME_FLAG = 1;
/////////////////////////////////////////////////////////////////////////////
// CTimeTipDlg dialog

static UINT nIndicators[] = {
	ID_SEPARATOR,
	ID_SEPARATOR
};

CTimeTipDlg::CTimeTipDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTimeTipDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CTimeTipDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	
	// Save the full path of time setting file
	GetModuleFileName( AfxGetInstanceHandle(), m_strFileName.GetBuffer( MAX_PATH ), MAX_PATH );
	m_strFileName.ReleaseBuffer( MAX_PATH );
	m_strFileName.Replace( _T( "SuperNotepad.exe" ), _T( "TimeInfo.dat" ) );

	m_nTip = 0;
	m_timeInfoArray.RemoveAll();
}


void CTimeTipDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTimeTipDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	DDX_Control(pDX, IDC_LIST_INFORMATION, m_listCtrlInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTimeTipDlg, CDialog)
	//{{AFX_MSG_MAP(CTimeTipDlg)
	ON_WM_TIMER()

	ON_NOTIFY_EX( TTN_NEEDTEXT, 0, OnShowToolBarTip )
	ON_COMMAND( ID_BTN_ADD, AddSettingItem )
	ON_MESSAGE( WM_SET_SHUTDOWN, OnShutDown )
	ON_COMMAND( ID_BTN_DEL, DelSettingItem )
	ON_MESSAGE( WM_SET_SHOWTIP, OnShowTip )
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTimeTipDlg message handlers

BOOL CTimeTipDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();

	InitListCtrl();
	AddToolBar();

	AddStatusBar();

	SetTimer( ID_TIME_FLAG, 1000, NULL );

	GetSetting();
	
	return TRUE;  
}

void CTimeTipDlg::InitListCtrl()
{
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
	m_listCtrlInfo.SetExtendedStyle( dwExStyle );

	m_listCtrlInfo.SetBkColor( RGB( 100, 180, 250 ) );
	m_listCtrlInfo.SetTextBkColor( RGB( 100, 180, 250 ) );

	LVCOLUMN lvcol;

	lvcol.mask	= LVCF_FMT | LVCF_SUBITEM | LVCF_TEXT | LVCF_WIDTH;
	lvcol.fmt	= LVCFMT_CENTER;
	lvcol.iSubItem	= 1;
	lvcol.cx	= 80;

	lvcol.pszText	= _T( "时间" );

	m_listCtrlInfo.InsertColumn( 0, & lvcol );

	lvcol.pszText	= _T( "行为" );
	m_listCtrlInfo.InsertColumn( 1, & lvcol );

	lvcol.cx	= 400;
	lvcol.pszText	= _T( "详细描述" );
	m_listCtrlInfo.InsertColumn( 2, & lvcol );
}

void CTimeTipDlg::AddToolBar()
{

	m_toolBar.Create( this, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS );
	m_toolBar.LoadToolBar( IDR_TOOLBAR_SETTING );

	CRect rectClientStart;
	CRect rectClientNow;

	GetClientRect( & rectClientStart );
	RepositionBars( AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0, reposQuery, rectClientNow );

	CPoint ptOffset( rectClientNow.left - rectClientStart.left, rectClientNow.top - rectClientStart.top );
	CRect rectChild;

	CWnd* pWndChild = GetWindow( GW_CHILD );
	ASSERT_VALID( pWndChild );

	while ( pWndChild )
	{
		pWndChild->GetWindowRect( & rectChild );
		ScreenToClient( & rectChild );
		rectChild.OffsetRect( ptOffset );
		pWndChild->MoveWindow( rectChild, FALSE );

		pWndChild = pWndChild->GetNextWindow();
	}
}

void CTimeTipDlg::AddStatusBar()
{
	m_statusBar.Create( this );
	m_statusBar.SetIndicators( nIndicators, sizeof( nIndicators ) / sizeof( UINT ) );

	CRect rectClient;
	GetClientRect( & rectClient );

	m_statusBar.SetPaneInfo( 0, ID_SEPARATOR, SBPS_STRETCH, rectClient.Width() / 2  );
	m_statusBar.SetPaneInfo( 1, ID_SEPARATOR, SBPS_STRETCH, 0 );
	RepositionBars( AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, ID_SEPARATOR );

	SetCurrentTime();
}

void CTimeTipDlg::OnTimer(UINT nIDEvent) 
{
	CString strTime = _T( "" );
	TCHAR   chSpace[ 4 ] = { ' ' };

	strTime.Format( _T( "当前时间" ) );
	strTime += chSpace;

	strTime += CTime::GetCurrentTime().Format( _T( "%Y-%m-%d %H:%M:%S" ) );

	m_statusBar.SetPaneText( 0, strTime );

	strTime = CTime::GetCurrentTime().Format( _T( "%H:%M" ) );

	for ( int i = 0; i < m_timeInfoArray.GetSize(); i++ )
	{
		if ( m_timeInfoArray.GetAt( i )->GetTime() == strTime )
		{
			if ( ( m_timeInfoArray.GetAt( i )->GetTipType() ) == _T( "关机" ) && ( m_nTip != i ) )
			{
				m_nTip = i;
				PostMessage( WM_SET_SHUTDOWN, ( WPARAM ) i );
			}
			else if ( m_nTip != i )
			{
				m_nTip = i;
				PostMessage( WM_SET_SHOWTIP, ( WPARAM ) i );
			}
		} 
	}
	
	CDialog::OnTimer(nIDEvent);	
}

void CTimeTipDlg::SetCurrentTime()
{
	CString strTime      = _T( "" );
	TCHAR   chSpace[ 4 ] = { ' ' };

	strTime.Format( _T( "当前时间" ) );
	strTime += chSpace;

	strTime += CTime::GetCurrentTime().Format( _T( "%Y-%m-%d %H:%M:%S" ) );

	m_statusBar.SetPaneText( 0, strTime );
}

BOOL CTimeTipDlg::OnShowToolBarTip( UINT uID, NMHDR* pTTTStruct, LRESULT* pResult )
{
	pResult = NULL;	
	uID     = 0;	

	TOOLTIPTEXT* pTTT = ( TOOLTIPTEXT* ) pTTTStruct;
	UINT nID = pTTTStruct->idFrom;

	switch( nID )
	{
	case ID_BTN_ADD:
		pTTT->lpszText = MAKEINTRESOURCE( ( LPCSTR )( ID_BTN_ADD ) );
		break;
	case ID_BTN_DEL:
		pTTT->lpszText = MAKEINTRESOURCE( ( LPCSTR )( ID_BTN_DEL ) );
		break;
	default:
		break;
	}

	return TRUE;
}

void CTimeTipDlg::AddSettingItem()
{
	CSettingDlg SettingDlg;

	if ( SettingDlg.DoModal() == IDOK )
	{
		LVITEM lvItem;

		lvItem.mask     = LVIF_TEXT;
		lvItem.iItem    = 0;

		lvItem.iSubItem = 0;
		lvItem.pszText  = ( LPSTR ) ( LPCSTR ) SettingDlg.m_strTime;
		m_listCtrlInfo.InsertItem( & lvItem );

		lvItem.iSubItem = 1;
		lvItem.pszText  = ( LPSTR ) ( LPCSTR ) SettingDlg.m_strTipType;
		m_listCtrlInfo.SetItem( & lvItem );

		lvItem.iSubItem = 2;
		lvItem.pszText  = ( LPSTR ) ( LPCSTR ) SettingDlg.m_strTipInfo;
		m_listCtrlInfo.SetItem( & lvItem );

		CTimeInfo* pTimeInfo = new CTimeInfo( SettingDlg.m_strTime, SettingDlg.m_strTipType, SettingDlg.m_strTipInfo );
		ASSERT_VALID( pTimeInfo );
		m_timeInfoArray.Add( pTimeInfo );
	}
}

void CTimeTipDlg::DelSettingItem()
{
	POSITION pos = m_listCtrlInfo.GetFirstSelectedItemPosition();
	if ( pos != NULL )
	{
		m_listCtrlInfo.LockWindowUpdate();

		if ( AfxMessageBox( _T( "确定要删除吗?" ), MB_YESNO ) == IDYES )
		{
			int nItem = m_listCtrlInfo.GetNextSelectedItem( pos );
			m_listCtrlInfo.DeleteItem( nItem );

			m_timeInfoArray.RemoveAt( nItem );
		}

		m_listCtrlInfo.UnlockWindowUpdate();
	}
}

void CTimeTipDlg::SaveSetting()
{
	CFile file;

	if ( file.Open( m_strFileName, CFile::modeCreate | CFile::modeWrite ) )
	{
		CArchive ar( & file, CArchive::store );

		m_timeInfoArray.Serialize( ar );

		ar.Close();
		file.Close();
	}

	int nCount = m_timeInfoArray.GetSize();
	for ( int i = 0; i < nCount; i++ )
		delete m_timeInfoArray.GetAt( i );

	m_timeInfoArray.RemoveAll();

	ASSERT( m_timeInfoArray.GetSize() == 0 );
}

void CTimeTipDlg::GetSetting()
{
	CFile file;

	if ( file.Open( m_strFileName, CFile::modeRead ) )
	{
		CArchive ar( & file, CArchive::load );

		m_timeInfoArray.Serialize( ar );

		ar.Close();
		file.Close();

		for ( int i = 0; i < m_timeInfoArray.GetSize(); i++ )
		{
			LVITEM lvItem;

			lvItem.mask     = LVIF_TEXT;
			lvItem.iItem    = i;

			lvItem.iSubItem = 0;
			lvItem.pszText  = ( LPSTR ) ( LPCSTR ) m_timeInfoArray.GetAt( i )->GetTime();
			m_listCtrlInfo.InsertItem( & lvItem );

			lvItem.iSubItem = 1;
			lvItem.pszText  = ( LPSTR ) ( LPCSTR ) m_timeInfoArray.GetAt( i )->GetTipType();
			m_listCtrlInfo.SetItem( & lvItem );

			lvItem.iSubItem = 2;
			lvItem.pszText  = ( LPSTR ) ( LPCSTR ) m_timeInfoArray.GetAt( i )->GetInfo();
			m_listCtrlInfo.SetItem( & lvItem );
		}
	}
}

void CTimeTipDlg::OnShowTip( WPARAM wParam, LPARAM lParam )
{
	CTipDlg TipDlg( m_timeInfoArray.GetAt( wParam )->GetTime(), \
					m_timeInfoArray.GetAt( wParam )->GetInfo() );
	TipDlg.DoModal();
}

void CTimeTipDlg::OnShutDown( WPARAM wParam, LPARAM lParam )
{
	if ( AfxMessageBox( _T( "关机吗 ?" ), MB_ICONINFORMATION | MB_YESNO ) == IDNO )
		return;

	TCHAR strInfo[] = _T( "到了你打算设置关机的时间了 !" );

	HANDLE hToken;
	if ( ! OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, & hToken ) )
		goto FAILED_MESSAGE;

	TOKEN_PRIVILEGES tkp;
	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 ) 
		goto FAILED_MESSAGE;

	if ( ! InitiateSystemShutdown( NULL, strInfo, 20, TRUE, FALSE ) )
		goto FAILED_MESSAGE;

	tkp.Privileges[ 0 ].Attributes = 0; 
	AdjustTokenPrivileges( hToken, FALSE, & tkp, 0, ( PTOKEN_PRIVILEGES ) NULL, 0 );
	if ( GetLastError() != ERROR_SUCCESS ) 
		goto FAILED_MESSAGE;

	ExitWindowsEx( EWX_SHUTDOWN, 0 );
	return;

FAILED_MESSAGE:
 	AfxMessageBox( _T( "关机设置不成功 ! \n对不起了 !" ), MB_ICONINFORMATION );
	return;
}

void CTimeTipDlg::OnCancel()
{
	SaveSetting();

	CDialog::OnCancel();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -