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

📄 box8dlg.cpp

📁 自制 拼图游戏 mfc程序 9格子拼图 可打开外部图片进行游戏 支持bmp,jpg,gif格式 记录最近打开的问件 上方有菜单
💻 CPP
字号:
// box8Dlg.cpp : implementation file
//

#include "stdafx.h"
#include<list>
#include<algorithm>
using namespace std;

#include"Picture.h"
#include "box8.h"
#include "box8Dlg.h"


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

const UINT IMG_PATH_LIST_COMMAND=2000;
const UINT IMG_PATH_LIST_COMMAND_TO=IMG_PATH_LIST_COMMAND-1+20;
/////////////////////////////////////////////////////////////////////////////
// CBox8Dlg dialog

CBox8Dlg::CBox8Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBox8Dlg::IDD, pParent)
{
	this->Load();
	//{{AFX_DATA_INIT(CBox8Dlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CBox8Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBox8Dlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBox8Dlg, CDialog)
	//{{AFX_MSG_MAP(CBox8Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_COMMAND(ID_MENUITEMNEW, OnMenuitemNew)
	ON_COMMAND(ID_MENUITEMQUIT, OnMenuitemQuit)
	ON_COMMAND(ID_MENUITEMBACK, OnMenuitemBack)
	ON_COMMAND(ID_MENUITEMOPEN, OnMenuitemOpen)
	ON_COMMAND(ID_MENUITEMClearPath, OnMenuitemClearPath)
	ON_WM_KEYUP()
	//}}AFX_MSG_MAP
	ON_COMMAND_EX_RANGE(IMG_PATH_LIST_COMMAND,IMG_PATH_LIST_COMMAND_TO, OnMenuitemIL)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBox8Dlg message handlers

BOOL CBox8Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	CDC *pDC=this->GetDC();
	mDC.CreateCompatibleDC(pDC);
	this->ReleaseDC(pDC);
	
	if(!imgPathList.empty())
		this->LoadImage(imgPathList.front());
	

		
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// If you add a minimize button to your dialog, you will need the code below
//  to OnDraw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
void CBox8Dlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	this->clickBoxIndex=GetClickIndex(point);		
			
	CDialog::OnLButtonDown(nFlags, point);
}
void CBox8Dlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	clickBoxIndex=GetClickIndex(point);
	if(clickBoxIndex!=this->clickBoxIndex)
		return;
	if(boxOder.Move(clickBoxIndex))
	{
		OnDraw(GetDC());
		UpdateMenuBackItem();
	}
	CDialog::OnLButtonUp(nFlags, point);
}
void CBox8Dlg::OnPaint() 
{
	CPaintDC dc(this);
	CDC *pDC=&dc;
	this->OnDraw(pDC);
	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;

		// OnDraw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CBox8Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CBox8Dlg::OnDraw(CDC *pDC)
{
	//pDC->SetBkMode(TRANSPARENT);
	CBrush brush(RGB(0,0,0));
	CFont *pFont=GetFont();
	pDC->SelectObject(&brush);
	pDC->SelectObject(pFont);

	//画方块
	if(!boxOder.IsWin())
	{
		for(int i=0;i<9;i++)
		{
			int partAtI=i;
			partAtI=this->boxOder[i];
			if(partAtI==0)
			{
				pDC->Rectangle(rectABox[i]);
				continue;
			}
			else
				pDC->BitBlt(rectABox[i].left,rectABox[i].top,rectABox[i].Width(),rectABox[i].Height(),
					&mDC,rectABox[partAtI-1].left,rectABox[partAtI-1].top,SRCCOPY);
			if(showBoxNum)
			{
				CString num;num.Format("%d",partAtI);
				pDC->DrawText(num,rectABox[i],0);
			}
		}		
		/*for(i=0;i<4;i++)
		{
			pDC->MoveTo(0,bmpHeigth/3*i);
			pDC->LineTo(bmpWidth,bmpHeigth/3*i);
			pDC->MoveTo(bmpWidth/3*i,0);
			pDC->LineTo(bmpWidth/3*i,bmpHeigth);
		}*/
	}
	//如果赢了 直接贴图片
	else
	{
		pDC->BitBlt(0,0,this->bmpWidth,this->bmpHeigth,&mDC,0,0,SRCCOPY);
		//pDC->StretchBlt(0,0,bmpWidth/2,bmpHeigth,&mDC,0,0,bmpWidth,bmpHeigth,SRCCOPY);
	}
}
void CBox8Dlg::OnMenuitemNew() 
{
	this->boxOder.ReSet();
	//this->InvalidateRect(this->rectABox[boxOder.blankBoxIndex]);
	UpdateMenuBackItem();
	OnDraw(GetDC());
}
void CBox8Dlg::OnMenuitemQuit() 
{
	this->PostMessage(WM_QUIT);
}

void CBox8Dlg::OnMenuitemBack() 
{
	if(boxOder.Back())
	{
		OnDraw(GetDC());
		UpdateMenuBackItem();
	}
}
inline void CBox8Dlg::UpdateMenuBackItem()
{
	if(this->boxOder.IsBackAble())
		this->GetMenu()->EnableMenuItem(ID_MENUITEMBACK,MF_BYCOMMAND |MF_ENABLED );
	else
		this->GetMenu()->EnableMenuItem(ID_MENUITEMBACK,MF_BYCOMMAND |MF_GRAYED   );
	this->DrawMenuBar();
}
void CBox8Dlg::UpdateMenuPLItem()
{

	CMenu* pMenu=this->GetMenu();
	pMenu->DeleteMenu(1,MF_BYPOSITION);

	if(imgPathList.size())
	{
		CMenu pathMenu;pathMenu.CreatePopupMenu();
		int t=0;
		for(list<CString>::iterator i=imgPathList.begin();i!=imgPathList.end();i++)
		{
			pathMenu.AppendMenu(MF_BYPOSITION,IMG_PATH_LIST_COMMAND+t++,*i);
		}
		pMenu->AppendMenu(MF_POPUP,(UINT)pathMenu.GetSafeHmenu(),"最近文件");
	}
	this->DrawMenuBar();
}
void CBox8Dlg::OnMenuitemOpen() 
{
	CFileDialog fileDlg(TRUE);
	fileDlg.m_ofn.lpstrTitle="图像位置";
	fileDlg.m_ofn.lpstrFilter="图像文件(*.bmp;*.jpg;*.gif)\0*.bmp;*.jpg;*.gif;cov001.pdg\0"
		                     "所有文件(*.*)\0*.*\0\0";

	if(IDOK!=fileDlg.DoModal()) {return ;}
	CString filePath=fileDlg.GetPathName();

	imgPathList.remove(filePath);
	imgPathList.push_front(filePath);	
	this->LoadImage(filePath);
}
void CBox8Dlg::OnMenuitemClearPath() 
{
	imgPathList.clear();
	Store();
	this->UpdateMenuPLItem();
}
/**
	装载图像并显示
*/
void CBox8Dlg::LoadImage(CString path,BOOL bResize) 
{	
	boxOder.ReSet();
	//CPicture pic;
	if(!pic.Load(path))
	{	MessageBox("不支持的文件格式");return;}
	this->Store();
	//取得图像大小
	CSize size=pic.GetImageSize();
	bmpWidth=size.cx;
	bmpHeigth=size.cy;

	int maxW;
	int maxH;

	CWnd *pDesk=this->GetDesktopWindow();
	CRect rcDesk;
	pDesk->GetClientRect(rcDesk);
	int screenW=rcDesk.Width(),screenH=rcDesk.Height();  //=1280=800

	static int perW;static int perH;
/*	if(bResize)
	{
		CRect rcWnd;
		GetClientRect(rcWnd);
		int sizeW=rcWnd.Width();int sizeH=rcWnd.Height();

		afxDump<<perW<<" "<<sizeW;
		if(perW<sizeW && perH==sizeH)
		{	maxW=rcWnd.Width();maxH=screenW;
		}
		else if(perW==sizeW && perH<sizeH)
		{	maxW=screenW;maxH=rcWnd.Height();
		}
		else
		{	maxW=rcWnd.Width();maxH=rcWnd.Height();
		}
	}
	else
	*/
	{
		// 按屏幕大小缩放
		maxW=screenW;maxH=screenH;     
	}
	if(bmpWidth>maxW)
	{
		bmpHeigth=bmpHeigth* maxW/bmpWidth ;
		bmpWidth=maxW;		
	}
	if(bmpHeigth>maxH)
	{
		bmpWidth=bmpWidth* maxH/bmpHeigth ;
		bmpHeigth=maxH;		
	}


	CBitmap bmp;
	bmp.CreateCompatibleBitmap(this->GetDC(),bmpWidth,bmpHeigth);
	mDC.SelectObject(&bmp);
	pic.Render(&mDC,CRect(0,0,bmpWidth,bmpHeigth));
	//mDC.StretchBlt(0,0,bmpWidth,bmpHeigth,&mDC,0,0,bmpHeigth,bmpHeigth,SRCCOPY);
	for(int i=0;i<9;i++)
	{
		int x=i%3* int(bmpWidth/3);
		int y=int(i/3)* int(bmpHeigth/3);
		rectABox[i].SetRect(x,y,x+bmpWidth/3,y+bmpHeigth/3);
	}

	const UINT titleHeight=45;
	perW=bmpWidth+2;
	perH=bmpHeigth-2;
//	bOnSize=false;
	this->SetWindowPos(0,0,0,bmpWidth+6,bmpHeigth+titleHeight,SWP_NOMOVE);
	//this->CenterWindow();
	this->OnDraw(GetDC());
	UpdateMenuPLItem();
}


void CBox8Dlg::Serialize(CArchive& ar) 
{
	if (ar.IsStoring())
	{	// storing code
		ar<<imgPathList.size();
		for(list<CString>::iterator i=imgPathList.begin();i!=imgPathList.end();i++)
		{
			ar<<*i;
		}		
	}
	else
	{	// loading code
		int size;
		ar>>size;
		for(int i=0;i<size;i++)
		{
			CString readed;
			ar>>readed;
			imgPathList.push_back(readed);
		}
	}
}
CString GetDirectory()
{
	char f[500];
	GetModuleFileName(NULL,f,500);
	CString ret=f;
	int i=ret.ReverseFind('\\');
	return ret.Left(i+1);
}
void CBox8Dlg::Store()
{
	
	CString &file=GetDirectory()+CString("box8.dat");

	CFile f(file,CFile::modeWrite|CFile::modeCreate);
	CArchive ar(&f,CArchive::store);
	this->Serialize(ar);
}

BOOL CBox8Dlg::Load()
{
	CString &file=GetDirectory()+CString("box8.dat");
	CFile f(file,CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate);
	if(f.GetLength()==0)
		return false;
	CArchive ar(&f,CArchive::load);
	this->Serialize(ar);
	return true;
}
/**
	打开最近文件
*/
void CBox8Dlg::OnMenuitemIL(UINT id)
{
	list<CString>::iterator iter=imgPathList.begin();
	int i=0;
	while(i++<id-IMG_PATH_LIST_COMMAND)
		iter++;
	CString path=*iter;

	imgPathList.erase(iter);
	imgPathList.push_front(path);
	Store();
	if(boxOder.IsWin())
		boxOder.ReSet();
	LoadImage(path);
}
void CBox8Dlg::OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	if(nChar==VK_UP||nChar==VK_DOWN||nChar==VK_LEFT||nChar==VK_RIGHT)
		if(boxOder.MoveByDir(nChar))
			OnDraw(GetDC());
	CDialog::OnKeyUp(nChar, nRepCnt, nFlags);
}

//DEL void CBox8Dlg::OnSize(UINT nType, int cx, int cy) 
//DEL {
//DEL 	if( bOnSize)
//DEL 	{
//DEL 		if(!imgPathList.empty())
//DEL 			this->LoadImage(imgPathList.front(),true);
//DEL 	}
//DEL 	else bOnSize=true;
//DEL }


⌨️ 快捷键说明

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