MFC 生成界面及刷新客户区

 (1)利用MFC的CFrameWnd继承一个窗口类,由此画一个简单的界面

(2)在客户区内画一个网格

(3)实现鼠标左键点击网格中某个格子后,对格子进行颜色填充,鼠标右键点击格子后恢复原来的颜色

#pragma once
#include <afxwin.h>

class Myapp : public CWinApp //WinApp应用类
{
public:
	//程序入口
	virtual BOOL InitInstance();
};

class MyFrame :public CFrameWnd //应用程序窗口框架类
{
public:
	MyFrame();

public:
	afx_msg void OnPaint();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	DECLARE_MESSAGE_MAP()
};
#include "mfc.h"

Myapp app;//要实例化一个,有且仅有一个,全局的应用程序类对象!!

BOOL Myapp::InitInstance()
{
	//创建窗口
	MyFrame *frame = new MyFrame;

	//显示和更新
	frame->ShowWindow(SW_SHOWNORMAL);
	frame->UpdateWindow();

	//将自己创建的窗口的指针传递给 CWinThread类成员m_pMainWnd,用于取消息等操作
	m_pMainWnd = frame;  //保存指向应用程序主窗口的指针

	return TRUE;
}

BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
END_MESSAGE_MAP()


MyFrame::MyFrame()
{
	Create(NULL, TEXT("mfx"));
}

void MyFrame::OnPaint()
{
	CPaintDC dc(this);
	
	CBrush brush(RGB(100, 100, 200));
	CRect rect(0, 0, 200, 200);//0-200区域是会被蓝色重新刷的200-500区域则不会被蓝色重刷
	dc.FillRect(&rect, &brush);

	int N = 50,step=10;
	for (int i = 0; i < 50; ++i)
	{
		dc.MoveTo(i * step, 0);
		dc.LineTo(i * step, step*N);
		dc.MoveTo(0, i * step);
		dc.LineTo(step*N, i * step);
	}


	MessageBox(TEXT("oo"));
}

void MyFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
	CRect rect;
	int lt = 0,up=0,rt=0,dn=0;
	CClientDC dc(this);

	CBrush brush(RGB(200, 100, 100));

	if (point.x > 0 && point.y > 0 && point.x < 500 && point.y < 500)
	{
		lt = point.x / 10*10;
		up = point.y / 10*10;
		rt = lt + 10;
		dn = up + 10;
		rect = CRect(lt, up, rt, dn);

		dc.FillRect(&rect, &brush);

	}
}

void MyFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
	CRect rect;
	int lt = 0, up = 0, rt = 0, dn = 0;


	if (point.x > 0 && point.y > 0 && point.x < 500 && point.y < 500)
	{
		lt = point.x / 10 * 10;
		up = point.y / 10 * 10;
		rt = lt + 10;
		dn = up + 10;
		rect = CRect(lt, up, rt, dn);

		InvalidateRect(&rect, FALSE);//当为FALSE时,不会导致背景被刷新
		//Invalidate(TRUE);

	}
}

 

(4)利用键盘输入 key_left,key_up, key_right,key_down来控制一个方块的运动

(5)方块离开之前的位置后,删除掉之前的方块,重绘(到这里,离贪吃蛇小游戏已经不远了)

#pragma once
#include <afxwin.h>

class Myapp : public CWinApp //WinApp应用类
{
public:
	//程序入口
	virtual BOOL InitInstance();
};

class MyFrame :public CFrameWnd //应用程序窗口框架类
{
public:
	MyFrame();

public:
	afx_msg void OnPaint();
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
	DECLARE_MESSAGE_MAP()
	// 重绘block
	void ReDrawBlock();
	// 可控制的颜色块
	CPoint block=(300,300);
};

 

#include "mfc.h"

Myapp app;//要实例化一个,有且仅有一个,全局的应用程序类对象!!

BOOL Myapp::InitInstance()
{
	//创建窗口
	MyFrame *frame = new MyFrame;

	//显示和更新
	frame->ShowWindow(SW_SHOWNORMAL);
	frame->UpdateWindow();

	//将自己创建的窗口的指针传递给 CWinThread类成员m_pMainWnd,用于取消息等操作
	m_pMainWnd = frame;  //保存指向应用程序主窗口的指针

	return TRUE;
}

BEGIN_MESSAGE_MAP(MyFrame, CFrameWnd)
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	ON_WM_RBUTTONDOWN()
	ON_WM_KEYDOWN()
END_MESSAGE_MAP()


MyFrame::MyFrame()
{
	Create(NULL, TEXT("mfx"));
}

void MyFrame::OnPaint()
{
	CPaintDC dc(this);
	
	CBrush brush(RGB(100, 100, 200));
	CRect rect(0, 0, 200, 200);//0-200区域是会被蓝色重新刷的200-500区域则不会被蓝色重刷
	dc.FillRect(&rect, &brush);

	int N = 50,step=10;
	for (int i = 0; i < 50; ++i)
	{
		dc.MoveTo(i * step, 0);
		dc.LineTo(i * step, step*N);
		dc.MoveTo(0, i * step);
		dc.LineTo(step*N, i * step);
	}

	ReDrawBlock();
	//MessageBox(TEXT("oo"));
}

void MyFrame::OnLButtonDown(UINT nFlags, CPoint point)
{
	CRect rect;
	int lt = 0,up=0,rt=0,dn=0;
	CClientDC dc(this);

	CBrush brush(RGB(200, 100, 100));

	if (point.x > 0 && point.y > 0 && point.x < 500 && point.y < 500)
	{
		lt = point.x / 10*10;
		up = point.y / 10*10;
		rt = lt + 10;
		dn = up + 10;
		rect = CRect(lt, up, rt, dn);

		dc.FillRect(&rect, &brush);

	}
}

void MyFrame::OnRButtonDown(UINT nFlags, CPoint point)
{
	CRect rect;
	int lt = 0, up = 0, rt = 0, dn = 0;


	if (point.x > 0 && point.y > 0 && point.x < 500 && point.y < 500)
	{
		lt = point.x / 10 * 10;
		up = point.y / 10 * 10;
		rt = lt + 10;
		dn = up + 10;
		rect = CRect(lt, up, rt, dn);

		InvalidateRect(&rect, TRUE);//当为FALSE时,不会导致背景被刷新
		//Invalidate(TRUE);

	}
}

// 重绘block
void MyFrame::ReDrawBlock()
{
	// TODO: 在此处添加实现代码.
	CPoint point = block;
	CRect rect;
	int lt = 0, up = 0, rt = 0, dn = 0;
	CClientDC dc(this);

	CBrush brush(RGB(200, 100, 100));

	if (point.x > 0 && point.y > 0 && point.x < 500 && point.y < 500)
	{
		lt = point.x / 10 * 10;
		up = point.y / 10 * 10;
		rt = lt + 10;
		dn = up + 10;
		rect = CRect(lt, up, rt, dn);

		dc.FillRect(&rect, &brush);

	}
}

void MyFrame::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	CPoint block_old = block;//记录旧的位置

	switch (nChar)
	{
	case VK_LEFT:
		if (block.x > 10)
		{
			block.x -= 10;
			OnRButtonDown(0, block_old); //使当前位置的块失效		
		}
		break;
	case VK_RIGHT:
		if (block.x < 400)
		{
			block.x += 10;
			OnRButtonDown(0, block_old); //使当前位置的块失效		
		}
		break;
	case VK_UP:
		if (block.y > 10)
		{
			block.y -= 10;
			OnRButtonDown(0, block_old); //使当前位置的块失效		
		}
		break;
	case VK_DOWN:
		if (block.y < 400)
		{
			block.y += 10;
			OnRButtonDown(0, block_old); //使当前位置的块失效		
		}
		break;
	default:
		break;
	}
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值