📄 towermfcdlg.cpp
字号:
// TowerMfcDlg.cpp : implementation file
//
#include "stdafx.h"
#include "TowerMfc.h"
#include "TowerMfcDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// 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)
//}}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)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTowerMfcDlg dialog
CTowerMfcDlg::CTowerMfcDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTowerMfcDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTowerMfcDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
floor=0; //初始化层数
i=0; //初始化下标
m_first=false; //初始化变量
}
void CTowerMfcDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTowerMfcDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTowerMfcDlg, CDialog)
//{{AFX_MSG_MAP(CTowerMfcDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_NEXT, OnNext)
ON_BN_CLICKED(IDC_FORWARD, OnForward)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTowerMfcDlg message handlers
BOOL CTowerMfcDlg::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
CInitDlg dlg;
dlg.DoModal();
m_tower_first.m_floor=floor; //初始化楼层
m_tower_first.m_add=WEIDTH/floor*1.0; //初始化增量
// m_tower_first.m_add=5;
CRect rect;
GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&rect);//获得第一个柱子的位置
ScreenToClient(&rect); //转化坐标系
int weidthtemp=WEIDTH;
for(int i=0;i<floor;i++) //初始化第一个柱子用于开始的时候显示
{
RECT temp;
temp.left=rect.left-weidthtemp;
temp.right=rect.right+weidthtemp;
temp.bottom=rect.bottom;
temp.top=rect.bottom-HEIGTH;
rect.bottom-=HEIGTH;
weidthtemp-=m_tower_first.m_add;
m_tower_first.allrect.push_back(temp);
}
/* CPoint currentpoint;
currentpoint.x=rect.left;
currentpoint.y=rect.bottom;
m_tower_first.GiveInitPer(WEIDTH,floor,WEIDTH/floor,currentpoint);*/
//创建线
hanoi(floor,1,2,3); //计算路径
static CString m_str;
CString str;
for(i=0;i<m_line.size();i++)
{
str.Format("%d,%d\r\n",m_line[i].a,m_line[i].b);
m_str+=str;
}
// MessageBox(m_str);
return TRUE; // return TRUE unless you set the focus to a control
}
void CTowerMfcDlg::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 CTowerMfcDlg::OnPaint()
{
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();
}
CClientDC dc(this);
//显示第一个塔
for(int i=0;i<m_tower_first.allrect.size();i++)
{
m_tower_first.DrawRectangle(dc.m_hDC,m_tower_first.allrect[i]);
}
//显示第二个塔
for( i=0;i<m_tower_second.allrect.size();i++)
{
m_tower_second.DrawRectangle(dc.m_hDC,m_tower_second.allrect[i]);
}
//显示第三个塔
for(i=0;i<m_tower_three.allrect.size();i++)
{
m_tower_three.DrawRectangle(dc.m_hDC,m_tower_three.allrect[i]);
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CTowerMfcDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CTowerMfcDlg::OnAdd()
{
// TODO: Add your control notification handler code her
}
void CTowerMfcDlg::OnDelete()
{
// TODO: Add your control notification handler code here
}
void CTowerMfcDlg::hanoi(int n,int a,int b,int c)
{
StartToEnd temp;
if(n==1)
{
// printf("%d->%d\n",a,c);
temp.a=a;
temp.b=c;
m_line.push_back(temp);
// m_line
}
else
{
hanoi(n-1,a,c,b);
// printf("%d->%d\n",a,c);
temp.a=a;
temp.b=c;
m_line.push_back(temp);
hanoi(n-1,b,a,c);
}
}
void CTowerMfcDlg::OnNext()
{
// TODO: Add your control notification handler code here
CRect rectout;
CRect rectin;
int left=0;
int right=0;
if(i<m_line.size())
{
switch(m_line[i].a)//出去
{
case 1:
{
CRect rect_first; //用于第一个柱子这层做两边离中间的宽度
GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&rect_first);//获得第一个柱子的位置
ScreenToClient(&rect_first); //转化坐标系
rectout=m_tower_first.allrect[m_tower_first.allrect.size()-1];//保存向量的最后一个值
m_tower_first.allrect.pop_back(); //出栈
left=rect_first.left-rectout.left; //中间线左边的宽度
right=rectout.right-rect_first.right; //中间线右边的宽度
break;
}
case 2:
{
CRect rect_second; //用于第二个柱子这层做两边离中间的宽度
GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&rect_second);//获得第一个柱子的位置
ScreenToClient(&rect_second);
rectout=m_tower_second.allrect[m_tower_second.allrect.size()-1];//保存向量的最后一个值
m_tower_second.allrect.pop_back();//出栈
left=rect_second.left-rectout.left;//中间线左边的宽度
right=rectout.right-rect_second.right;//中间线右边的宽度
break;
}
case 3:
{
CRect rect_three; //用于第三个柱子这层做两边离中间的宽度
GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&rect_three);//获得第一个柱子的位置
ScreenToClient(&rect_three);
rectout=m_tower_three.allrect[m_tower_three.allrect.size()-1];//保存向量的最后一个值
m_tower_three.allrect.pop_back();//出栈
left=rect_three.left-rectout.left;
right=rectout.right-rect_three.right;
break;
}
}
int weidth=rectout.Width(); //得到这个层显示的宽度
switch(m_line[i].b)//进来
{
case 1:
GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&rectin);//得到这个柱子的位置
ScreenToClient(&rectin);
rectout.left=rectin.left-left; //计算宽度
rectout.right=rectin.right+right; //计算宽度
if((m_tower_first.allrect.size())<=0) //如果向量里没有值
{
rectout.bottom=rectin.bottom;
//把柱子的最下面的坐标付给层
rectout.top=rectin.bottom-HEIGTH;
}
else
{
//计算要添加的这一层的bottom为下一层的顶部坐标
rectout.bottom=m_tower_first.allrect[m_tower_first.allrect.size()-1].top;
rectout.top=rectout.bottom-HEIGTH;//添加的这一层的高度
}
m_tower_first.allrect.push_back(rectout);
break;
case 2:
GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&rectin);
ScreenToClient(&rectin);
rectout.left=rectin.left-left;
rectout.right=rectin.right+right;
if((m_tower_second.allrect.size())<=0)
{
rectout.bottom=rectin.bottom;
rectout.top=rectin.bottom-HEIGTH;
}
else
{
rectout.bottom=m_tower_second.allrect[m_tower_second.allrect.size()-1].top;
rectout.top=rectout.bottom-HEIGTH;
}
m_tower_second.allrect.push_back(rectout);
break;
case 3:
GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&rectin);
ScreenToClient(&rectin);
rectout.left=rectin.left-left;
rectout.right=rectin.right+right;
if((m_tower_three.allrect.size())<=0)
{
rectout.bottom=rectin.bottom;
rectout.top=rectin.bottom-HEIGTH;
}
else
{
rectout.bottom=m_tower_three.allrect[m_tower_three.allrect.size()-1].top;
rectout.top=rectout.bottom-HEIGTH;
}
m_tower_three.allrect.push_back(rectout);
break;
}
i++; //下标加一
Invalidate(true);
}
}
void CTowerMfcDlg::OnForward()
{
// TODO: Add your control notification handler code here
CRect rectout;
CRect rectin;
int left=0;
int right=0;
if(i!=0)
{
i--;
switch(m_line[i].b)//出去
{
case 1:
{
CRect rect_first; //用于第一个柱子这层做两边离中间的宽度
GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&rect_first);//获得第一个柱子的位置
ScreenToClient(&rect_first); //转化坐标系
rectout=m_tower_first.allrect[m_tower_first.allrect.size()-1];//保存向量的最后一个值
m_tower_first.allrect.pop_back(); //出栈
left=rect_first.left-rectout.left; //中间线左边的宽度
right=rectout.right-rect_first.right; //中间线右边的宽度
break;
}
case 2:
{
CRect rect_second; //用于第二个柱子这层做两边离中间的宽度
GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&rect_second);//获得第一个柱子的位置
ScreenToClient(&rect_second);
rectout=m_tower_second.allrect[m_tower_second.allrect.size()-1];//保存向量的最后一个值
m_tower_second.allrect.pop_back();//出栈
left=rect_second.left-rectout.left;//中间线左边的宽度
right=rectout.right-rect_second.right;//中间线右边的宽度
break;
}
case 3:
{
CRect rect_three; //用于第三个柱子这层做两边离中间的宽度
GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&rect_three);//获得第一个柱子的位置
ScreenToClient(&rect_three);
rectout=m_tower_three.allrect[m_tower_three.allrect.size()-1];//保存向量的最后一个值
m_tower_three.allrect.pop_back();//出栈
left=rect_three.left-rectout.left;
right=rectout.right-rect_three.right;
break;
}
}
int weidth=rectout.Width(); //得到这个层显示的宽度
switch(m_line[i].a)//进来
{
case 1:
GetDlgItem(IDC_TOWER_FIRST)->GetWindowRect(&rectin);//得到这个柱子的位置
ScreenToClient(&rectin);
rectout.left=rectin.left-left; //计算宽度
rectout.right=rectin.right+right; //计算宽度
if((m_tower_first.allrect.size())<=0) //如果向量里没有值
{
rectout.bottom=rectin.bottom;
//把柱子的最下面的坐标付给层
rectout.top=rectin.bottom-HEIGTH;
}
else
{
//计算要添加的这一层的bottom为下一层的顶部坐标
rectout.bottom=m_tower_first.allrect[m_tower_first.allrect.size()-1].top;
rectout.top=rectout.bottom-HEIGTH;//添加的这一层的高度
}
m_tower_first.allrect.push_back(rectout);
break;
case 2:
GetDlgItem(IDC_TOWER_SECOND)->GetWindowRect(&rectin);
ScreenToClient(&rectin);
rectout.left=rectin.left-left;
rectout.right=rectin.right+right;
if((m_tower_second.allrect.size())<=0)
{
rectout.bottom=rectin.bottom;
rectout.top=rectin.bottom-HEIGTH;
}
else
{
rectout.bottom=m_tower_second.allrect[m_tower_second.allrect.size()-1].top;
rectout.top=rectout.bottom-HEIGTH;
}
m_tower_second.allrect.push_back(rectout);
break;
case 3:
GetDlgItem(IDC_TOWER_THREE)->GetWindowRect(&rectin);
ScreenToClient(&rectin);
rectout.left=rectin.left-left;
rectout.right=rectin.right+right;
if((m_tower_three.allrect.size())<=0)
{
rectout.bottom=rectin.bottom;
rectout.top=rectin.bottom-HEIGTH;
}
else
{
rectout.bottom=m_tower_three.allrect[m_tower_three.allrect.size()-1].top;
rectout.top=rectout.bottom-HEIGTH;
}
m_tower_three.allrect.push_back(rectout);
break;
}
//下标加一
Invalidate(true);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -