WEB开发网
开发学院软件开发VC 多线程编程之二——MFC中的多线程开发 阅读

多线程编程之二——MFC中的多线程开发

 2007-03-17 22:00:08 来源:WEB开发网   
核心提示: 在UIThread.h中添加#include "UIThreadDlg.h"并在CUIThread类中添加protected变量CUIThread m_dlg:class CUIThread : public CWinThread{DECLARE_DYNCREATE(C

在UIThread.h中添加#include "UIThreadDlg.h"并在CUIThread类中添加protected变量CUIThread m_dlg:class CUIThread : public CWinThread
{
  DECLARE_DYNCREATE(CUIThread)
protected:
  CUIThread();      // protected constructor used by dynamic creation
// Attributes
public:
// Operations
public:
// Overrides
  // ClassWizard generated virtual function overrides
  //{{AFX_VIRTUAL(CUIThread)
  public:
  virtual BOOL InitInstance();
  virtual int ExitInstance();
  //}}AFX_VIRTUAL
// Implementation
protected:
  CUIThreadDlg m_dlg;
  virtual ~CUIThread();
  // Generated message map functions
  //{{AFX_MSG(CUIThread)
    // NOTE - the ClassWizard will add and remove member functions here.
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
};

分别重载InitInstance()函数和ExitInstance()函数:BOOL CUIThread::InitInstance()
{
  m_dlg.Create(IDD_UITHREADDLG);
  m_dlg.ShowWindow(SW_SHOW);
  m_pMainWnd=&m_dlg;
  return TRUE;
}
int CUIThread::ExitInstance()
{
  m_dlg.DestroyWindow();
  return CWinThread::ExitInstance();
}

双击按钮IDC_UI_THREAD,添加消息响应函数:void CMultiThread6Dlg::OnUiThread()
{
  CWinThread *pThread=AfxBeginThread(RUNTIME_CLASS(CUIThread));
}
并在MultiThread6Dlg.cpp的开头添加:#include "UIThread.h"

好了,编译并运行程序吧。每单击一次“用户界面线程”按钮,都会弹出一个线程对话框,在任何一个线程对话框内按下鼠标左键,都会弹出一个消息框。

(未完待续)

上一页  2 3 4 5 6 7 

Tags:线程 编程 MFC

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接