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

📄 棋盘覆盖.txt

📁 C精彩编程百例源码
💻 TXT
字号:
// chess1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "chess1.h"
#include "chess1Dlg.h"
#include <math.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()

/////////////////////////////////////////////////////////////////////////////
// CChess1Dlg dialog

CChess1Dlg::CChess1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChess1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChess1Dlg)
	m_k = 0;
	m_dr = 0;
	m_dc = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	board = new int*[50];                  
for(int i = 0;i<50;i++)
    board[i] = new int[50];

}

void CChess1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChess1Dlg)
	DDX_Text(pDX, Edit1, m_k);
	DDX_Text(pDX, Edit2, m_dr);
	DDX_Text(pDX, Edit3, m_dc);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChess1Dlg, CDialog)
	//{{AFX_MSG_MAP(CChess1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDOK1, OnOk1)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChess1Dlg message handlers

BOOL CChess1Dlg::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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CChess1Dlg::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 CChess1Dlg::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();
	}
}

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



void CChess1Dlg::DrawSubBoard(int x, int y, int w, int c)
{
    CClientDC dc(this);
    COLORREF clr;
    clr = RGB(c*c/256,c*c*c/256,c*c*c*c/256);     //将c值转换成颜色值
    CBrush br(clr);
    CRect r;
    r.top = 10 + x*m_dw;
    r.bottom = r.top + m_dw;
    r.left = 10+ y*m_dw;
    r.right = r.left + m_dw;
    dc.FillRect(&r,&br);
}

void CChess1Dlg::chessBoard(int tr, int tc, int dr, int dc, int size)
{
    if(1 == size)
    {
        return;
    }
    Sleep(500);                             //覆盖L型骨牌后停顿0.5秒,以便观察
    int t = this->tile++;
    int s = size/2;
    if(dr < tr+s && dc < tc+s)
        chessBoard(tr,tc,dr,dc,s);
    else
    {
        board[tr+s-1][tc+s-1] = t;
        chessBoard(tr,tc,tr+s-1,tc+s-1,s);
        this->DrawSubBoard(tr+s-1,tc+s-1,m_dw,t);//递归过程中,此子棋盘中没有特殊方格,调用DrawSubBoard()函数画一个方格,并填充颜色
    }
    if(dr < tr+s && dc >= tc+s)
        chessBoard(tr,tc+s,dr,dc,s);
    else
    {
        board[tr+s-1][tc+s] = t;
        chessBoard(tr,tc+s,tr+s-1,tc+s,s);
        this->DrawSubBoard(tr+s-1,tc+s,m_dw,t);
    }

    if(dr >= tr+s && dc < tc+s)
        chessBoard(tr+s,tc,dr,dc,s);
    else
    {
        board[tr+s][tc+s-1] = t;
        chessBoard(tr+s,tc,tr+s,tc+s-1,s);
        this->DrawSubBoard(tr+s,tc+s-1,m_dw,t);
    }

    if(dr >= tr+s && dc >= tc+s)
        chessBoard(tr+s,tc+s,dr,dc,s);
    else
    {
        board[tr+s][tc+s] = t;
        chessBoard(tr+s,tc+s,tr+s,tc+s,s);
        this->DrawSubBoard(tr+s,tc+s,m_dw,t);
    }
}


void CChess1Dlg::OnOK() 
{
UpdateData();                        //更新编辑框中数据,重要
    if(m_k > 5)
    {
        this->MessageBox("为了方便演示,建议输入1-5的整数!");
    }
    else
    {
        this->OnCancel();                //当输入一个新的k值时,清空原有图形
        CClientDC dc(this);
         int k=this->m_k;
        this->m_dw = 256/(int)pow(2,k); //棋盘区域大小固定为256*256,根据k值计算方格宽度
        for(int i = 10;i<=267;i+=m_dw)  //画棋盘
        {
            dc.MoveTo(10,i);
            dc.LineTo(267,i);
            dc.MoveTo(i,10);
            dc.LineTo(i,267);
        }
    }

	//CDialog::OnOK();
}




void CChess1Dlg::OnCancel() 
{
	CClientDC dc(this);
    dc.Rectangle(10,10,267,267);


	// TODO: Add extra cleanup here
	
	//CDialog::OnCancel();
}

void CChess1Dlg::OnOk1() 
{
UpdateData();
    if(m_dr >= (int)pow(2,m_k) || m_dc >= (int)pow(2,m_k) || m_dr < 0 || m_dc < 0)
          //判断dr,dc中数据是否溢出
    {
        this->MessageBox("特殊方格行号或列号错误,请重新输入!");
    }
    else
    {
        //调用以下两个函数重新进行覆盖
        this->OnCancel();
        this->OnOK();
        //得到特殊方格的行号和列号后,将它用黑色填充,以作标记
        CClientDC dc(this);
        COLORREF clr;
        clr = RGB(0,0,0);
        CBrush br(clr);
        CRect r;
        int w = 256/(int)pow(2,m_k);
        r.top = 10 + m_dr*w;
        r.bottom = r.top + w;
        r.left = 10 + m_dc*w;
        r.right = r.left + w;
        dc.FillRect(&r,&br);
        Sleep(1000);
        this->chessBoard(0,0,this->m_dr,this->m_dc,(int)pow(2,this->m_k));

	
}
}

void CChess1Dlg::OnButton1() 
{
DestroyWindow();   
	
}

⌨️ 快捷键说明

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