我自定义一个类,继承自CWinThread,在类中实现对大漠插件的调用,在线程函数中向指定窗口发送字符串。
1、类的头文件
#pragma once
#ifndef UserThread_H
#define UserThread_H
// CUserThread
#include "Cdmsoft.h"
class CUserThread : public CWinThread
{
public:
CUserThread();
CUserThread(AFX_THREADPROC pfnThreadProc);
CUserThread(AFX_THREADPROC pfnThreadProc, LONG task, LONG hwnd);//线程的启动函数 及实际任务索引
static UINT ThreadFunc(LPVOID param); //线程函数
virtual ~CUserThread();
public:
LONG m_nStartCounter; //成员变量,统计启用的线程数量
LONG m_DestinationhWnd;
LONG m_Task;
LONG m_X;
LONG m_Y;
Cdmsoft m_DM;
CString m_CurrentMission;
BOOLEAN m_bEnable;
private:
virtual VOID Go(); // The "real" startup function
public:
void MainMission();
};
#endif
2、类的cpp
// UserThread.cpp : 实现文件
//
#pragma once
#include "stdafx.h"
#include "UserThread.h"
#include "GlobalDefine.h"
// CUserThread
CUserThread::CUserThread()
{
}
CUserThread::CUserThread(AFX_THREADPROC pfnThreadProc) :CWinThread(pfnThreadProc, NULL)
{
m_bAutoDelete = FALSE;
// Set the pointer to the class to be the startup value.
// m_pThreadParams is undocumented,
// but there is no work-around.
m_pThreadParams = this;
}
CUserThread::CUserThread(AFX_THREADPROC pfnThreadProc, LONG task,LONG hwnd) :CWinThread(pfnThreadProc, NULL)
{
m_DestinationhWnd = hwnd;
m_bAutoDelete = FALSE;
m_pThreadParams = this;
m_Task = task;
::CoInitialize(NULL); //初始化线程COM库
m_DM.CreateDispatch(_T("dm.dmsoft"), NULL);
}
CUserThread::~CUserThread()
{
::CoUninitialize();
}
// static
UINT CUserThread::ThreadFunc(LPVOID n)
{
CUserThread* pThread = (CUserThread*)n;
pThread->Go();
return 0;
}
void CUserThread::Go()
{
int n = m_nStartCounter;
switch (m_Task)
{
case 0:
MainMission();
break;
case 1:
break;
case 2:
break;
default:
break;
}
}
void CUserThread::MainMission()
{
m_CurrentMission = _T("开始");
while (m_bEnable)
{
if (!(m_DM.IsBind(m_DestinationhWnd)))
{
m_DM.BindWindow(m_DestinationhWnd, Display, Mouse, Keyboard, BindMode);
}
m_DM.SendString(m_DestinationhWnd, _T("m_DestinationhWnd"));
m_DM.delay(1000);
}
m_CurrentMission = _T("停止");
//::CoUninitialize(); //卸载COM库
}
现在创建了2个对象,分别对2个窗口发送字符串,但是只能对前面的窗口发送,后面的窗口发送不了,点一下后面的窗口,就对后面的窗口发送,前面的又不能发送了