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

📄 1dlg.cpp

📁 学习VC时做课程设计做的系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// 1Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "1.h"
#include "1Dlg.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()

/////////////////////////////////////////////////////////////////////////////
// CMy1Dlg dialog

CMy1Dlg::CMy1Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMy1Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMy1Dlg)
	m_lines = 0;
	m_level = 0;
	m_score = 0;
	m_time=1000-m_level*90;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMy1Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CMy1Dlg)
	DDX_Control(pDX, IDC_GAMEPREVIEW, m_gamepreview);
	DDX_Control(pDX, IDC_GAMEBOARD, m_gameboard);
	DDX_Text(pDX, IDC_LINES, m_lines);
	DDX_Text(pDX, IDC_LEVEL, m_level);
	DDX_Text(pDX, IDC_SCORE, m_score);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CMy1Dlg, CDialog)
	//{{AFX_MSG_MAP(CMy1Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START, OnStart)
	ON_BN_CLICKED(IDC_ROTATE, OnRotate)
	ON_BN_CLICKED(IDC_PAUSE, OnPause)
	ON_BN_CLICKED(IDC_MOVELEFT, OnMoveleft)
	ON_BN_CLICKED(IDC_MOVERIGHT, OnMoveright)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_DOWN, OnDown)
	ON_BN_CLICKED(IDC_help, Onhelp)
	//ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_WM_CTLCOLOR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMy1Dlg message handlers

CBitmap m_bitmap;
 CBrush  m_brush;

BOOL CMy1Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();


 m_bitmap.LoadBitmap(IDB_BITMAP1);
 //m_brush.CreatePatternBrush(&m_bitmap);
	
	
	
	// 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

	int zy;
	// TODO: Add extra initialization here
     srand(   (unsigned)time(   NULL   )   );   

	zy= rand()%4;
if (zy==0)
	 m_stone.LoadBitmap(IDB_STONE); else 
if (zy==1)
	 m_stone.LoadBitmap(IDB_STONE1); else 
if (zy==2)
	 m_stone.LoadBitmap(IDB_STONE2); else 
if (zy==3)
	 m_stone.LoadBitmap(IDB_STONE3);  else
if (zy==4)
	 m_stone.LoadBitmap(IDB_STONE4);  

	BITMAP bm;
	m_stone.GetBitmap(&bm);
	m_BitWidth=bm.bmWidth;
	m_BitHeight=bm.bmHeight;
	CRect rectdlg,rectgame,rectpreview;
	CPoint LTpoint;//图像区左上角
	GetWindowRect(rectdlg);
	m_gameboard.GetWindowRect(rectgame);
	m_size.cx=(rectgame.right-rectgame.left)/10;
	m_size.cy=(rectgame.bottom-rectgame.top)/25;
	LTpoint.x=rectgame.left-rectdlg.left;
	LTpoint.y=30;
	CPoint point;//预览区左上角
	m_gamepreview.GetWindowRect(rectpreview);
	m_pvsize.cx=(rectpreview.right-rectpreview.left)/PREVIEW;
	m_pvsize.cy=(rectpreview.bottom-rectpreview.top)/PREVIEW;
	point.x=rectpreview.left-rectdlg.left;
	point.y=30;
	for(int i=0;i<25;i++)
		for(int j=0;j<10;j++)
		{
			shape[i][j].active=1;
			shape[i][j].rect.left=LTpoint.x+j*m_size.cx;
			shape[i][j].rect.top=LTpoint.y+i*m_size.cy;
			shape[i][j].rect.right=LTpoint.x+(j+1)*m_size.cx;
			shape[i][j].rect.bottom=LTpoint.y+(i+1)*m_size.cy;
			shape[i][j].hrect=0;
		}
	for(i=0;i<PREVIEW;i++)
		for(int j=0;j<PREVIEW;j++)
		{
			shapepreview[i][j].rect.left=point.x+j*m_pvsize.cx;
			shapepreview[i][j].rect.top=point.y+i*m_pvsize.cy;
			shapepreview[i][j].rect.right=point.x+(j+1)*m_pvsize.cx;
			shapepreview[i][j].rect.bottom=point.y+(i+1)*m_pvsize.cy;
			shapepreview[i][j].hrect =0;
			shapepreview[i][j].active =0;
		}
	shapepreview[1][2].hrect=1;
	shapepreview[1][3].hrect=1;
	shapepreview[2][2].hrect=1;
	shapepreview[3][2].hrect=1;
	Start=FALSE;

	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMy1Dlg::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 CMy1Dlg::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
	{
		CPaintDC dc(this);

		CDC MemDC;
		MemDC.CreateCompatibleDC(NULL);
		MemDC.SelectObject(m_stone);
		for(int i=0;i<25;i++)
			for(int j=0;j<10;j++)
			{
				if(1==shape[i][j].hrect)
					dc.StretchBlt(shape[i][j].rect.left,shape[i][j].rect.top,m_size.cx,m_size.cy,&MemDC,0,0,m_BitWidth,m_BitHeight,SRCCOPY);
			}
		for(i=0;i<PREVIEW;i++)
			for(int j=0;j<PREVIEW;j++)
			{
				if(1==shapepreview[i][j].hrect)
					dc.StretchBlt(shapepreview[i][j].rect.left,shapepreview[i][j].rect.top,m_pvsize.cx,m_pvsize.cy,&MemDC,0,0,m_BitWidth,m_BitHeight,SRCCOPY);
			}
		CDialog::OnPaint();
	}
}

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

void CMy1Dlg::OnStart() 
{
	// TODO: Add your control notification handler code here
	for(int i=0;i<MAX_LINE;i++)
		for(int j=0;j<MAX_COW;j++)
		{
			shape[i][j].hrect=0;
			shape[i][j].active=1;
		}
	Invalidate();
	OnGenerateShape();
	SetTimer(1,m_time,NULL);
	Start=TRUE;
	
}

void CMy1Dlg::OnRotate() 
{
	// TODO: Add your control notification handler code here
	struct rotate
	{
		int i;
		int j;
	};
	struct rotate rect[6];
	int k=0;
	BOOL board=FALSE;
	if(Start&&!AttachBoard('d'))
	{
		for(int i=0;i<25;i++)
		{
			for(int j=0;j<10;j++)
			{
				if(1==shape[i][j].hrect&&1==shape[i][j].active)
				{
				  rect[k].i=i;
				  rect[k].j=j;
				  k++;
				}
			}
		}
		for(i=1;i<4;i++)
		{
			if(rect[0].j+1>9)
				board=TRUE;
			if(rect[i].i==rect[0].i)
			{
				int m=rect[i].j-rect[0].j;
				if(rect[0].i+m+1>24||rect[0].j+1>9||(shape[rect[0].i+m+1][rect[0].j+1].hrect==1&&shape[rect[0].i+m+1][rect[0].j+1].active==0)) 
					board=TRUE;
			}
			else
			{
				if(rect[i].j==rect[0].j)
				{
					int m=rect[i].i-rect[0].i;	
					if(rect[0].j-m+1<0||(shape[rect[0].i+1][rect[0].j-m+1].hrect==1&&shape[rect[0].i+1][rect[0].j-m+1].active==0)) 
							board=TRUE;
				}
				else
				{
					int line=rect[i].i-rect[0].i;
					int cow=abs(rect[i].j-rect[0].j);
					if(rect[i].j>rect[0].j)
					{
						if(rect[0].i+cow+1>24||rect[0].j-line+1<0||(shape[rect[0].i+cow+1][rect[0].j-line+1].hrect==1&&shape[rect[0].i+cow+1][rect[0].j-line+1].active==0))
							board=TRUE;
					}
					else
					{
						if(rect[0].i-cow+1<0||rect[0].j-line+1<0||(shape[rect[0].i-cow+1][rect[0].j-line+1].hrect==1&&shape[rect[0].i-cow+1][rect[0].j-line+1].active==0))
							board=TRUE;
					}
				}
			}
		}
		if(!board)
		{
			shape[rect[0].i][rect[0].j].hrect-=1;
			shape[rect[0].i+1][rect[0].j+1].hrect+=1;
			InvalidateRect(shape[rect[0].i][rect[0].j].rect);
			InvalidateRect(shape[rect[0].i+1][rect[0].j+1].rect);
			for(i=1;i<4;i++)
			{
				BOOL board=FALSE;
				if(rect[i].i==rect[0].i)
				{
					int m=rect[i].j-rect[0].j;
					shape[rect[i].i][rect[i].j].hrect-=1;
					shape[rect[0].i+m+1][rect[0].j+1].hrect+=1;
					InvalidateRect(shape[rect[i].i][rect[i].j].rect );
					InvalidateRect(shape[rect[0].i+m+1][rect[0].j+1].rect);
				}
				else 
				{
					if(rect[i].j==rect[0].j)
					{
						int m=rect[i].i-rect[0].i;		
						shape[rect[i].i][rect[i].j].hrect-=1;
						shape[rect[0].i+1][rect[0].j-m+1].hrect+=1;
						InvalidateRect(shape[rect[i].i][rect[i].j].rect);
						InvalidateRect(shape[rect[0].i+1][rect[0].j-m+1].rect);
			
					}		
					else
					{
						int line=rect[i].i-rect[0].i;
						int cow=abs(rect[i].j-rect[0].j);
						if(rect[i].j>rect[0].j)
						{
							shape[rect[i].i][rect[i].j].hrect-=1;
							shape[rect[0].i+cow+1][rect[0].j-line+1].hrect+=1;
							InvalidateRect(shape[rect[i].i][rect[i].j].rect);
							InvalidateRect(shape[rect[0].i+cow+1][rect[0].j-line+1].rect);
						}
						else
						{
							shape[rect[i].i][rect[i].j].hrect-=1;
							shape[rect[0].i-cow+1][rect[0].j-line+1].hrect+=1;
							InvalidateRect(shape[rect[i].i][rect[i].j].rect);
							InvalidateRect(shape[rect[0].i-cow+1][rect[0].j-line+1].rect);
						}
					}
				}
			}
		}

	}
	
}

void CMy1Dlg::OnPause() 
{
	// TODO: Add your control notification handler code here
	if(Start)
	{
		KillTimer(1);
		Start=FALSE;
	}
	else
	{
		SetTimer(1,m_time,NULL);
		Start=TRUE;
	}
	
}

void CMy1Dlg::OnMoveleft() 
{
	// TODO: Add your control notification handler code here
	if(Start&&!AttachBoard('l'))
	{
		for(int i=0;i<25;i++)
			for(int j=0;j<10;j++)
			{
				if(1==shape[i][j].hrect&&1==shape[i][j].active)
				{
					shape[i][j].hrect=0;
					shape[i][j-1].hrect =1;
					InvalidateRect(shape[i][j].rect);
					InvalidateRect(shape[i][j-1].rect);
				}
			}
	}
}
	

void CMy1Dlg::OnMoveright() 
{
	// TODO: Add your control notification handler code here
	if(Start&&!AttachBoard('r'))
	{
		for(int i=0;i<25;i++)
			for(int j=9;j>=0;j--)
			{
				if(1==shape[i][j].hrect&&1==shape[i][j].active)
				{
					shape[i][j].hrect=0;
					shape[i][j+1].hrect =1;
					InvalidateRect(shape[i][j].rect);
					InvalidateRect(shape[i][j+1].rect);
				}
			}
	}
	
}

void CMy1Dlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	KillTimer(1);
	

⌨️ 快捷键说明

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