我看书上讲,用CWinThread可以启动一个用户界面线程,但又没给例子,不知哪位大侠能提供一个例子。因为现在做的项目中涉及到该问题,所以很急,谢谢了。

解决方案 »

  1.   

    http://www.vchelp.net/wyy/paper/z_thread.asp
      

  2.   

    十分感谢。不过有个问题,当运行示例程序时,如果按了Loop按钮,则另一线程中的对话框的OK和Cancel按钮就不能操作了,为什么?
      

  3.   

    可以稍微做一下改动BOOL CMyThread::InitInstance()
    {
    CMyDlg *pDlg=new CMyDlg;
    pDlg->Create(IDD_GUIDLG,CWnd::FromHandle(GetDesktopWindow()));//指定其父窗口为桌面就正常了.
    pDlg->ShowWindow(SW_SHOW);
    m_pMainWnd = pDlg;
    return TRUE;
    }
      

  4.   

    我想新开能支持Document/View结构一个窗口该如何??!!!有没有这方面的例子?
      

  5.   

    不要总是找例子,研究一下doc/view的内在,自己实现!
      

  6.   

    Step 1:Using the AppWizard, create a dialog based application by choosing the "Dialog based" in the step 1 of the wizard and click finish.Step 2:As explained before, we have to drive our own class derived from CWinThread class and pass run-time information about the class to the user interface version of AfxBeginThread(...) function, so open the class view, write click on the classes (Yourprojectname class) and choose "New class" from the pop up menu.Step 3:From the "new class" dialog box ,create a new class with CWinThread as the "Base class" and use MFC class as the "Class type" and enter a name for this class.(CYourThreadClassName). Step 4:Save your project and look at the newly created class from the class view, inspect the source code for your thread class .Note that the wizard has provided stub implementation of InitInstance() and ExitInstance() functions and in these function you should add your class-specific implementation code. You may notice also that a message map is implemented for your thread class( as explained before the user interface threads implement a message map to respond to events and messages.)Step 5:If the thread class should respond to the user events it should be associated with an user interface object and we use an object of CDialog class. So create a dialog resource and associate it with a CDialog class. ( CYourDialogClassName)Step 6:Open the class view, expand your newly created dialog class and double click on the constructor function. Call the Create function in this function as below:Create(YourDialogResourceID,NULL);
    You find your dialog resource ID from the "Dialog Properties" by entering on the Dialog resource from the keyboard or by right clicking on the dialog resource  and choosing Properties from the pop op menu. Step 7:Click on class view, expand the thread class and double click on the InitInstance() function. Now we create a dialog object in this function and assign the m_pMainWnd member variable of the thread class to this object.( By doing this ,our thread class  associates with a user interface object ). Add the following code into the IniInstance() function.        m_pMainWnd=new CYourDialogClassName;
            m_pMainWnd->SetForegroundWindow();
            m_pMainWnd->ShowWindow(SW_SHOW);
            
    Remember to include the header file of your dialog class to the currently opened file. Step 8:It will be a god idea to make an array of CWinThread pointers and assign these pointers to the created threads, so it would be easy to get our thread objects whenever necessary so open the class view, double click on the CYourProjectNameApp class and in the class definition add the following code:CWinThread* m_pThread[5];(Remember to add: extern CYourProjectNameApp theApp; at the bottom of the currently opened file, after DECLARE_MESSAGE_MAP()};)Step 9:Put a button in the main dialog window in order to create and run  the user interface threads.Step 10:With this new button selected, open the class wizard by entering CTRL+W from the keyboard, and make a command handler function.Step 11:Open this newly created handler function and add the following code in order to create 5 User interface threads:        for(int i=0;i<5;i++)
                 theApp.m_pThread[i]=AfxBeginThread(RUNTIME_CLASS(CYourThreadClassName));
            
    (Remember to include the header file of your thread class (YourThreadClassName.h) at the top of the  currently opened file)That's it, Save your project and build it .