📄 chessboarddlg.cpp
字号:
// ChessBoardDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ChessBoard.h"
#include "ChessBoardDlg.h"
#include "ChessBoardView.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();
int m_Num;
// 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_Num = 16;
//}}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_EDIT1, m_Num);
//}}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_OnPaint, OnPaintChessboard)
ON_WM_LBUTTONDOWN()
ON_BN_CLICKED(IDC_FillBoard, OnFillBoard)
ON_BN_CLICKED(IDC_OnOK, OnOK)
ON_BN_CLICKED(IDC_OnCancle, OnOnCancle)
//}}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
iFlag=0;// 标志量,用来保证单击棋盘之后高亮方格,然后再单击则无效
iBflag=0;//标志量,用来保证只有单击绘制棋盘按钮之后才能单击棋盘覆盖按钮
iCflag=0;//标志量,单击绘制棋盘按钮前不能选中特殊方格
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()
{
PaintSquare();
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 CChessBoardDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CChessBoardDlg::OnPaintChessboard()
{
UpdateData(TRUE);
if (m_Num==2||m_Num==4||m_Num==8||m_Num==16||m_Num==32)
{
int a=m_Num;
CClientDC dc(this);
CPoint StartPoint,EndPoint;
StartPoint.x=140;//起始点
StartPoint.y=20;
long ChessNumber=(long)480/a;
for(int i=0;i<=a;i++)
{
dc.MoveTo(StartPoint);
EndPoint.x=StartPoint.x;//终点
EndPoint.y=500;
dc.LineTo(EndPoint);
StartPoint.x+=ChessNumber;
}
StartPoint.x=140;
StartPoint.y=20;
for(i=0;i<=a;i++)
{
dc.MoveTo(StartPoint);
EndPoint.y=StartPoint.y;
EndPoint.x=620;
dc.LineTo(EndPoint);
StartPoint.y+=ChessNumber;
}
iCflag=1;
}
else if(m_Num!=0&&m_Num%64==0)
{
MessageBox("有没有搞错?数太大将无法显示清晰","输入错误");
UpdateData(FALSE);
}
else
{
MessageBox("瞎输入,懂不懂什么叫棋盘覆盖?","输入错误");
UpdateData(FALSE);
}
}
void CChessBoardDlg::OnLButtonDown(UINT nFlags, CPoint point) //单击鼠标左键填充此方格
{
if(iFlag==0&&iCflag==1)
{//此前已单击绘制棋盘按钮,且未单击选中特殊方格
if(point.x>140&&point.x<620&&point.y>20&&point.y<500)//如果单击的点在棋盘内
{//填充此方格
long Width=(long)480/m_Num;
lrect.left=(point.x-140)/(int)Width*(long)Width+140;
lrect.top=(point.y-20)/(int)Width*(long)Width+20;
lrect.bottom=lrect.top+Width;
lrect.right=lrect.left+Width;
CClientDC dc(this);
CBrush Red(RGB(255,0,0));
dc.SelectObject(&Red);
dc.FillRect(lrect,&Red);
iFlag=1;//再单击无效
iBflag=1;//标记为已单击棋盘覆盖按钮
}
}
CDialog::OnLButtonDown(nFlags, point);
}
void CChessBoardDlg::InitBoard()
{ //定义特殊方格的行列号,存储到Board数组中
tile=1;
int dr,dc;
long Width=480/m_Num;
dr=(lrect.top-20)/Width+1;
dc=(lrect.left-140)/Width+1;
board[dr][dc]=0;
chessboard(1,1,dr,dc,m_Num);//用L型骨牌覆盖棋盘
}
void CChessBoardDlg::chessboard(int tr, int tc, int dr, int dc, int size)//棋盘覆盖
{
if (size==1) return;
register int t=tile++; //L型骨牌号
register 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;//用t号L型骨牌覆盖右下角
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;//用t号L型骨牌覆盖左下角
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;//用t号L型骨牌覆盖右上角
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;//用t号L型骨牌覆盖左上角
chessboard(tr+s,tc+s,tr+s,tc+s,s);}//覆盖其余方格
}
void CChessBoardDlg::OnFillBoard()
{//填充棋盘
if(iBflag==1)
{//已单击绘制棋盘按钮
InitBoard();
int i,j;
long Width=480/m_Num;
for(i=1;i<=m_Num;i++)//填充棋盘
for(j=1;j<=m_Num;j++)
{
CRect rect;
rect.top=20+Width*(i-1);
rect.left=140+Width*(j-1);
rect.bottom=rect.top+Width;
rect.right=rect.left+Width;
CClientDC dc(this);
if(board[i][j]!=0)
{//填充除特殊方格以外的所有其他方格
CBrush Blue(RGB(board[i][j]*80,board[i][j]*40,board[i][j]*12));//初始化笔刷
dc.SelectObject(&Blue);
dc.FillRect(rect,&Blue);
}
}
}
}
void CChessBoardDlg::OnOK()
{//重新开始
CDialog::Invalidate(FALSE);
OnInitDialog();
}
//背景布色
void CChessBoardDlg::PaintSquare()
{
lrect.left=140;
lrect.top=20;
lrect.bottom=lrect.top+480;
lrect.right=lrect.left+480;
CClientDC dc(this);
CBrush Red(RGB(160,160,160));
dc.SelectObject(&Red);
dc.FillRect(lrect,&Red);
}
void CChessBoardDlg::OnOnCancle()
{
OnCancel();
// TODO: Add your control notification handler code here
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -