CWinThreadの派生クラスでウィンドウを持つスレッドを作る(VC++) ウィンドウを持つスレッドを作る場合は、メインスレッドのメッセージ処理ループを利用できないので、スレッドの中で、メッセージ処理ループを実装しなければいけません。この場合は、メッセージ処理ル 1.スレッドクラスを作ります。CWinThreadから、CTestThreadクラスを発生します。InitInstance()関数を実装します。コンストラクターの上のprotected:をpublic:に修正しておきます。テストのため、CFrameWndクラスによるウィンドウを表示するような初期化ぐらいを実装しました。 // CTestThread class CTestThread : public CWinThread { DECLARE_DYNCREATE(CTestThread) //protected: public: CTestThread(); virtual ~CTestThread(); public: virtual BOOL InitInstance(); virtual int ExitInstance(); protected: DECLARE_MESSAGE_MAP() }; // CTestThread IMPLEMENT_DYNCREATE(CTestThread, CWinThread) CTestThread::CTestThread() { } CTestThread::~CTestThread() { } BOOL CTestThread::InitInstance() { CFrameWnd* pWnd = new CFrameWnd; pWnd->Create(NULL,L"CWinThread Test"); pWnd->ShowWindow(SW_SHOW); pWnd->UpdateWindow(); m_pMainWnd = pWnd; return TRUE; } int CTestThread::ExitInstance() { // TODO: perform any per-thread cleanup here return CWinThread::ExitInstance(); } BEGIN_MESSAGE_MAP(CTestThread, CWinThread) END_MESSAGE_MAP() 2.スレッドを開始させます。 void Test() { CRuntimeClass *pRuntime = RUNTIME_CLASS(CTestThread); g_pThread = (CTestThread*)pRuntime->CreateObject(); g_pThread->CreateThread(0, 0, NULL); ... }3.スレッドを停止、削除します。 CWinThreadのコンストラクターの中で、m_bAutoDeleteメンバーをTRUEに設定してくれるので、ワー |