用户界面线程的AfxBeginThread的原型如下:
CWinThread* AFXAPI AfxBeginThread(
CRuntimeClass* pThreadClass,
int nPriority,
UINT nStackSize,
DWORD dwCreateFlags,
LPSECURITY_ATTRIBUTES lpSecurityAttrs)
CRuntimeClass* pThreadClass,
int nPriority,
UINT nStackSize,
DWORD dwCreateFlags,
LPSECURITY_ATTRIBUTES lpSecurityAttrs)
其中:
参数1是从CWinThread派生的RUNTIME_CLASS类;
参数2指定线程优先级,如果为0,则与创建该线程的线程相同;
参数3指定线程的
堆栈大小,如果为0,则与创建该线程的线程相同;
参数4是一个创建标识,如果是CREATE_SUSPENDED,则在悬挂状态创建
线程,在线程创建后线程挂起,否则线程在创建后开始线程的执行。
参数5表示线程的安全属性,NT下有用。
本例子建立的工程名称为WinThread
首先建立一个UIThread 这个类。这个类的作用是过一会要通过AfxBeginThread 来启动的。然后通过这个类来完成窗口类的创建和使用
看代码
首先建立CwinThread派生类 UIThread.h
#pragma once
#include "TanChuDlg.h"//0
class UIThread : public CWinThread //这里建造派生类
{
DECLARE_DYNCREATE(UIThread)//必须有他 //1 和CPP里的一个是对应的。必须有
public://初始化和析构
UIThread(); //2
virtual ~UIThread(); //3
//重载初始化和退出两个函数
virtual BOOL InitInstance();//4
virtual int ExitInstance();//5
protected:
DECLARE_MESSAGE_MAP()//5 和BIGEN END MESSAGE 成对使用的
public:
TanChuDlg m_tanchu; 是为了在 InitInstance里面调用.domodle
};
UIThread.cpp
#pragma once
#include "stdafx.h"
#include "UIThread.h"
IMPLEMENT_DYNCREATE(UIThread, CWinThread)//与UIThread.h对应的
UIThread::UIThread()//构造函数实现
{
}
UIThread::~UIThread()//析构函数实现
{
}
BOOL UIThread::InitInstance()//初始化函数实现
{
m_tanchu.DoModal();
return TRUE;
}
int UIThread::ExitInstance()//退出函数实现
{
return 0;
}
BEGIN_MESSAGE_MAP(UIThread,CWinThread)//消息
END_MESSAGE_MAP()
#include "stdafx.h"
#include "UIThread.h"
IMPLEMENT_DYNCREATE(UIThread, CWinThread)//与UIThread.h对应的
UIThread::UIThread()//构造函数实现
{
}
UIThread::~UIThread()//析构函数实现
{
}
BOOL UIThread::InitInstance()//初始化函数实现
{
m_tanchu.DoModal();
return TRUE;
}
int UIThread::ExitInstance()//退出函数实现
{
return 0;
}
BEGIN_MESSAGE_MAP(UIThread,CWinThread)//消息
END_MESSAGE_MAP()
下面是通过建立对话框,自己命名的一个类
#pragma once
#include "stdafx.h"
#include "UIThread.h"
IMPLEMENT_DYNCREATE(UIThread, CWinThread)//与UIThread.h对应的
UIThread::UIThread()//构造函数实现
{
}
UIThread::~UIThread()//析构函数实现
{
}
BOOL UIThread::InitInstance()//初始化函数实现
{
m_tanchu.DoModal();
return TRUE;
}
int UIThread::ExitInstance()//退出函数实现
{
return 0;
}
BEGIN_MESSAGE_MAP(UIThread,CWinThread)//消息
END_MESSAGE_MAP()
#include "stdafx.h"
#include "UIThread.h"
IMPLEMENT_DYNCREATE(UIThread, CWinThread)//与UIThread.h对应的
UIThread::UIThread()//构造函数实现
{
}
UIThread::~UIThread()//析构函数实现
{
}
BOOL UIThread::InitInstance()//初始化函数实现
{
m_tanchu.DoModal();
return TRUE;
}
int UIThread::ExitInstance()//退出函数实现
{
return 0;
}
BEGIN_MESSAGE_MAP(UIThread,CWinThread)//消息
END_MESSAGE_MAP()
下面是通过建立对话框系统自行建立的类
TanChuDlg.h
#pragma once
#include "resource.h"
#include "afxcmn.h"
// TanChuDlg 对话框
class TanChuDlg : public CDialog
{
DECLARE_DYNAMIC(TanChuDlg)
public:
TanChuDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~TanChuDlg();
// 对话框数据
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
virtual BOOL OnInitDialog();
CAnimateCtrl m_anima;
virtual BOOL PreTranslateMessage(MSG* pMsg);
};
#include "resource.h"
#include "afxcmn.h"
// TanChuDlg 对话框
class TanChuDlg : public CDialog
{
DECLARE_DYNAMIC(TanChuDlg)
public:
TanChuDlg(CWnd* pParent = NULL); // 标准构造函数
virtual ~TanChuDlg();
// 对话框数据
enum { IDD = IDD_DIALOG1 };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnBnClickedButton1();
virtual BOOL OnInitDialog();
CAnimateCtrl m_anima;
virtual BOOL PreTranslateMessage(MSG* pMsg);
};
// TanChuDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "WinThread.h"
#include "TanChuDlg.h"
// TanChuDlg 对话框
IMPLEMENT_DYNAMIC(TanChuDlg, CDialog)
TanChuDlg::TanChuDlg(CWnd* pParent /*=NULL*/)
: CDialog(TanChuDlg::IDD, pParent)
{
}
TanChuDlg::~TanChuDlg()
{
}
void TanChuDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_ANIMATE1, m_anima);
}
BEGIN_MESSAGE_MAP(TanChuDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &TanChuDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// TanChuDlg 消息处理程序
void TanChuDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
}
BOOL TanChuDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_anima.Open(IDR_AVI1);显示加载的AVI图像
// TODO: 在此添加额外的初始化
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
BOOL TanChuDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN) //屏蔽回车和ESC
return TRUE;
if (pMsg->message == WM_SYSKEYDOWN && pMsg->wParam == VK_F4 ) //屏蔽ALT+F4
return TRUE;
// TODO: 在此添加专用代码和/或调用基类
return CDialog::PreTranslateMessage(pMsg);
}
//
#include "stdafx.h"
#include "WinThread.h"
#include "TanChuDlg.h"
// TanChuDlg 对话框
IMPLEMENT_DYNAMIC(TanChuDlg, CDialog)
TanChuDlg::TanChuDlg(CWnd* pParent /*=NULL*/)
: CDialog(TanChuDlg::IDD, pParent)
{
}
TanChuDlg::~TanChuDlg()
{
}
void TanChuDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_ANIMATE1, m_anima);
}
BEGIN_MESSAGE_MAP(TanChuDlg, CDialog)
ON_BN_CLICKED(IDC_BUTTON1, &TanChuDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
// TanChuDlg 消息处理程序
void TanChuDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
}
BOOL TanChuDlg::OnInitDialog()
{
CDialog::OnInitDialog();
m_anima.Open(IDR_AVI1);显示加载的AVI图像
// TODO: 在此添加额外的初始化
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
BOOL TanChuDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN)
if (pMsg->wParam==VK_ESCAPE || pMsg->wParam==VK_RETURN) //屏蔽回车和ESC
return TRUE;
if (pMsg->message == WM_SYSKEYDOWN && pMsg->wParam == VK_F4 ) //屏蔽ALT+F4
return TRUE;
// TODO: 在此添加专用代码和/或调用基类
return CDialog::PreTranslateMessage(pMsg);
}