// MainWnd.cpp : implementation file
//

#include "stdafx.h"
#include "main.h"
#include "MainWnd.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainWnd dialog

CMainWnd::CMainWnd(CWnd* pParent /*=NULL*/)
	: CDialog(CMainWnd::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMainWnd)
	//}}AFX_DATA_INIT
	SetDisplayScreen();
	strChapNum = "1";
	cout.clip(2);
	cout.setSpaceWidth(0);
	cout.SupportBmp();
	cout.setTextColor(RGB(230,255,255));
	cout.setTextBkMode(TRANSPARENT);
	reader.readFile("intro\\intro.txt");
}


void CMainWnd::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMainWnd)
	DDX_Control(pDX, IDC_BUTTON11, m_button11);
	DDX_Control(pDX, IDC_BUTTON10, m_button10);
	DDX_Control(pDX, IDC_BUTTON_TV4, m_buttonTV4);
	DDX_Control(pDX, IDC_BUTTON_TV3, m_buttonTV3);
	DDX_Control(pDX, IDC_BUTTON_TV2, m_buttonTV2);
	DDX_Control(pDX, IDC_BUTTON_TV1, m_buttonTV1);
	DDX_Control(pDX, IDC_BUTTON9, m_button9);
	DDX_Control(pDX, IDC_BUTTON8, m_button8);
	DDX_Control(pDX, IDC_BUTTON7, m_button7);
	DDX_Control(pDX, IDC_BUTTON6, m_button6);
	DDX_Control(pDX, IDC_BUTTON5, m_button5);
	DDX_Control(pDX, IDC_BUTTON4, m_button4);
	DDX_Control(pDX, IDC_BUTTON3, m_button3);
	DDX_Control(pDX, IDC_BUTTON1, m_button1);
	DDX_Control(pDX, IDC_BUTTON2, m_button2);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CMainWnd, CDialog)
	//{{AFX_MSG_MAP(CMainWnd)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_BUTTON_TV1, OnButtonTv1)
	ON_BN_CLICKED(IDC_BUTTON_TV2, OnButtonTv2)
	ON_BN_CLICKED(IDC_BUTTON_TV3, OnButtonTv3)
	ON_BN_CLICKED(IDC_BUTTON_TV4, OnButtonTv4)
	ON_BN_CLICKED(IDC_BUTTON9, OnButton9)
	ON_BN_CLICKED(IDC_BUTTON11, OnButton11)
	ON_WM_CHAR()
	ON_WM_KEYDOWN()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON10, OnButton10)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
	ON_BN_CLICKED(IDC_BUTTON6, OnButton6)
	ON_BN_CLICKED(IDC_BUTTON7, OnButton7)
	ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainWnd message handlers

void CMainWnd::DisplayBitmap(UINT ID, int x, int y)
{
	CBitmap Bitmap;
	Bitmap.LoadBitmap(ID);
	CDC MemDC;
	MemDC.CreateCompatibleDC(NULL);
	MemDC.SelectObject(&Bitmap);
	BITMAP BM;
	Bitmap.GetObject(sizeof(BM),&BM);
	CDC *pDC = GetDC();
	pDC->BitBlt(x,y,BM.bmWidth,BM.bmHeight,&MemDC,0,0,SRCCOPY);
	ReleaseDC(pDC);
}

void CMainWnd::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	DisplayBitmap(IDB_BITMAP_MAIN,0,0);
	InvalidateCnotrols();
	SetNewBackGround();
	cout.setDC(&dc);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	// Do not call CDialog::OnPaint() for painting messages
}

void CMainWnd::InvalidateCnotrols()
{
	m_button1.Invalidate();
	m_button2.Invalidate();
	m_button3.Invalidate();
	m_button4.Invalidate();
	m_button5.Invalidate();
	m_button6.Invalidate();
	m_button7.Invalidate();
	m_button8.Invalidate();
	m_button9.Invalidate();
	m_button10.Invalidate();
	m_button11.Invalidate();
	m_buttonTV1.Invalidate();
	m_buttonTV2.Invalidate();
	m_buttonTV3.Invalidate();
	m_buttonTV4.Invalidate();
}

void CMainWnd::OnButtonTv1() 
{
	// TODO: Add your control notification handler code here
	::WinExec("bin\\choose.exe "+strChapNum,SW_NORMAL);
	
}

void CMainWnd::OnButtonTv2() 
{
	// TODO: Add your control notification handler code here
	::WinExec("bin\\fillblank.exe "+strChapNum,SW_NORMAL);
	
}

void CMainWnd::OnButtonTv3() 
{
	// TODO: Add your control notification handler code here
	::WinExec("bin\\program.exe "+strChapNum,SW_NORMAL);
	
}

void CMainWnd::OnButtonTv4() 
{
	// TODO: Add your control notification handler code here
	::WinExec("bin\\usage.exe "+strChapNum,SW_NORMAL);
}

void CMainWnd::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnChar(nChar, nRepCnt, nFlags);
}

void CMainWnd::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CDialog::OnKeyDown(nChar, nRepCnt, nFlags);
}

BOOL CMainWnd::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	BOOL rt;
	if(pMsg->message == WM_KEYDOWN)
	{
//		CString s;
//		s.Format("%d",pMsg->wParam);
//		MessageBox(s);
		if((pMsg->wParam != 27)&&(pMsg->wParam != 13))
			rt = CDialog::PreTranslateMessage(pMsg);
	}
	else
	{
		rt = CDialog::PreTranslateMessage(pMsg);
	}
	return rt;
}

void CMainWnd::OnClose()
{
	// TODO: Add your message handler code here and/or call default
	//防止Alt+F4
//	CDialog::OnClose();
}

void CMainWnd::OnButton1() //"绪论";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "1";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton2() //"线性表";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "2";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton3() //"栈和队列";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "3";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton4() //"串";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "4";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton5() //"数组";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "5";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton6() //"树和二叉树";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "6";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton7() //"图";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "7";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton8() //"查找";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "8";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}


void CMainWnd::OnButton9() //"排序";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "9";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton10() //"帮助";
{
	// TODO: Add your control notification handler code here
	SetNewBackGround();
	strChapNum = "10";
	CDC *pDC = GetDC();
	cout.setDC(pDC);
	reader.getText(strChapNum,"1","1");
	cout<<reader.text.problem;
	ReleaseDC(pDC);
}

void CMainWnd::OnButton11() 
{
	// TODO: Add your control notification handler code here
	ChangeDisplaySettings(NULL,0);
	::ShowWindow(::FindWindow("Shell_TrayWnd",NULL),SW_SHOW);
	GetParent()->PostMessage(WM_CLOSE);
}

void CMainWnd::SetNewBackGround()
{
	cout.setStart(30,40);
	DisplayBitmap(IDB_BT_MAIN_COVER,20,20);
}

void CMainWnd::SetDisplayScreen()
{
	DEVMODE *pMode = new DEVMODE;
	int result = EnumDisplaySettings(NULL,0,pMode);
	for(int i=1;result;i++)
	{
		if((pMode->dmPelsWidth == 800) && (pMode->dmPelsHeight == 600))
		{
			if((pMode->dmBitsPerPel == 32) || (pMode->dmBitsPerPel == 24))
			{
				pMode->dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
				ChangeDisplaySettings(pMode,0);
				//::MessageBox(NULL,"ok","",MB_OK);
				break;
			}
		}
		result = EnumDisplaySettings(NULL,i,pMode);
	}
	::ShowWindow(::FindWindow("Shell_TrayWnd",NULL),SW_HIDE);

}

BOOL CMainWnd::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetWindowPos(NULL,0,0,800,600,SWP_SHOWWINDOW);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}
