2009-10-26 176 views

回答

6

您應該能夠添加引用了System.Windows.Forms的,然後是好去。您可能還必須將STAThreadAttribute應用於應用程序的入口點。

using System.Windows.Forms; 

class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     MessageBox.Show("hello"); 
    } 
} 

...更復雜......

using System.Windows.Forms; 

class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     var frm = new Form(); 
     frm.Name = "Hello"; 
     var lb = new Label(); 
     lb.Text = "Hello World!!!"; 
     frm.Controls.Add(lb); 
     frm.ShowDialog(); 
    } 
} 
4

是的,你可以在控制檯中初始化一個表單。添加到System.Windows.Forms的一個參考,使用下面的示例代碼:

System.Windows.Forms.Form f = new System.Windows.Forms.Form(); 
f.ShowDialog(); 
+0

我可以在downmods上得到一些評論嗎? – 2009-10-26 20:09:26

+0

爲什麼這是低調?這可能不是很好的做法,但它絕對有可能。 – 2009-10-26 20:09:58

+0

這個工作沒有STAThread屬性嗎? – 2009-10-26 20:15:41

1

你可以試試這個

using System.Windows.Forms; 

[STAThread] 
static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.Run(new MyForm()); 
} 

再見。

4

常見的答案:

[STAThread] 
static void Main() 
{  
    Application.Run(new MyForm()); 
} 

替代品(從here拍攝)如果,例如 - 你想從比主應用程序的線程上推出的一種形式:

Thread t = new Thread(new ThreadStart(StartNewStaThread)); 

// Make sure to set the apartment state BEFORE starting the thread. 
t.ApartmentState = ApartmentState.STA; 
t.Start(); 

private void StartNewStaThread() { 
    Application.Run(new Form1()); 
} 

Thread t = new Thread(new ThreadStart(StartNewStaThread)); 
t.Start(); 

[STAThread] 
private void StartNewStaThread() { 
    Application.Run(new Form1()); 
}