如何在程序中启动两个窗口窗体?

时间:2012-04-30 00:08:38

标签: visual-studio-2010 c++-cli

我有两种形式。我想同时开始他们两个。在主程序中,我遵循Md Kamruzzaman Pallob的建议。以下代码是更新版本,但它仍然无法正常工作。

错误是错误C3350:'System :: Threading :: ThreadStart':委托构造函数需要1个参数

   #include "stdafx.h"
  #include "Form1.h"
  #include "Form3.h"
   using namespace MySearch;
   using namespace System;
   using namespace System::Threading;



 public ref class ThreadX{
 public: ThreadX(){}
 public: static void func1()
{
    Application::Run(gcnew Form1());
}

public: static void func2()
{
    Application::Run(gcnew Form3());
}


};


[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false); 

// Create the main window and run it


    ThreadX^ o1 = gcnew ThreadX();
    ThreadX^ o2 = gcnew ThreadX();

    Thread^ th = gcnew Thread(gcnew ThreadStart(o1, &ThreadX::func1));
    Thread^ th1 = gcnew Thread(gcnew ThreadStart(o2, &ThreadX::func2));
    th->Start();
    th1->Start();



return 0;

}

3 个答案:

答案 0 :(得分:1)

为什么不直接创建一个form1加载事件? :

private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
         Form2^ form2 = gcnew Form2;
         form2->Show();
     }

然后每次Form1打开时,Form2也会打开。它似乎对我有用。

答案 1 :(得分:0)

您可以使用线程来完成。对不起,因为我不太了解c ++。但我可以在c#

中给你解决方案
 public static void func1()
    {
        Application.Run(new Form1());
    }

   public static void func2()
    {
        Application.Run(new Form2());
    }

    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Thread th = new Thread(func1);
        Thread th1 = new Thread(func2);
        th.Start();
        th1.Start();
    }

答案 2 :(得分:0)

尝试:

Thread^ th = gcnew Thread(gcnew ThreadStart( &ThreadX::func1 ) );
Thread^ th1 = gcnew Thread(gcnew ThreadStart( &ThreadX::func2 ) );

请参阅http://msdn.microsoft.com/en-us/library/system.threading.threadstart.aspx