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

📄 maindlg.cpp

📁 一个帮助你学习英语的软件~~很不错的咯~~ 对功能又做了改进~大家支持下哈~
💻 CPP
字号:
// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
//PK 2006/10/10 - 2007/03/07

#include "stdafx.h"
#include "resource.h"
#include "aboutdlg.h"
#include "CAddDlg.h"
#include "MainDlg.h"
#include "Item.h"
#include "Book.h"
#include "BooksDlg.h"

BOOL CMainDlg::PreTranslateMessage(MSG* pMsg)
{
	return CWindow::IsDialogMessage(pMsg);
}
BOOL CMainDlg::OnIdle()
{
	return FALSE;
}
LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{	//PK 2006/10/10 - 2007/03/07
	// center the dialog on the screen
	CenterWindow();

	// set icons
	HICON hIcon = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXICON), ::GetSystemMetrics(SM_CYICON), LR_DEFAULTCOLOR);
	SetIcon(hIcon, TRUE);
	HICON hIconSmall = (HICON)::LoadImage(_Module.GetResourceInstance(), MAKEINTRESOURCE(IDR_MAINFRAME), 
		IMAGE_ICON, ::GetSystemMetrics(SM_CXSMICON), ::GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR);
	SetIcon(hIconSmall, FALSE);

	// register object for message filtering and idle updates
	CMessageLoop* pLoop = _Module.GetMessageLoop();
	ATLASSERT(pLoop != NULL);
	pLoop->AddMessageFilter(this);
	pLoop->AddIdleHandler(this);

	UIAddChildWindowContainer(m_hWnd);

	_repository.load();

	//PK initialize the fonts
	string fontname = "Arial";
	int size = 15;
	g_config.font(string("list_item"), fontname, size);
	_ft_list.CreateFont(size, 0, 0, 0, FW_NORMAL,
		FALSE, FALSE, 0, ANSI_CHARSET, OUT_OUTLINE_PRECIS, 
		CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, 
		DEFAULT_PITCH | FF_DONTCARE, fontname.c_str());
	g_config.font(string("contents"), fontname, size);
	_ft_view.CreateFont(size, 0, 0, 0, FW_NORMAL, 
		FALSE, FALSE, 0, ANSI_CHARSET, OUT_OUTLINE_PRECIS, 
		CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, 
		DEFAULT_PITCH | FF_DONTCARE, fontname.c_str());

	//PK initial the controls
	_list.SubclassWindow(GetDlgItem(IDC_LIST1));
	_edit_content = GetDlgItem(IDC_EDT_CONTENT);
	_btn_view = GetDlgItem(IDC_BTN_REVIEW);
	_btn_add = GetDlgItem(IDC_BTN_ADD);
	_btn_modify = GetDlgItem(IDC_BTN_MODIFY);
	_btn_delete = GetDlgItem(IDC_BTN_DELETE);
	_btn_book = GetDlgItem(IDC_BTN_BOOK);
	_btn_exit = GetDlgItem(IDCANCEL);
	_btn_about = GetDlgItem(ID_APP_ABOUT);

	_list.SetFont(_ft_list);
	_edit_content.SetFont(_ft_view);

	//PK set active books and records
	int amount = _repository.size();
	for (int i = 0; i < amount; ++i) {
		CBook * book = _repository.get_book(i);
		if (book->is_active()) _list.set_book(book);
	}
	if (!_list.init()) return false;

	MoveWindow(g_config._left, g_config._top, g_config._width, g_config._height);

	PostMessage(WM_FIRST_RUN);

	return TRUE;
}
void CMainDlg::OnSize(UINT flag, CSize size)
{	//PK 2006/11/02 - 09
	
	if (SIZE_MINIMIZED == flag) return;

	int gap = 2;

	//PK locate list
	CRect rect_list;
	_list.GetWindowRect(rect_list);
	int list_height = rect_list.Height();
	rect_list.top = rect_list.left = gap;
	rect_list.bottom = rect_list.top + list_height;
	rect_list.right = size.cx - gap;
	_list.MoveWindow(rect_list);
	
	//PK locate buttons
	CRect rect_btn;
	_btn_view.GetWindowRect(rect_btn);
	int btn_height = rect_btn.Height();
	int btn_width = rect_btn.Width();
	int btn_top = size.cy - rect_btn.Height() - gap;
	int btn_right = (size.cx - btn_width * 7 - gap * 6) / 2;
	rect_btn.MoveToXY(btn_right, btn_top);
	_btn_view.MoveWindow(rect_btn);
	btn_right += btn_width + gap;
	rect_btn.MoveToX(btn_right);
	_btn_add.MoveWindow(rect_btn);
	btn_right += btn_width + gap;
	rect_btn.MoveToX(btn_right);
	_btn_modify.MoveWindow(rect_btn);
	btn_right += btn_width + gap;
	rect_btn.MoveToX(btn_right);
	_btn_delete.MoveWindow(rect_btn);
	btn_right += btn_width + gap;
	rect_btn.MoveToX(btn_right);
	_btn_book.MoveWindow(rect_btn);
	btn_right += btn_width + gap;
	rect_btn.MoveToX(btn_right);
	_btn_exit.MoveWindow(rect_btn);
	btn_right += btn_width + gap;
	rect_btn.MoveToX(btn_right);
	_btn_about.MoveWindow(rect_btn);

	//PK locate edit
	CRect rect_content;
	rect_content.top = list_height + gap * 2;
	rect_content.left = 2;
	rect_content.bottom = size.cy - btn_height - gap * 2;
	rect_content.right = size.cx - 2;
	_edit_content.MoveWindow(rect_content);

	//PK save the size and location of window
	CRect rect_window;
	GetWindowRect(rect_window);
	if ((g_config._top == rect_window.top) &&
		(g_config._left == rect_window.left) &&
		(g_config._width == rect_window.Width()) &&
		(g_config._height == rect_window.Height()))
	return;

	g_config._top = rect_window.top;
	g_config._left = rect_window.left;
	g_config._width = rect_window.Width();
	g_config._height = rect_window.Height();
	g_config._is_dirty = true;
}
void CMainDlg::OnMove(CPoint ps)
{	//PK 2006/11/09 - 11/30
	//PK first time this method will be evoked before the evokation of MoveWindow in OnInitDialog method
	static bool is_first_time = true;
	if (is_first_time) {
		is_first_time = false;
		return;
	}
	CRect rect_window;
	GetWindowRect(rect_window);
	if ((rect_window.left != g_config._left) || (rect_window.top != g_config._top)) {
		g_config._top = rect_window.top;
		g_config._left = rect_window.left;
		g_config._is_dirty = true;
	}
}
LRESULT CMainDlg::OnFirstRun(UINT, WPARAM, LPARAM, BOOL&)
{	//PK 2007/02/26 - 03/10
	stringstream prompt;
	
	//PK if there are no books, prompt user load some books
	if (0 == _repository.size()) {
		prompt << "There are no any books." << std::endl
		 		 << "Please copy some books in \"repository\" directory." << std::endl;
		MessageBox(prompt.str().c_str(), "No books!", MB_OK | MB_ICONINFORMATION);
		return 0;
	}

	//PK if there are no active books, prompt user to select some books.
	if (0 == g_config.active_books_size()) {
		prompt << "There are no any active books." << std::endl
				 << "Please push \"Books\" button, and select some books you want to study." << std::endl;
		MessageBox(prompt.str().c_str(), "No active books!", MB_OK | MB_ICONINFORMATION);
	}

	//PK verify if every book's recorders and items are matched, if not, prompt whether trim the recorders
	int amount = _repository.size();
	for (int i = 0; i < amount; ++i) {
		CBook * book = _repository.get_book(i);
		if (!book->is_recorders_matched()) {
			prompt << "Some records' relative items in book \""
					 << book->book_name() << "\" disappeared! "
					 << "That's because of moving items manually or changing item's title. "
					 << "Changing back the item's title can restore item's record." << std::endl << std::endl
					 << "Or do you want to delete these records?";
			int res = MessageBox(prompt.str().c_str(), "Records not matched!", MB_YESNO | MB_ICONQUESTION);
			if (res == IDYES) book->trim_recorders();
		}
	}
	return 0;
}
LRESULT CMainDlg::OnAppAbout(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CAboutDlg dlg;
	dlg.DoModal();
	return 0;
}
LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
	CloseDialog(wID);
	return 0;
}
LRESULT CMainDlg::OnReview(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{	//PK 2006/10/15 - 2007/02/19
	int index = _list.GetSelectedIndex();
	if (index < 0) return 0;
	CItem * item = _list.get_current_item();

	int res = MessageBox("Are you sure?", "Have reviewed this item?", MB_YESNO);
	if (res = IDYES) {
		date last_day = item->access_date();
		if (last_day.is_not_a_date()) item->access_date(day_clock::local_day());
		else {
			last_day = item->next_day();
			if (last_day > day_clock::local_day()) item->access_date(day_clock::local_day());
			else item->access_date(last_day);
		}
		item->update();
		_list.update_an_item(index, item);
		_list.sort_list();
	}
	return 0;
}
LRESULT CMainDlg::OnAddItem(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{	//PK 2006/10/10 - 2007/03/08
	CAddDlg dlg(_repository);
//	dlg.list(&_list);
	dlg.DoModal();
	_list.sort_list();	
	return 0;
}
LRESULT CMainDlg::OnModify(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{	//PK 2006/10/15 - 2007/03/08
	int index = _list.GetSelectedIndex();
	if (-1 == index) {
		MessageBox("Please select the item you want to modify!", "No selected item!", MB_OK | MB_ICONWARNING);
		return 0;
	}
	CItem * item = _list.get_current_item();
	string title = item->title();
	CAddDlg dlg(_repository);
	dlg.item(item);
	dlg.status(CAddDlg::MODIFY);
	WORD id = dlg.DoModal();
	if (IDC_BTN_DLGADD_OK == id) {
		if (title != item->title()) {
		//PK if title changed, we must update the relative record info, because they are connected through title
			item->title_changed(title);
		}
		_list.update_an_item(index, item);
		_list.sort_list();
		item->book()->dirty();
	}
	return 0;
}
LRESULT CMainDlg::OnDelete(WORD , WORD wID, HWND , BOOL&)
{	//PK 2006/10/17 - 2007/03/10
	int index = _list.GetSelectedIndex();
	if (index < 0) {
		MessageBox("Please select the item you want to modify!", "No selected item!", MB_OK | MB_ICONWARNING);
		return 0;
	}
	CItem * item = _list.get_current_item();
	int res = MessageBox("Do you really want to delete this item?\nThis action can't undo!", "Confirm deleting", MB_YESNO | MB_ICONWARNING);
	if (IDYES == res) item->book()->delete_item(item);
	_list.CListViewCtrl::DeleteItem(index);
	return 0;
}
void CMainDlg::CloseDialog(int nVal)
{
	DestroyWindow();
	::PostQuitMessage(nVal);
}

LRESULT CMainDlg::OnClickList(LPNMHDR hdr)
{	//PK 2006/10/15
	CItem * item = _list.get_current_item();
	if (NULL == item) return -1;

	CEdit content = GetDlgItem(IDC_EDT_CONTENT);
	string article = item->get_article();
	content.SetWindowText(article.c_str());

	return 0;
}

LRESULT CMainDlg::OnDBClickList(LPNMHDR hdr)
{	//PK 2006/10/15
	int index = _list.GetSelectedIndex();
	return 0;
}
LRESULT CMainDlg::OnBook(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{	//PK 2007/03/05
	CBooksDlg dlg(_repository);
	dlg.DoModal();
	return 0;
}
LRESULT CMainDlg::OnBooknameModified(UINT, WPARAM book, LPARAM, BOOL&)
{	//PK 2007/03/07
	CBook * pbook = (CBook *)book;
	_list.update_book_name(pbook);
	return 0;
}
LRESULT CMainDlg::OnBookDelete(UINT, WPARAM book, LPARAM, BOOL&)
{	//PK 2007/03/07
	CBook * pbook = (CBook *)book;
	_list.delete_book(pbook);
	return 0;
}
LRESULT CMainDlg::OnBookAdd(UINT, WPARAM book, LPARAM, BOOL&)
{	//PK 2007/03/07
	CBook * pbook = (CBook *)book;
	_list.add_book(pbook);
	_list.sort_list();
	return 0;
}
LRESULT CMainDlg::OnItemAdd(UINT, WPARAM item, LPARAM, BOOL&)
{	//PK 2007/03/08
	CItem * pitem = (CItem *)item;
	_list.add_an_item(pitem);
	return 0;
}

⌨️ 快捷键说明

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