MFC 利用工作线程 刷新 界面 (开启多个工作者线程)

 (1)开启两个工作者线程,一个用于更新数据,一个用于定时发送刷新界面的消息

#include "testworkThread.h"
#include <cstdlib>
#include <ctime>

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

	//开启两个工作者线程,一个用于更新block的位置
	//一个用于定时发送刷新界面的消息,将最新的block位置显示出来
	pThread1 = AfxBeginThread(ThreadFunc1, &block);
	pThread2 = AfxBeginThread(ThreadFunc2, m_hWnd);
}

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(dc);
	//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(CDC &DC)
{
	// TODO: 在此处添加实现代码.
	

	CPoint point = block;
	CRect rect;
	int lt = 0, up = 0, rt = 0, dn = 0;
	CClientDC dc(this);
	CBrush brush(RGB(200, 0, 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);

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

//第一个工作者线程
UINT ThreadFunc1(LPVOID pParam)
{
	CPoint* point = (CPoint*)pParam;

	srand((unsigned)time(NULL));

	while (true)
	{

		point->x = rand() % 100;
		point->y = rand() % 100;
		Sleep(1000);
	}
}

//第二个工作者线程
UINT ThreadFunc2(LPVOID pParam)
{
	HWND hwnd = (HWND)pParam;

	
	//CString str;
	//str.Format(TEXT("%d"), hwnd);
	//AfxMessageBox(str);

	while (true)
	{
		::SendMessage(hwnd, WM_PAINT, NULL, NULL);
		Sleep(100);

	}

}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值