MyThreadView.cpp
资源名称:08.zip [点击查看]
上传用户:ynjin1970
上传日期:2014-10-13
资源大小:6438k
文件大小:4k
源码类别:
中间件编程
开发平台:
Visual C++
- // MyThreadView.cpp : implementation of the CMyThreadView class
- //
- #include "stdafx.h"
- #include "MyThread.h"
- #include "MyThreadDoc.h"
- #include "MyThreadView.h"
- #include "UIThread.h"
- #include "MainFrm.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyThreadView
- IMPLEMENT_DYNCREATE(CMyThreadView, CView)
- BEGIN_MESSAGE_MAP(CMyThreadView, CView)
- //{{AFX_MSG_MAP(CMyThreadView)
- ON_COMMAND(ID_THREAD_RESUME, OnThreadResume)
- ON_COMMAND(ID_THREAD_SUSPEND, OnThreadSuspend)
- ON_COMMAND(ID_THREAD_UI, OnThreadUi)
- ON_COMMAND(ID_THREAD_WORKER, OnThreadWorker)
- //}}AFX_MSG_MAP
- // Standard printing commands
- ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
- ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CMyThreadView construction/destruction
- CMyThreadView::CMyThreadView()
- {
- // TODO: add construction code here
- }
- CMyThreadView::~CMyThreadView()
- {
- }
- BOOL CMyThreadView::PreCreateWindow(CREATESTRUCT& cs)
- {
- // TODO: Modify the Window class or styles here by modifying
- // the CREATESTRUCT cs
- return CView::PreCreateWindow(cs);
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyThreadView drawing
- void CMyThreadView::OnDraw(CDC* pDC)
- {
- CMyThreadDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- // TODO: add draw code for native data here
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyThreadView printing
- BOOL CMyThreadView::OnPreparePrinting(CPrintInfo* pInfo)
- {
- // default preparation
- return DoPreparePrinting(pInfo);
- }
- void CMyThreadView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add extra initialization before printing
- }
- void CMyThreadView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
- {
- // TODO: add cleanup after printing
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyThreadView diagnostics
- #ifdef _DEBUG
- void CMyThreadView::AssertValid() const
- {
- CView::AssertValid();
- }
- void CMyThreadView::Dump(CDumpContext& dc) const
- {
- CView::Dump(dc);
- }
- CMyThreadDoc* CMyThreadView::GetDocument() // non-debug version is inline
- {
- ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyThreadDoc)));
- return (CMyThreadDoc*)m_pDocument;
- }
- #endif //_DEBUG
- /////////////////////////////////////////////////////////////////////////////
- // CMyThreadView message handlers
- /////////////////////////////////////////////////////////////
- // 工作线程函数 //
- /////////////////////////////////////////////////////////////
- UINT ThreadProc(LPVOID ThreadParam)
- {
- CDC * pDC;
- CMyThreadView * pView;
- //获取视图指针,注意包含头文件MainFrm.h
- CMainFrame * pFrame = (CMainFrame *)(AfxGetApp()->m_pMainWnd);
- pView = (CMyThreadView *)(pFrame->GetActiveView());
- pDC = pView->GetDC(); // 获取设备环境
- RECT rect;
- pView->GetClientRect(&rect); // 获取客户区大小
- int num = 0;//计数器
- CBrush brush(HS_CROSS, RGB(0,255,0));// 构造一支阴影模式画刷,填充矩形内部
- CBrush * poldbrush = pDC->SelectObject(&brush);
- // 使用循环画矩形,达到动画效果
- do{
- num ++;//计数器加1
- //画矩形。
- pDC->Rectangle(0,rect.bottom-(num/10),300,rect.bottom);
- //延迟
- Sleep(3); // 每画一个矩形延时3毫秒
- }while(num < 4000); // 循环4000次
- pDC->SelectObject(poldbrush); //落选画笔
- pView->ReleaseDC(pDC); // 释放设备环境
- return 1;
- }
- //唤醒工作线程
- void CMyThreadView::OnThreadResume()
- {
- if(m_pThread) // 如果工作线程指针非空
- m_pThread->ResumeThread();// 将线程挂起数-1
- }
- //挂起工作线程
- void CMyThreadView::OnThreadSuspend()
- {
- if(m_pThread) // 如果工作线程指针非空
- m_pThread->SuspendThread();// 将线程挂起数+1
- }
- //产生界面线程
- void CMyThreadView::OnThreadUi()
- {
- //使用AfxBeginThread函数创建用户界面线程,
- //并保存新线程对象的指针到m_pUIThread中
- m_pUIThread = AfxBeginThread(RUNTIME_CLASS(CUIThread));// 线程类的执行期类指针
- }
- //产生工作线程
- void CMyThreadView::OnThreadWorker()
- {
- //使用AfxBeginThread函数创建工作线程,
- //并保存新线程对象的指针到m_pThread中
- m_pThread = AfxBeginThread(ThreadProc, // 全局线程函数名字
- 0 // 传入的参数
- );
- }