MFCui线程

参考MFCp292,win32多线程程序设计


MyThread.h文件里添加如下代码:

DECLARE_DYNCREATE(MyThread)//声明动态生成

public:

    MyThread(void);

    ~MyThread(void);

    //{{AFX_VIRTUAL(MyThread)

public:

    virtual BOOL InitInstance();//初始化实例

    virtual int ExitInstance();//退出

    //}}AFX_VIRTUAL

    void MyMessageHandler(WPARAM, LPARAM);//线程的消息处理函数

    // Generated message map functions

    //{{AFX_MSG(MyThread)

    // NOTE - the ClassWizard will add and remove member functions here.

    //}}AFX_MSG

DECLARE_MESSAGE_MAP()//声明消息映射

 

MyThread.cpp里添加代码如下:

在文件的开头添加:(这一行比较重要)

IMPLEMENT_DYNCREATE(MyThread, CWinThread)//这里是要调用的,不然就会

 

 

什么消息映射:

//MyThread.cpp

BEGIN_MESSAGE_MAP(MyThread, CWinThread)

    //{{AFX_MSG_MAP(CMyThread)

    // NOTE - the ClassWizard will add and remove mapping macros here.

    ON_THREAD_MESSAGE ( WM_MYTHREADMESSAGE, MyMessageHandler )

    //}}AFX_MSG_MAP

END_MESSAGE_MAP()

编写消息映射函数如下:

void MyThread::MyMessageHandler(WPARAM, LPARAM)

{

    //required functionality.

    CFrameWnd *l_FrameWnd;

    CView *l_View;

    l_FrameWnd = (CFrameWnd *)AfxGetApp()->m_pMainWnd;

    l_View = l_FrameWnd ->GetActiveView();

    CClientDC dc(l_View);

    dc.TextOut(100,y,TEXT("Hello"));

    y = y+30;

}

 

下面这两个函数几乎可以不用实现

InitInstance()

ExitInstance(){return CWinThread::ExitInstance();}

 

2、        在工程的view类里添加生成线程的代码

MyThread* pThread;

      pThread = new MyThread();

      pThread->CreateThread();

      pThread->PostThreadMessage(WM_MYTHREADMESSAGE,NULL,NULL);

这里发送消息了之后,就会在view里看见一行字,实现这个功能的代码在线程里

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值