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

📄 alarmdlg.cpp

📁 一个简单的日程计划工具。用VC7+DAO35开发。有兴趣的话下载后自己研究吧。
💻 CPP
字号:
// AlarmDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Alarm.h"
#include "AlarmDlg.h"
#include "mymessage.h"

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

//定义自己的消息
#define WM_MYMESSAGE WM_USER + 100
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

void CAboutDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnOK();
	//CDialog::OnLButtonDblClk(nFlags, point);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAlarmDlg dialog

CAlarmDlg::CAlarmDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAlarmDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAlarmDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAlarmDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAlarmDlg)
	DDX_Control(pDX, IDC_STATIC_TEST, m_ctlStatic);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAlarmDlg, CDialog)
	//{{AFX_MSG_MAP(CAlarmDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_NCHITTEST()
	ON_MESSAGE(WM_MYMESSAGE, OnNotifyIcon)
	ON_WM_DESTROY()
	ON_COMMAND(IDC_EXIT, OnExit)
	ON_COMMAND(IDC_SETUP, OnSetup)
	ON_WM_TIMER()
	ON_COMMAND(IDC_ABOUT, OnAbout)
	ON_COMMAND(IDC_SET_TIME, OnSetTime)
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_COMMAND(IDC_SHOW, OnShowHideWindow)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAlarmDlg message handlers

BOOL CAlarmDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	this->ModifyStyleEx(WS_EX_APPWINDOW, 0);

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	//初始化时间列表数组
	message.m_TimeList.RemoveAll();	
	message.m_strPlanArray.SetSize(10,10);
	message.m_strPlanArray.RemoveAll();
	message.m_nModeList.RemoveAll();

	NotifyIconData.cbSize = sizeof(NOTIFYICONDATA);
	NotifyIconData.hWnd   = m_hWnd;
	NotifyIconData.uID    = IDR_MAINFRAME;
	NotifyIconData.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;
	NotifyIconData.uCallbackMessage = WM_MYMESSAGE;
	NotifyIconData.hIcon = m_hIcon;
	CString strTemp;
	strTemp.LoadString(IDS_VERSION_INFO);
	strcpy(NotifyIconData.szTip,strTemp);

	Shell_NotifyIcon(NIM_ADD,&NotifyIconData);
	bWindowStatus = TRUE;  //窗口现在在正常状态
	
	m_ctlStatic.OnStart();

	//重新布置窗口布局 并且设置窗口为半透明状
	CRect rect;
	GetWindowRect(rect);
	int nCyCaption = GetSystemMetrics(SM_CYCAPTION);
	int nDesktopWidth = GetSystemMetrics(SM_CXSCREEN);
	int nWindowWidth  = rect.Width();
	SetWindowPos(&wndTopMost,nDesktopWidth - nWindowWidth - nCyCaption * 3,2,rect.Width(),rect.Height(),SWP_SHOWWINDOW);

	SetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE,
	GetWindowLong(this->GetSafeHwnd(),GWL_EXSTYLE)^0x80000);
	HINSTANCE hInst = LoadLibrary("User32.DLL"); 
	if(hInst) 
	{ 
		typedef BOOL (WINAPI *MYFUNC)(HWND,COLORREF,BYTE,DWORD); 
		MYFUNC fun = NULL;
		//取得SetLayeredWindowAttributes函数指针 
		fun=(MYFUNC)GetProcAddress(hInst, "SetLayeredWindowAttributes");
		if(fun)fun(this->GetSafeHwnd(),0,128,2); 
		FreeLibrary(hInst); 
	}
	
	//打开数据库
	db.Open("plan.mdb",
		TRUE,
		FALSE,
		"");
	ReadTimeInfoFromDataBase();
	ReadPlanFromDataBase();
	ReadModeInfoFromDB();

	//启动定时器
	SetTimer(0,1000,NULL);
	// return TRUE  unless you set the focus to a control
	return TRUE;  
}

void CAlarmDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CAlarmDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CRect rect;
		CWnd* m_nWnd = GetDlgItem(IDC_STATIC_TEST);
		m_nWnd->GetClientRect(rect);
		CClientDC pDC(m_nWnd);
		//DisPlayTime(pDC,rect);
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAlarmDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

UINT CAlarmDlg::OnNcHitTest(CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	/*
	UINT nRes = CDialog::OnNcHitTest(point);
	if(nRes == HTCLIENT)
	{
		return HTCAPTION;
	}
	else
	{
		return nRes;
	}
	*/
	return CDialog::OnNcHitTest(point);
}

LRESULT CAlarmDlg::OnNotifyIcon(WPARAM wParam,LPARAM lParam)
{
	if((wParam == IDR_MAINFRAME) && (lParam == WM_LBUTTONDBLCLK))
	{
		if(bWindowStatus)
		{
			ShowWindow(SW_HIDE);
			bWindowStatus = FALSE;
		}
		else
		{
			ShowWindow(SW_SHOW);
			bWindowStatus = TRUE;
		}
	}

	if((wParam == IDR_MAINFRAME) && (lParam == WM_RBUTTONUP))
	{
		CMenu pMenu;
		if (pMenu.LoadMenu(IDR_MENU1))        
		{
			CMenu* pPopup = pMenu.GetSubMenu(0);
			ASSERT(pPopup != NULL);
			CPoint Point;
			// 确定鼠标位置以便在该位置附近显示菜单
			GetCursorPos( &Point );
			SetForegroundWindow();
			pPopup->TrackPopupMenu(
				TPM_LEFTALIGN | TPM_RIGHTBUTTON,
				Point.x, Point.y, this
				); 
		}
	}
	return 0;
}

void CAlarmDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	KillTimer(0);
	db.Close();
	Shell_NotifyIcon(NIM_DELETE,&NotifyIconData);
}

void CAlarmDlg::DisPlayTime(CClientDC pDC, CRect rect)
{
	
}

void CAlarmDlg::OnExit() 
{
	// TODO: Add your command handler code here
	PostMessage(WM_CLOSE);
}

void CAlarmDlg::OnSetup() 
{
	// TODO: Add your command handler code here
	COptionDlg dlg;
	CAlarmApp* pApp = (CAlarmApp*)AfxGetApp();

	if(dlg.DoModal() == IDOK)
	{
		message.SetPlan(dlg.m_strPlan);
		message.SetPlanTime(dlg.m_tPlanTime);
		message.SetPlanDate(dlg.m_tPlanDate);
		message.SetPlanPri(dlg.m_nPri);
		message.SetMode(dlg.m_nMode);

		CTime time(dlg.m_tPlanDate.GetYear(),
			dlg.m_tPlanDate.GetMonth(),
			dlg.m_tPlanDate.GetDay(),
			dlg.m_tPlanTime.GetHour(),
			dlg.m_tPlanTime.GetMinute(),
			dlg.m_tPlanTime.GetSecond(),
			-1);
		message.m_TimeList.Add(time);
		message.m_strPlanArray.Add(dlg.m_strPlan);
		message.m_nModeList.Add(dlg.m_nMode);
	}
}

void CAlarmDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if(nIDEvent == 0)
	{
		int nLength = message.m_TimeList.GetSize();
		for(int i = 0; i < nLength; i ++)
		{
			CTime tNowTime = CTime::GetCurrentTime();
			CTimeSpan span = message.m_TimeList.GetAt(i) - tNowTime;
			//是否是重复事件 是则进入
			if(message.m_nModeList.GetAt(i) == 0)
			{
				if((message.m_TimeList.GetAt(i).GetHour() == tNowTime.GetHour())
					&& (message.m_TimeList.GetAt(i).GetMinute () == tNowTime.GetMinute())
					&& (message.m_TimeList.GetAt(i).GetSecond() == tNowTime.GetSecond())
					)
				{
					MessageDisplay.m_strPlan = message.m_strPlanArray.GetAt(i);
					if(message.m_strPlanArray.GetAt(i).Compare("关机") == 0)
					{
						PostMessage(WM_CLOSE);
						ShutDownComputer();
					}
					MessageDisplay.m_strDateTime = message.m_TimeList.GetAt(i).Format(
										"%Y年%m月%d日,%H:%M:%S");
					MessageDisplay.DoModal();
				}
			}
			else
			{
				//不是重复事件,则判断计划事件的日是否为当天
				if(
					(message.m_TimeList.GetAt(i).GetYear() == tNowTime.GetYear())
					&& (message.m_TimeList.GetAt(i).GetMonth() == tNowTime.GetMonth())
					&& (message.m_TimeList.GetAt(i).GetDay() == tNowTime.GetDay())
					)
				{
					if(((span.GetMinutes() + span.GetHours() * 60)  <= message.m_nRemainTimeList.GetAt(message.m_nModeList.GetAt(i)))
						&& (span.GetSeconds() == 0)
						&& (span.GetMinutes() >= 0)
						&& (span.GetDays() == 0)
						)
					{
						MessageDisplay.m_strPlan = message.m_strPlanArray.GetAt(i);
						if(message.m_strPlanArray.GetAt(i).Compare("关机") == 0)
						{
							PostMessage(WM_CLOSE);
							ShutDownComputer();
						}
						MessageDisplay.m_strDateTime = message.m_TimeList.GetAt(i).Format(
											"%Y年%m月%d日,%H:%M:%S");
						MessageDisplay.DoModal();
					}
				
				}
			}
		}
	}

	CDialog::OnTimer(nIDEvent);
}

void CAlarmDlg::ReadTimeInfoFromDataBase()
{
	CDaoRecordset rs(&db);
	int nRecord = 0;
	try
	{
		rs.Open( dbOpenTable,"plan");
		while( !rs.IsEOF( ) )
		{
			COleVariant var;
			COleDateTime time1;
			COleDateTime time2;			
			var = rs.GetFieldValue(2);
			time1 = var.date;
			var = rs.GetFieldValue(3);
			time2 = var.date;

			CTime time(time2.GetYear(),time2.GetMonth(),time2.GetDay(),
				time1.GetHour(),time1.GetMinute(),time1.GetSecond(),-1);
			message.m_TimeList.Add(time);
			
			nRecord++;
			rs.MoveNext( );
		}
	}
	catch( CDaoException* e )
	{
		e->Delete( );
	}

	if(rs.IsOpen())
	{
		rs.Close();
	}
}

void CAlarmDlg::ReadPlanFromDataBase()
{
	CDaoRecordset rs(&db);
	try
	{
		rs.Open( dbOpenTable,"plan");
		while( !rs.IsEOF( ) )
		{
			COleVariant var;			
			var = rs.GetFieldValue(1);
			CString strTemp;
			strTemp.Format("%s",var.bstrVal);
			message.m_strPlanArray.Add(strTemp);
			rs.MoveNext( );
		}
	}
	catch( CDaoException* e )
	{
		e->Delete( );
	}

	if(rs.IsOpen())
	{
		rs.Close();
	}
}

void CAlarmDlg::ReadModeInfoFromDB()
{
	CDaoRecordset rs(&db);
	try
	{
		rs.Open( dbOpenTable,"plan");
		while( !rs.IsEOF( ) )
		{
			COleVariant var;			
			var = rs.GetFieldValue(5);
			message.m_nModeList.Add(var.intVal);
			rs.MoveNext( );
		}
	}
	catch( CDaoException* e )
	{
		e->Delete( );
	}

	if(rs.IsOpen())
	{
		rs.Close();
	}
}

void CAlarmDlg::ShutDownComputer()
{
	HANDLE hProcess,hToken;
	TOKEN_PRIVILEGES Privileges;
	LUID luid;
	hProcess=GetCurrentProcess();

	//Open Process Token
	OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGES,&hToken);
	Privileges.PrivilegeCount=1;
	LookupPrivilegeValue(NULL,SE_SHUTDOWN_NAME,&luid);
	Privileges.Privileges[0].Luid=luid;
	Privileges.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
	//Adjust the Privileges
	AdjustTokenPrivileges(hToken,FALSE,&Privileges,NULL,NULL,NULL);
	//shutdown
	//注意此处我设为强制关机,所以注意要保存当前数据
	ExitWindowsEx(EWX_SHUTDOWN|EWX_FORCE|EWX_POWEROFF,NULL);
}

void CAlarmDlg::OnAbout() 
{
	// TODO: Add your command handler code here
	CAboutDlg dlgAbout;	
	dlgAbout.DoModal();

}


void CAlarmDlg::OnSetTime() 
{
	// TODO: Add your command handler code here
	ShellExecute(this->GetSafeHwnd(),"OPEN","RUNDLL32","Shell32.dll,Control_RunDLL timedate.cpl",NULL,SW_SHOW);
}

void CAlarmDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	PostMessage(WM_NCLBUTTONDOWN,HTCAPTION,MAKELPARAM(point.x,point.y));
	CDialog::OnLButtonDown(nFlags, point);
}

void CAlarmDlg::OnRButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CMenu menu,*pSubMenu;
	menu.LoadMenu(IDR_MENU1);
	pSubMenu = menu.GetSubMenu(0);
	ClientToScreen(&point);
	pSubMenu->TrackPopupMenu(TPM_RIGHTBUTTON,point.x,point.y,this,0);

	CDialog::OnRButtonDown(nFlags, point);
}

void CAlarmDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(bWindowStatus)
	{
		ShowWindow(SW_HIDE);
		bWindowStatus = FALSE;
	}
	else
	{
		ShowWindow(SW_SHOW);
		bWindowStatus = TRUE;
	}
	CDialog::OnLButtonDblClk(nFlags, point);
}

void CAlarmDlg::OnShowHideWindow() 
{
	// TODO: Add your command handler code here
	if(bWindowStatus)
	{
		ShowWindow(SW_HIDE);
		bWindowStatus = FALSE;
	}
	else
	{
		ShowWindow(SW_SHOW);
		bWindowStatus = TRUE;
	}
}

⌨️ 快捷键说明

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