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

📄 towerdlg.cpp

📁 自制 汉诺塔 游戏 mfc程序 程序非常简单 适合初学
💻 CPP
字号:
// towerDlg.cpp : implementation file
//

#include "stdafx.h"
#include "tower.h"
#include "towerDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CTowerDlg dialog

CTowerDlg::CTowerDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CTowerDlg::IDD, pParent)
{
	stepout=0;
	//{{AFX_DATA_INIT(CTowerDlg)
	//}}AFX_DATA_INIT
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CTowerDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CTowerDlg)
	DDX_Control(pDX, IDC_BUTTON4, btnbac);
	DDX_Control(pDX, IDC_SPIN1, spin);
	DDX_Control(pDX, IDC_EDIT1, edit);
	DDX_Control(pDX, IDC_OUT, out);
	DDX_Control(pDX, IDC_BUTTON3, btnc);
	DDX_Control(pDX, IDC_BUTTON2, btnb);
	DDX_Control(pDX, IDC_BUTTON1, btna);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CTowerDlg, CDialog)
	//{{AFX_MSG_MAP(CTowerDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButtonA)
	ON_BN_CLICKED(IDC_BUTTON2, OnButtonB)
	ON_BN_CLICKED(IDC_BUTTON3, OnButtonC)
	ON_BN_CLICKED(IDOK, OnNew)
	ON_BN_CLICKED(IDC_BUTTON4, OnBac)
	ON_WM_LBUTTONDOWN()
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTowerDlg message handlers

BOOL CTowerDlg::OnInitDialog()
{	
	CDialog::OnInitDialog();
	
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	SetDlgItemInt(IDC_EDIT1,3);
	OnNew();
	edit.SetLimitText(1);
//	CFont font;
//	font.CreatePointFont(500,"System");
//	edit.SetFont(&font,NULL);
	spin.SetRange(3,7);
	spin.SetPos(3);

	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 draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.
void CTowerDlg::PaintTower(CPaintDC &dc, int idc, int a[])
{
	CRect rect;
	this->GetDlgItem(idc)->GetWindowRect(rect);   
	ScreenToClient(rect);
	rect.top=rect.bottom-10;
	int fillnum=0;
	for(int i=floor;i>=1;i--)
	{
		if(a[i]==0) continue;
		CRect one=rect;
		one.left-=i*10/2;one.right+=i*10/2; 
		rect.OffsetRect(0,-10);
		dc.FillRect(one,&CBrush(RGB(0x77,0x77,0x77)));
		fillnum++;
		if(fillnum==a[0]&&set==a)
			dc.FillRect(one,&CBrush(RGB(0,0,0)));
	}
}
void CTowerDlg::OnPaint() 
{
	CPaintDC dc(this);
	PaintTower(dc,IDC_A,a);
	PaintTower(dc,IDC_B,b);
	PaintTower(dc,IDC_C,c);
	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
	{
		CDialog::OnPaint();
	}
}

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

void CTowerDlg::SetFloor(int floor)
{
	setbefore=set=NULL;
	this->floor=floor;
	a[0]=floor;
	b[0]=c[0]=0;
	for(int i=1;i<=floor;i++)
	{
		a[i]=1;
		b[i]=c[i]=0;
	}
}

	

void CTowerDlg::OnButtonA() 
{
	OnButton(a);	
}
void CTowerDlg::OnButtonB() 
{
	OnButton(b);
	
}
void CTowerDlg::OnButtonC() 
{
	OnButton(c);	
}

void CTowerDlg::OnButton(int *i)
{
	set=i;
	CRect top;
	if(setbefore==NULL){
		setbefore=set;
		top=GetTopRect(set);
	}
	else if(setbefore==set) {
		top=GetTopRect(set);
		setbefore=set=NULL;
	}
	else{
		Move(set,setbefore);
		setbefore=set=NULL;
		IsWin();
		btnbac.EnableWindow(1);
		return;
	}
	InvalidateRect(top);
}

void CTowerDlg::Move(int *set, int *setbefore)
{
	setbac1=setbefore;setbac2=set;
	int i=1;
	while(!setbefore[i]==1) i++;
	CRect befrect=GetTopRect(setbefore);
	for(int j=1;j<=i;j++)
	{
		if(set[j]==1)
		{
			InvalidateRect(befrect);
			return;
		}
	}
	set[0]++;setbefore[0]--;
	set[i]=1;setbefore[i]=0;
	stepout++;
	SetDlgItemInt(IDC_STEPNUM,stepout);
	CRect rect=GetItemRect(IDC_STEPNUM);
	InvalidateRect(rect);	
	InvalidateRect(befrect);
	CRect setrect=GetTopRect(set);
	InvalidateRect(setrect);

}

void CTowerDlg::IsWin()
{
	if(c[0]!=floor) return;
	btna.EnableWindow(0);
	btnb.EnableWindow(0);
	btnc.EnableWindow(0);
	int lest=GetLestStep();
	char out[200];
	sprintf(out,"恭喜你,你赢了  你的步数是%d  最佳步数是%d",
				stepout,lest);
	this->out.SetWindowText(out);
}

void CTowerDlg::OnNew() 
{
	floor=GetDlgItemInt(IDC_EDIT1);
	SetFloor(floor);
	btna.EnableWindow(1);
	btnb.EnableWindow(1);
	btnc.EnableWindow(1);
	out.SetWindowText("");
	stepout=0;
	SetDlgItemInt(IDC_STEPNUM,stepout);
	btnbac.EnableWindow(0);
	this->Invalidate();
}



int CTowerDlg::GetLestStep()
{
	int ret=0;
	for(int i=0;i<floor;i++)
		ret=ret*2+1;
	return ret;
}

void CTowerDlg::OnBac() 
{
	stepout-=(1+1);   // move中加了一
	SetDlgItemInt(IDC_STEPNUM,stepout);
	Move(setbac1,setbac2);
	
	btnbac.EnableWindow(0);
}

CRect CTowerDlg::GetTopRect(int *tower)
{
	int idc;
	if(tower==a)
		idc=IDC_A;
	else if(tower==b)
		idc=IDC_B;
	else if(tower==c)
		idc=IDC_C;
	CRect rect;
	this->GetDlgItem(idc)->GetWindowRect(rect);   
	ScreenToClient(rect);
	rect.top=rect.bottom-10;
	rect.OffsetRect(0,-10*(tower[0]-1));
	int i=1;
	while(tower[i]!=1) i++;
	rect.left-=i*10/2;rect.right+=i*10/2; 
	return rect;
}

void CTowerDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CRect a,b,c;
	a=GetItemRect(IDC_AC);
	b=GetItemRect(IDC_BC);
	c=GetItemRect(IDC_CC);
	if(a.PtInRect(point))
		OnButtonA();
	else if(b.PtInRect(point))
		OnButtonB();
	else if(c.PtInRect(point))
		OnButtonC();
	CDialog::OnLButtonDown(nFlags, point);
}

CRect CTowerDlg::GetItemRect(int idc)
{
	CRect rect;
	this->GetDlgItem(idc)->GetWindowRect(rect);   
	ScreenToClient(rect);
	return rect;
}

HBRUSH CTowerDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	if   ( nCtlColor==CTLCOLOR_STATIC)   
	{   
		pDC->SetBkMode(TRANSPARENT); 
		return   (HBRUSH)::GetStockObject(HOLLOW_BRUSH);   
	} 
	// TODO: Return a different brush if the default is not desired
	return hbr;

}

⌨️ 快捷键说明

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