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

📄 chessboarddlg.cpp

📁 本程序可以动态显示棋盘覆盖的过程
💻 CPP
字号:
// chessBoardDlg.cpp : implementation file
//

#include "stdafx.h"
#include "chessBoard.h"
#include "chessBoardDlg.h"

#include "stdlib.h"

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

extern CChessBoardApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About


int num=0;
CClientDC dc(theApp.m_pMainWnd);


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()

/////////////////////////////////////////////////////////////////////////////
// CChessBoardDlg dialog

CChessBoardDlg::CChessBoardDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CChessBoardDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CChessBoardDlg)
	m_size = 0;
	tile=1;
//	overwrite=1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CChessBoardDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChessBoardDlg)
	DDX_Text(pDX, IDC_EDIT_SIZE, m_size);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChessBoardDlg, CDialog)
	//{{AFX_MSG_MAP(CChessBoardDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTONOK, OnButtonok)
	ON_BN_CLICKED(IDC_BTNOVERWRITE, OnBtnoverwrite)
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChessBoardDlg message handlers

BOOL CChessBoardDlg::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
	m_size=4;
	UpdateData(FALSE);
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CChessBoardDlg::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 CChessBoardDlg::OnPaint() 
{
	CPaintDC dc(this); 
	if (IsIconic())
	{
	// 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
	{
		if(num!=0) 
		{
/*			(GetDlgItem(IDC_OUTPUT))->GetWindowRect(&rect);
			averow=(rect.bottom-rect.top)/m_size;
			aveline=(rect.right-rect.left)/m_size;
			ScreenToClient(&rect);
			dc.FillSolidRect(&rect,RGB(255,255,255));
			
			for(int i=0;i<=m_size;i++)
			{
				dc.MoveTo(rect.left,rect.top+(i)*averow);
				dc.LineTo(rect.right,rect.top+i*averow);
			}
			
			
			for(i=0;i<=m_size;i++)
			{
				dc.MoveTo(rect.left+(i)*aveline,rect.top);
				dc.LineTo(rect.left+i*aveline,rect.bottom);
			}
			
			dc.FillSolidRect(rect.left+randnum1*aveline,rect.top+randnum2*averow,
				aveline,averow,0);
		*/
			OnButtonok();
		}
		num++;
		CDialog::OnPaint();
	}
}


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

void CChessBoardDlg::OnButtonok() 
{
    UpdateData();
    if(m_size<=0 || m_size>128)
	{
		MessageBox("输入一个1---128之间的数");
		return;
	}
    int a;
	a=m_size;
	while(a%2==0)
	{
		a=a/2;
	}
	if(a!=1)
	{
		MessageBox("输入一个2的N次方的数");
		return;
	}
		
	overwrite=1;
	tile=1;
	board=new int*[m_size];
	for(int j=0;j<m_size;j++)
	{
		board[j]=new int[m_size];
	}
    
    srand((unsigned)time(NULL));
    randnum1=rand();
	randnum1%=m_size;
    randnum2=rand();
	randnum2%=m_size;
	
	board[randnum2][randnum1]=0;
    chessBoard(0,0,randnum1,randnum2,m_size);
	
	
	(GetDlgItem(IDC_OUTPUT))->GetWindowRect(&rect);
	averow=(rect.bottom-rect.top)/m_size;
	aveline=(rect.right-rect.left)/m_size;
//	ScreenToClient(&rect);
	dc.FillSolidRect(&rect,RGB(255,255,255));
	
	for(int i=0;i<=m_size;i++)
	{
		dc.MoveTo(rect.left,rect.top+(i)*averow);
		dc.LineTo(rect.right,rect.top+i*averow);
	}
    
	
	for(i=0;i<=m_size;i++)
	{
		dc.MoveTo(rect.left+(i)*aveline,rect.top);
		dc.LineTo(rect.left+i*aveline,rect.bottom);
	}
	
	dc.FillSolidRect(rect.left+randnum1*aveline,rect.top+randnum2*averow,
		aveline,averow,0);
}	


void CChessBoardDlg::chessBoard(int tr, int tc, int dr, int dc, int size)
{ 
	if(size==1) return;
	int t=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);
	}
	
	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);
	}
	

	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);
	}

	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);
	}
}


void CChessBoardDlg::OnBtnoverwrite() 
{
	timenum=SetTimer(1,500,NULL);
}


void CChessBoardDlg::OnTimer(UINT nIDEvent) 
{  
    int i;
	int j;
	if(overwrite==(m_size*m_size-1)/3+1)
	{
		for(i=0;i<m_size;i++)
		{
			delete board[i];
		}
		delete board;
		KillTimer(timenum);
		MessageBox("覆盖完毕");
		return;
	}
	for(i=0;i<m_size;i++)
	{
		for(j=0;j<m_size;j++)
		{
			if(board[i][j]==overwrite)
			{
				dc.FillSolidRect(rect.left+i*aveline,rect.top+j*averow,
		aveline,averow,RGB((overwrite+30)*20,overwrite*20,overwrite*50));
			}
		}
	}
	overwrite++;
	CDialog::OnTimer(nIDEvent);
}

⌨️ 快捷键说明

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