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

📄 pymap9dlg.cpp

📁 九宫 为初次学习游戏编程的朋友提供参考
💻 CPP
字号:
// pymap9Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "pymap9.h"
#include "pymap9Dlg.h"
#include "spell.h"

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


CSpell spell(3, 3, 2, 2);

//////////////////////////////////////////////
//
void ShowTranBmp(CDC * dc, CBitmap * bmp, COLORREF bgcolor, int x, int y, int w, int h, int bx, int by, int bw, int bh){

	CDC mdc;
	CDC tdc; // 图像
	CDC ddc; // 遮罩
	CBitmap tbmp;
	CBitmap dbmp;
	CBitmap *ob;
	CBitmap *tob;
	CBitmap *dob;

	mdc.CreateCompatibleDC(dc);
	tdc.CreateCompatibleDC(dc);
	ddc.CreateCompatibleDC(dc);

	tbmp.CreateBitmap(bw, bh, 1, 32, NULL);
	dbmp.CreateBitmap(bw, bh, 1, 1, NULL);

	ob = mdc.SelectObject(bmp);
	tob = tdc.SelectObject(&tbmp);
	dob = ddc.SelectObject(&dbmp);

	tdc.BitBlt(-bx, -by, bw+bx, bh+by, &mdc, 0, 0, SRCCOPY);

	tdc.SetBkColor(bgcolor);
	ddc.BitBlt(0, 0, bw, bh, &tdc, 0, 0, SRCCOPY);

	///////////////////////////////////
	//显示
	dc->BitBlt(x, y, w, h, &tdc, 0, 0, SRCINVERT);
	dc->BitBlt(x, y, w, h, &ddc, 0, 0, SRCAND);
	dc->BitBlt(x, y, w, h, &tdc, 0, 0, SRCINVERT);

	mdc.SelectObject(ob);
	tdc.SelectObject(tob);
	ddc.SelectObject(dob);
}

void ShowTranBmp(CDC * dc, CBitmap * bmp, COLORREF bgcolor, int x, int y, int w, int h){

	CDC tdc; // 图像
	CDC ddc; // 遮罩
	CBitmap dbmp;
	CBitmap *tob;
	CBitmap *dob;

	tdc.CreateCompatibleDC(dc);
	ddc.CreateCompatibleDC(dc);

	dbmp.CreateBitmap(w, h, 1, 1, NULL);

	tob = tdc.SelectObject(bmp);
	dob = ddc.SelectObject(&dbmp);

	tdc.SetBkColor(bgcolor);
	ddc.BitBlt(0, 0, w, h, &tdc, 0, 0, SRCCOPY);

	///////////////////////////////////
	//显示
	dc->BitBlt(x, y, w, h, &tdc, 0, 0, SRCINVERT);
	dc->BitBlt(x, y, w, h, &ddc, 0, 0, SRCAND);
	dc->BitBlt(x, y, w, h, &tdc, 0, 0, SRCINVERT);

	tdc.SelectObject(tob);
	ddc.SelectObject(dob);
}
/////////////////////////////////////////////////////////////////////////////
// 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)
	virtual BOOL OnInitDialog();
	afx_msg void OnPaint();
	//}}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
}

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

BOOL CAboutDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	SetDlgItemText(IDC_QQ, "252351107");
	SetDlgItemText(IDC_EMAIL, "_tg15@163.com");
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CAboutDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting

	CBitmap tg15;
	tg15.LoadBitmap(IDB_TG15);
	ShowTranBmp(&dc, &tg15, RGB(255,255,255), 8, 8, 93, 96);

	// Do not call CDialog::OnPaint() for painting messages
}
/////////////////////////////////////////////////////////////////////////////
// CMyDlg dialog

CMyDlg::CMyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMyDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMyDlg)
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	begin = TRUE;
	step = 0;
	pic.Load(IDR_PIC_VIEW);
	size = 3;
	border = TRUE;
//	m_pp5.SetText(_T("http://www.pinpin5.com"));
}

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

BEGIN_MESSAGE_MAP(CMyDlg, CDialog)
	//{{AFX_MSG_MAP(CMyDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_COMMAND(ID_MENU_EXIT, OnMenuExit)
	ON_COMMAND(ID_MENU_ABOUT, OnMenuAbout)
	ON_WM_LBUTTONDOWN()
	ON_COMMAND(ID_MENU_BEGIN, OnMenuBegin)
	ON_COMMAND(ID_MENU_NUMBER, OnMenuNumber)
	ON_COMMAND(ID_MENU_VIEW, OnMenuView)
	ON_COMMAND(ID_MENU_PERSON, OnMenuPerson)
	ON_COMMAND(ID_MENU_3, OnMenu3)
	ON_COMMAND(ID_MENU_4, OnMenu4)
	ON_COMMAND(ID_MENU_5, OnMenu5)
	ON_COMMAND(ID_MENU_6, OnMenu6)
	ON_COMMAND(ID_MENU_BORDER, OnMenuBorder)
	ON_COMMAND(ID_MENU_OPEN, OnMenuOpen)
	ON_WM_LBUTTONDBLCLK()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDlg message handlers

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

	// 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
	CMenu* pMenu = this->GetMenu();
	pMenu->CheckMenuItem(ID_MENU_3, MF_CHECKED);
	pMenu->CheckMenuItem(ID_MENU_BORDER, MF_CHECKED);
	pp5.m_link = _T("http://www.pinpin5.com");
	pp5.SubclassDlgItem(IDC_PP5, this);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMyDlg::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 CMyDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	if (IsIconic())
	{

		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
	{
		CDialog::OnPaint();
	}
	Show(&dc);
}

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

void CMyDlg::Show(CDC * dc){

	CDC tdc;
	CBitmap bmp, *obmp;

	bmp.LoadBitmap(IDB_BG);
	tdc.CreateCompatibleDC(dc);

	obmp = tdc.SelectObject(&bmp);

	CDC bmpdc, picdc;
	bmpdc.CreateCompatibleDC(dc);
	picdc.CreateCompatibleDC(dc);
	CBitmap bbmp, * obbmp;
	CBitmap pbmp, * opbmp;
	bbmp.CreateCompatibleBitmap(dc, 360, 360);
	pbmp.CreateCompatibleBitmap(dc, (360/size), (360/size));
	obbmp = bmpdc.SelectObject(&bbmp);
	opbmp = picdc.SelectObject(&pbmp);
	pic.Render(&bmpdc, CRect(0, 0, 360, 360));
	int i;
	if(border){
		//画边框
		CPen pen, * open;
		pen.CreatePen(PS_SOLID, 1, RGB(96, 96, 96));
		open = bmpdc.SelectObject(&pen);
		for(i = 0; i < size; i++){
			bmpdc.MoveTo(0, (360/size)*(i+1)-1);
			bmpdc.LineTo(359, (360/size)*(i+1)-1);
			bmpdc.MoveTo((360/size)*(i+1)-1, 0);
			bmpdc.LineTo((360/size)*(i+1)-1, 359);
		}
		bmpdc.SelectObject(open);
		pen.DeleteObject();
		pen.CreatePen(PS_SOLID, 1, RGB(240, 240, 240));
		open = bmpdc.SelectObject(&pen);
		for(i = 0; i < size; i++){
			bmpdc.MoveTo(0, (360/size)*i);
			bmpdc.LineTo(359, (360/size)*i);
			bmpdc.MoveTo((360/size)*i, 0);
			bmpdc.LineTo((360/size)*i, 359);
		}
		bmpdc.SelectObject(open);
	}

	obmp = tdc.SelectObject(&bmp);
	for(i = 0; i < size; i++){
		for(int j = 0; j < size; j++){
			int n = spell.GetPic(i, j);
			if(n != size*size-1){
				picdc.BitBlt(-(n%size)*(360/size), -(n/size)*(360/size), 360, 360, &bmpdc, 0, 0, SRCCOPY);
				tdc.BitBlt((360/size)*i+2, (360/size)*j+2, (360/size), (360/size), &picdc, 0, 0, SRCCOPY);
			}
		}
	}
	dc->BitBlt(10, 10, 364, 364, &tdc, 0, 0, SRCCOPY);
	pic.Render(&bmpdc, CRect(0, 0, 150, 150));
	dc->BitBlt(393, 45, 150, 150, &bmpdc, 0, 0, SRCCOPY);
	dc->MoveTo(392, 44);
	dc->LineTo(392, 194);
	dc->LineTo(542, 194);
	dc->LineTo(542, 44);
	dc->LineTo(392, 44);

	bmpdc.SelectObject(obbmp);
	picdc.SelectObject(opbmp);
	tdc.SelectObject(obmp);
}

void CMyDlg::OnMenuAbout() {
	// 关于对话框
	CAboutDlg dlgAbout;
	dlgAbout.DoModal();
}

void CMyDlg::OnMenuExit() {
	// 退出
	OnOK();
}

void CMyDlg::OnMenuBegin() {
	// 重新开始游戏
	spell.Init();
	step = 0;
	SetDlgItemText(IDC_STEP, "0");
	begin = TRUE;
	Invalidate(FALSE);
}

void CMyDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(begin){
		if(point.x > 12 && point.x < 372 && point.y > 12 && point.y < 372){
			int w = (point.x - 12) / (360/size);
			int h = (point.y - 12) / (360/size);
			int r = spell.Move(w, h);
			Invalidate(FALSE);
			if(0 <= r){
				step ++;
				CString str;
				str.Format("%d", step);
				SetDlgItemText(IDC_STEP, str);
			}
			if(1 == r){
				MessageBox("完成!");
				begin = FALSE;
			}
		}
	}

	CDialog::OnLButtonDown(nFlags, point);
}

void CMyDlg::OnMenuNumber() {
	// 选择数字
	pic.Load(IDR_PIC_NUMBER);
	Invalidate(FALSE);
}

void CMyDlg::OnMenuView() {
	// 选择风景
	pic.Load(IDR_PIC_VIEW);
	Invalidate(FALSE);
}

void CMyDlg::OnMenuPerson() {
	// 选择草莓
	pic.Load(IDR_PIC_CM);
	Invalidate(FALSE);
}


void CMyDlg::OnMenu3() {
	// 选择3*3的方块
	if(3 != size){
		CMenu* pMenu = this->GetMenu();
		pMenu->CheckMenuItem(ID_MENU_3, MF_CHECKED);
		pMenu->CheckMenuItem(ID_MENU_4, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_5, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_6, MF_UNCHECKED);
		spell.ReCreate(3, 3, 2, 2);
		size = 3;
		OnMenuBegin();
	}
}

void CMyDlg::OnMenu4() {
	// 选择4*4的方块
	if(4 != size){
		CMenu* pMenu = this->GetMenu();
		pMenu->CheckMenuItem(ID_MENU_4, MF_CHECKED);
		pMenu->CheckMenuItem(ID_MENU_3, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_5, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_6, MF_UNCHECKED);
		spell.ReCreate(4, 4, 3, 3);
		size = 4;
		OnMenuBegin();
	}
}

void CMyDlg::OnMenu5() {
	// 选择5*5的方块
	if(5 != size){
		CMenu* pMenu = this->GetMenu();
		pMenu->CheckMenuItem(ID_MENU_5, MF_CHECKED);
		pMenu->CheckMenuItem(ID_MENU_3, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_4, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_6, MF_UNCHECKED);
		spell.ReCreate(5, 5, 4, 4);
		size = 5;
		OnMenuBegin();
	}
}

void CMyDlg::OnMenu6() {
	// 选择5*5的方块
	if(6 != size){
		CMenu* pMenu = this->GetMenu();
		pMenu->CheckMenuItem(ID_MENU_6, MF_CHECKED);
		pMenu->CheckMenuItem(ID_MENU_3, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_4, MF_UNCHECKED);
		pMenu->CheckMenuItem(ID_MENU_5, MF_UNCHECKED);
		spell.ReCreate(6, 6, 5, 5);
		size = 6;
		OnMenuBegin();
	}
}


void CMyDlg::OnMenuBorder() {
	// 是否有边框
	border = !border;
	CMenu* pMenu = this->GetMenu();
	pMenu->CheckMenuItem(ID_MENU_BORDER, border?MF_CHECKED:MF_UNCHECKED);
	Invalidate(FALSE);
}

void CMyDlg::OnMenuOpen() {
	// 打开文件
	CFileDialog file(true, NULL, NULL, OFN_OVERWRITEPROMPT, "图片文件|*.bmp;*.jpg;*.gif|*.jpg|*.jpg|*.bmp|*.bmp|*.gif|*.gif|", NULL);
	if(file.DoModal()==IDOK){
		pic.Load(file.GetPathName());
		Invalidate(FALSE);
	}
}

⌨️ 快捷键说明

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